You have number *a*, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.

Number *a* doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.
The first line contains positive integer *a* in the decimal record. It is guaranteed that the record of number *a* contains digits: 1, 6, 8, 9. Number *a* doesn't contain any leading zeroes. The decimal representation of number *a* contains at least 4 and at most 10<sup class="upper-index">6</sup> characters.
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
# from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect, insort
from time import perf_counter
from fractions import Fraction
import copy
from copy import deepcopy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
    # sys.setrecursionlimit(int(pow(10,6)))
    sys.stdin = open("input.txt", "r")
    # sys.stdout = open("output.txt", "w")
except:
    pass
def pmat(A):
    for ele in A: print(*ele,end="\n")

# from sys import stdin
# input = stdin.buffer.readline
# I = lambda : list(map(int,input().split()))

A=["1","8","6","9"]

pos=[]
for ele in perm(A):
    x=int("".join(ele))
    if x%7==0:
        pos=str(x)
        break

n=input()
d={}
for ele in n:
    d[ele]=d.get(ele,0)+1

cnt=min(d["1"],d["8"],d["6"],d["9"])
y=pos*cnt
d["1"]-=cnt
d["6"]-=cnt 
d["9"]-=cnt 
d["8"]-=cnt 

for ele in d:
    if ele!="0":
        y=ele*d[ele]+y 
    else:
        y=y+ele*d[ele]
print(y)
