#include #include #include #include #include #include #include #include #include #include #include using ll = long long; using namespace std; int main(){ int m, n, ans = 0, t; cin >> m >> n; t = gcd(m, n); m /= t; n /= t; if (n == 1) { cout << m - 1 << endl; return 0; } else if (m > n) { while (m > n){ m -= n; ans++; } } while (m != 1){ swap(m, n); ans++; while (m > n){ m -= n; ans++; } } cout << ans + n << endl; }