#include #include #include #include #include #include #include using namespace std; const int INF = 100000000; int gcd( int m, int n ) { if((0==m)||(0==n)) return 0; while( m != n ) { if (m>n) m = m - n; else n = n - m; } return m; } struct K{ int n; int m; int deep; K():n(0),m(0), deep(0){} K(int n,int m, int deep) { this->n = n; this->m = m; this->deep = deep; } }; int main(){ int N,M; scanf("%d %d",&N,&M); int ret = gcd(N,M); N /= ret; M /= ret; queue q; q.push(K(1,1,0)); while(q.size()!=0) { K k = q.front(); q.pop(); if(k.n == N && k.m == M) { printf("%d\n",k.deep); return 0; } K nk1=k, nk2=k; nk1.n += k.m; nk1.deep++; nk2.n = k.m; nk2.m = k.n; nk2.deep++; if(nk1.n > N && nk1.m > M) q.push(nk1); if(nk2.n > N && nk2.m > M) q.push(nk2); } return 0; }