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.
def mod(num, a): 
      
    # Initialize result 
    res = 0
  
    # One by one process all digits 
    # of 'num' 
    for i in range(0, len(num)): 
        res = (res * 10 + int(num[i])) % a; 
  
    return re
from itertools import permutations 
perm = permutations([1,8,6,9]) 
n=[] 
for i in list(perm):
    s=''
    for j in list(i):
        s=s+str(j)
    n.append(int(s))
d={}
for i in n:
    d[i%7]=str(i)
s=input()
z=0
nz=''
c1,c2,c3,c4=0,0,0,0
for i in range(len(s)):
    if s[i]=='1' and c1==0:
        c1=1
    elif s[i]=='8' and c2==0:
        c2=1
    elif s[i]=='6' and c3==0:
        c3=1
    elif s[i]=='9' and c4==0:
        c4=1
    elif s[i]=='0':
        z=z+1
    else:
        nz=nz+s[i]
if nz!='':
    x=mod(int(nz+'0'*(z+4)),7)
    if x==0:
        ans=nz+'0'*z+d[0]
    else:
        ans=nz+'0'*z+d[7-x]
else:
    ans=d[0]+'0'*z
print(int(ans))