'''
n = int(input())

if n<=6:
    print()
else:
    n -= 7
    share = i // 9 
    if share % 2 == 0:
        print()
    else:
        print()
'''

n = int(input())
i = 666
cnt = 0
# s = set()
while(True):
    if '666' in str(i):
        cnt += 1
    if cnt == n:
        print(i)
        break
    i += 1

