#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
const long double PI = (long double)(3.1415926535897932384626433832795);
inline void yes() { cout << "Yes\n"; }
inline void no() { cout << "No\n"; }
inline void notposs() { cout << -1 << endl; }
bool tests = 0;
void solve() {
  long long a, b;
  cin >> a >> b;
  long long ans = 0;
  while (a && b) {
    if (a < b) swap(a, b);
    ans += a / b;
    a %= b;
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  if (tests) cin >> t;
  for (long long i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}
