/** * @FileName a.cpp * @Author kanpurin * @Created 2022.05.21 02:16:25 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; int f2(int n) { if (n < 0) return 0; else if (n <= 3) return 1; else if (n <= 5) return 2; else if (n <= 7) return 3; else if (n <= 8) return 5; else return 6; } ll f(ll n) { string s = to_string(n); vector dp(s.size()+1),cnt(s.size()+1); int t = 0; for (int i = 0; i < s.size(); i++) { if (i > 0) dp[i+1] += t*(s[i]-'0')+f2(s[i]-'0'-1); else dp[i+1] += f2(s[i]-'0'-1)-1; dp[i+1] += dp[i]*10+cnt[i]*6; if (i > 0) dp[i+1] += 5; if (s[i] == '0' || s[i] == '4' || s[i] == '6' || s[i] == '9') t++; else if (s[i] == '8') t+=2; cnt[i+1] = cnt[i]*10+s[i]-'0'+(i>0?9:-1); } return dp[s.size()]; } int main() { ll k;cin >> k; ll l = 0, r = 1e17; while(r-l>1) { ll mid = l+(r-l)/2; if (f(mid+1) >= k) { r = mid; } else { l = mid; } } if (f(r+1) == k) { cout << r << endl; } else { cout << -1 << endl; } return 0; }