#include "bits/stdc++.h" using namespace std; #define fastcin {\ cin.tie(0);\ ios::sync_with_stdio(false);\ } #define rep(i, a, b) for(int i = a; i < b; i++) #define print(x) cout << x << "\n" int main() { fastcin; int m, n, g, ans = 0; cin >> m >> n; g = gcd(m, n); m /= g; n /= g; while(n!=1) { if(m>n) { ans += m/n; m %= n; } else { swap(m, n); ans++; } } ans += m-1; print(ans); return 0; }