d = [1, 0, 0, 0, 1, 0, 1, 0, 2, 1] s = [0] * 11 for i in range(10): s[i+1] = s[i] + d[i] def g(n): res = 0 while n: res += d[n % 10] n //= 10 return res def f(n): if n == 0: return 0 x = n // 10 y = n % 10 if x == 0: return s[y] res = s[y] + x * s[10] res += g(x) * y + (f(x) - 1) * 10 return res k = int(input()) + 1 l = 0 r = 1 while f(r) <= k: l = r r *= 2 while r - l > 1: c = l + r >> 1 if f(c) <= k: l = c else: r = c if f(l) == k: print(l - 1) else: print(-1)