import itertools from math import factorial comb = lambda n, k: factorial(n) // (factorial(k) * factorial(n - k)) N = int(input()) ct, cur = 0, 0 i = 3 while ct + cur < N: ct += cur cur = 0 for fives_num in range(3, i + 1, 3): cur += comb(i - 1, fives_num - 1) i += 1 i -= 1 lst = [] for fives_num in range(3, i + 1, 3): for t in itertools.combinations(range(i - 1), fives_num - 1): x = int(''.join(map(str, ['5' if j in t else '3' for j in range(i - 1)])) + '5') lst.append(x) lst.sort() print(lst[N - ct - 1])