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 itertools as ite
from sys import stdin, stdout

def calcSum(arr1, arr2):
    return sum([a * b for a, b in zip(arr1, arr2)])

def main():
    cycle = [1, 3, 2, 6, 4, 5, 1, 3, 2, 6, 4, 5]
    tam = len(cycle)

    perm = [x for x in ite.permutations([1, 6, 8, 9])]

    lines = stdin.readlines()
    for line in lines:
        num = line.strip()
        res = []
        #solution
        cnt = [0] * 10

        for c in num:
            cnt[ord(c) - ord('0')] += 1

        for c in "1689":
            cnt[ord(c) - ord('0')] -= 1
        
        idx = 0
        aux = 0
        for val in range(10):
            while (cnt[val] > 0):
                cnt[val] -= 1
                aux += val * cycle[idx % tam]
                # aux %= 7
                idx += 1
                res += str(val)

        idx %= 7
        currArr = cycle[idx: idx + 4]
        # print (aux, currArr)
        sol = []
        solu = False
        for auxArr in perm:
            sumMod = calcSum(auxArr, currArr)
            if (sumMod + aux) % 7 == 0:
                sol = [str(x) for x in auxArr]
                solu = True
                break
        
        if solu:
            sol.extend(res)
        else:
            sol = [0]
            
        stdout.write(str("".join(sol)))
        stdout.write("\n")

main()
  	 	  	  		  	 						   	  	 	