#define _USE_MATH_DEFINES #include using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int m, n; cin >> m >> n; int g = __gcd(m, n); m /= g; n /= g; int ans = 0; while (true) { if (n == 1) { ans += m - 1; break; } if (m < n) { ans++; swap(m, n); continue; } ans += m / n; m %= n; } cout << ans << endl; return 0; }