#include using namespace std; using pint = pair; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i> x1 >> x2; pint p1 = fac(x1), p2 = fac(x2); int cnt = 0; while (true) { if (p1 == p2) return 0; int dist = abs(p1.first - p2.first) + abs(p1.second - p2.second); if (dist == 1) p1 = p2; else if (dist % 2 == 0) { if (p1 == pint(9, 9)) p1 = pint(7, 9); else if (p1 == pint(7, 9)) p1 = pint(9, 9); else if (p1.second < 9) p1.second++; else if (p1.first == 8) p1.first--; else p1.first++; } else { if (p1.first <= p2.first + 1 and p1.first < 9) p1.first++; else if (p1.second <= p2.second + 1 and p1.second < 9) p1.second++; else if (p1.second - 2 > p2.second) p1.second--; else if (p1.first - 2 > p2.first) p1.first--; else exit(8); } cnt++; if (cnt >= 36) return 0; cout << val(p1) << endl; if (p1 == p2) return 0; int x; cin >> x; p2 = fac(x); } }