#!/usr/bin/python2 # -*- coding: utf-8 -*- # † def f(x): a = map(int, str(x)) n = len(a) # dp[][nuke0][15でのあまり] := パターン数 dp = [[[0] * 15 for _ in xrange(2)] for _ in xrange(2)] dp[0][0][0] = 1 for i in xrange(n): ndp = [[[0] * 15 for _ in xrange(2)] for _ in xrange(2)] for less in xrange(2): for nuke0 in xrange(2): for rem in xrange(15): if dp[less][nuke0][rem] == 0: continue for d in xrange(10 if less else a[i]+1): if d != 0 and d != 3 and d != 5: continue if nuke0 and d == 0: continue nless = less or d < a[i] n_nuke0 = nuke0 or d > 0 nrem = (rem * 10 + d) % 15 ndp[nless][n_nuke0][nrem] += dp[less][nuke0][rem] dp = ndp res = 0 for less in xrange(2): res += dp[less][1][0] return res def g(n): lo, hi = 0, 2**100 while hi - lo > 1: md = (lo + hi) / 2 if f(md) < n: lo = md else: hi = md return hi n = int(raw_input()) res = g(n) print res