#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair P; typedef tuple T; typedef vector vec; inline bool cheak(ll x, ll y, ll xMax, ll yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; } inline ll toInt(string s) { ll v; istringstream sin(s); sin >> v; return v; } template inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template inline T sqr(T x) { return x*x; } #define For(i,a,b) for(ll (i) = (a);i < (b);(i)++) #define rep(i,n) For(i,0,n) #define rFor(i,a,b) for(ll (i) = (a-1);i >= (b);(i)--) #define rrep(i,n) rFor(i,n,0) #define clr(a) memset((a), 0 ,sizeof(a)) #define mclr(a) memset((a), -1 ,sizeof(a)) #define all(a) (a).begin(),(a).end() #define sz(a) (sizeof(a)) const ll dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }, dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 }; const ll mod = 1e9 + 7; const ll INF = 1e9 + 9; int gcd(int a, int b){ return (b == 0 ? a : gcd(b, a % b)); } int main(){ int m, n; cin >> m >> n; int g = gcd(m, n); m /= g; n /= g; int cnt = 0; while (true){ if (n == 1){ cnt += (m - 1); break; } else{ if (m > n){ m -= n; } else{ swap(m, n); } cnt++; } } cout << cnt << endl; return 0; }