#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cout << fixed << setprecision(12);
  long long a, b;
  cin >> a >> b;
  long long ans = 0;
  if (a % b == 0) {
    cout << (a / b) << "\n";
    return 0;
  }
  ans += a / b;
  long long r = a % b;
  a = r;
  while (true) {
    if (a == 1) {
      ans += b;
      break;
    }
    ans += b / a;
    r = (b % a);
    b = a;
    a = r;
  }
  cout << (ans) << "\n";
  return 0;
}
