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
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]

numdict = {'all':'1869234570','-1':'2345896','-2':'1345869','-3':'1245986','-4':'1235689','-5':'1234968','-6':'1235948','-8':'1234569','-9':'1234856'}

def list2num(numlist):
    num = 0
    for digit in numlist:
        num = num * 10 + digit
    #print(num)
    #input()
    return num

def div7(numlist):
    pailie = itertools.permutations(numlist)
    flag = 0
    for yuanshu in pailie:
        num = list2num(yuanshu)
        if num % 7 == 0:
            flag = 1
            return num
            break
    if flag == 0:
        return 0

def pre_num(num_string):
    count = []
    for i in range(10):
        if str(i) in '1689':
            count.append(num_string.count(str(i)) - 1)
        else:
            count.append(num_string.count(str(i)))
    return count
    
def simulinkinput():
    tmp = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    tmp = tmp * 5
    for i in range(10, 40):
        zhuhe = itertools.combinations(tmp, i)
        for yuanshu in zhuhe:
            num_string = str(list2num(yuanshu)) + '1689'
            main(num_string)

def count2num(numlist):
    tmplist = []
    for i in range(1,len(numlist)):
        if numlist[i] > 0:
            tmplist.append(i)
    return tmplist

def main():
    num_string = input()
    result_string = ['','111111','222222','333333','444444','555555','666666','777777','888888','999999']
    res_list = [1,8,6,9]
    count = []
    count = pre_num(num_string)
    result = ''
    for i in range(1,10):
        if count[i] >= 6:
            loop = count[i] // 6
            result = result + result_string[i] * loop
            count[i] = count[i] % 6
            
    mincount = min(count[1:])
    if mincount > 0:
        result = result + '186923457' * mincount
        for i in range(1,10):
            count[i] = count[i] - mincount
    for i in range(9):
        numzhlist = count2num(count)
        mincount = min(count[1:])
        tmpstring = div7(numzhlist)
        if tmpstring != 0:
            result = result + str(tmpstring) * mincount
            for j in numzhlist:
                count[j] = count[j] - mincount
        else:
            break
    for i in range(1,10):
        for j in range(count[i]):
            res_list.append(i)
    resnum = div7(res_list)
    if resnum == 0:
        exit('0')
    else:
        result = result + str(resnum) + '0' * count[0]
    print(result)
    #print('%s/7 = %d' %(result,int(result)/7.0))

        
if __name__ == '__main__':
    main()
    #simulinkinput()
    '''
    while True:
        main()
        input()
    '''
    # print(__name__)
