#include #include #include #include using namespace std; #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define DEBUG(x) cout<<#x<<": "< P; typedef long long int LL; typedef pair LP; LL gcd(LL x, LL y){ return y==0 ? x : gcd(y, x%y); } int count(int p, int q){ //DEBUG(p); DEBUG(q); cout << endl; if(q == 1) return p-1; else return p/q+1+count(q, p%q); } int main() { ios::sync_with_stdio(false); cin.tie(0); int M, N; cin >> M >> N; int g = gcd(M,N); cout << count(M/g, N/g) << endl; return 0; }