#include using namespace std; typedef long long ll; typedef unsigned long ul; typedef unsigned long long ull; #define limit 20000000 unsigned long facts[limit]; ul factorial(ul x) { if(x < limit && facts[x] != 0) return facts[x]; if (x <= 1) return 1; ull result = 1; while (x > 1) { result *= x; x--; } if(x < limit) facts[x] = result; return result; } unsigned long fs[limit]; ul f(ul x) { if (x < limit && fs[x] != 0) return fs[x]; if (x <= 9) { ul r = factorial(x); if(x < limit) fs[x] = r; return r; } ul r = factorial(x % 10) + f(x / 10); if(x < limit) fs[x] = r; return r; } int main(void) { ios_base::sync_with_stdio(false); ull y; cin >> y; ull x = 0; while(f(x) != y) x++; cout << x << endl; return 0; }