#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(x) (x).begin(),(x).end() #define rep(i,m,n) for(int i = m;i < n;++i) #define pb push_back #define fore(i,a) for(auto &i:a) #define rrep(i,m,n) for(int i = m;i >= n;--i) #define INF INT_MAX/2 using namespace std; using ll = long long; using R = double; const ll MOD = 1e9 + 7; const ll inf = 1LL << 50; struct edge { ll from; ll to; ll cost; }; ll gcd(ll a, ll b) { if (a == 0 || b == 0)return 0; if (a%b == 0)return b; return gcd(b, a%b); } int main() { ll n, m; cin >> m >> n; if (gcd(n, m) > 1) { n /= gcd(n, m); m /= gcd(n, m); } ll ans = 0; while (1) { if (m % n == 0) { ans += m / n - 1; break; } else if (m < n) { swap(m, n); } else if (m > n) { m -= n; } ans++; } cout << ans << endl; return 0; }