#include using namespace std; #define rep(i, n) for(int i = 0; i < int(n); i++) using ll = long long; using P = pair; int fx[] = {6, 2, 5, 5, 4, 5, 6, 4, 7, 6}; string f(string s) { int res = 0; rep(i, s.size()) { if(s[i] == '-') res++; else { int now = s[i] - '0'; res += fx[now]; } } return to_string(res); } int my_stoi(string s) { int res = 0; rep(i, s.size()) { res *= 10; res += s[i] - '0'; } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); string p, n; cin >> p >> n; if (n == "0") { cout << p << endl; return 0; } map mp; int start, syuuki; for(int i = 1; 1; i++) { p = f(p); int now = my_stoi(p); // cout << "now" << " " << now << endl; if(mp[now] != 0) { // mp[now]回進んでからの後は(i - mp[now])回周期で回るのだ start = mp[now], syuuki = i - mp[now]; break; } mp[my_stoi(p)] = i; } // cout << start << " " << syuuki << endl; // 周期が始まる前に終わるやつ if (n.size() < 8 && my_stoi(n) < start) { for (auto elem : mp) { if (elem.second == my_stoi(n)) { cout << elem.first << endl; return 0; } } } int cnt = 0; rep(i, n.size()) { cnt *= 10; cnt %= syuuki; cnt += (int)n[i] - '0'; cnt %= syuuki; } cnt -= start; cnt %= syuuki; if(cnt < 0) cnt += syuuki; // cout << start << " " << cnt << " " << syuuki << endl; for(auto elem : mp) { // cout << "{ " << elem.first << " " << elem.second << " }" << endl; if(elem.second == start + cnt) { cout << elem.first << endl; return 0; } } return 0; }