#include using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; inline int solve(int x, int y) { // cerr << format("{} {}\n", x, y); if (y == 1) return x - 1; if (x < y) return solve(y, x) + 1; assert(x != y); return solve(x - y, y) + 1; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int x, y; cin >> x >> y; int g = gcd(x, y); x /= g, y /= g; cout << solve(x, y) << '\n'; return 0; }