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.
'''
1: 111111 / 7 = 15873
2: 222222 / 7 = 31746
3: 333333 / 7 = 47619
4: 444444 / 7 = 63492
5: 555555 / 7 = 79365
6: 666666 / 7 = 95238
7: 7 / 7 = 1
8: 888888 / 7 = 126984
9: 999999 / 7 = 142857
##############################################
for i in range(1,10):
    t = 0
    while True:
        t += 1
        num = ''
        for n in range(0,t):
            num = num + str(i)
            #print(num)
        #print(num)
        if int(num, 10) % 7 == 0:
            print('%d: %s / 7 = %d' %(i, num, int(num, 10) / 7))
            break
'''
import itertools
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#digits = digits * 5

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

#将列表转换为整数例如：[1,2,3]->123
def list2num(numlist):
    num = 0
    for digit in numlist:
        num = num * 10 + digit
    #print(num)
    #input()
    return num

#传入一个数字列表，判断所有重新排列的数字可能是否可以被7整除
def div7(numlist):
    pailie = itertools.permutations(numlist)
    #pailist = list(pailie)
    flag = 0
    for yuanshu in pailie:
        num = list2num(yuanshu)
        if num % 7 == 0:
            #print(numlist)
            #print('%d / 7 = %d' %(num, num/7))
            #count += 1
            flag = 1
            return num
            break
    if flag == 0:
        return 0
        #count += 1
        #print(0)

#传入一个数字字符串，返回每个数出现的次数，并剔除1689
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)
        #zhlist = list(zhuhe)
        for yuanshu in zhuhe:
            num_string = str(list2num(yuanshu)) + '1689'
            main(num_string)
#传入一个列表，返回大于0的索引
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()
    #num_string = '0'
    result_string = ['','111111','222222','333333','444444','555555','666666','777777','888888','999999']
    res_list = [1,8,6,9]
    count = []
    count = pre_num(num_string)
    result = ''
    #将输入的数字个数大于6个的整理成已知的排列方式因为程序最开始的测试
    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
            #print(result)
            
    #若个数最小值大于0，则按已知的可能进行排序
    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:
        print(0)
        exit()
    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__)
