#include #include #include #include using namespace std; using PII = pair; const int wide = 1000; vector> check_dat; bool check(int x, int y) { return check_dat[x + wide][y + wide]; } void checked(int x, int y, bool val) { check_dat[x + wide][y + wide] = val; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int x, y, x2, y2; cin >> x >> y; int xy_gcd = gcd(x, y); x /= xy_gcd; y /= xy_gcd; if (x == y) { cout << 0 << endl; return 0; } if (abs(x - y) % 2 == 1) { cout << -1 << endl; return 0; } check_dat.assign(2 * wide + 1, vector(2 * wide + 1, false)); checked(x, y, true); vector v1, v2; v1.emplace_back(make_pair(x, y)); int ans = 0; while (!v1.empty()) { v2.clear(); ans++; for (auto p : v1) { x = p.first; y = p.second; x2 = y; y2 = x; // printf("%8d: %4d %4d\n", ans, x2, y2); if (!check(x2, y2)) { checked(x, y, true); v2.emplace_back(make_pair(x2, y2)); } x2 = x + y; y2 = x - y; // printf("%8d: %4d %4d\n", ans, x2, y2); if (x2 == y2) { cout << ans << endl; return 0; } if (abs(x2) > wide || abs(y2) > wide) { continue; } if (!check(x2, y2)) { checked(x, y, true); v2.emplace_back(make_pair(x2, y2)); } } v1 = v2; } cout << -1 << endl; return 0; }