#include using namespace std; void gcd( int x, int y, int &cnt ){ if( x == 1 and y == 1 ) return; if( not ( x >= y ) ) ++cnt, swap( x, y ); if( y == 1 ) return ( void ) ( cnt += x - 1 ); cnt += x / y; gcd( x % y, y, cnt ); } signed main(){ int M, N; cin >> M >> N; int g = __gcd( M, N ); M /= g, N /= g; int ans = 0; gcd( M, N, ans ); cout << ans << endl; return 0; }