#include <bits/stdc++.h>
using namespace std;
long long cnt = 0;
void resistance(long long m, long long n) {
  if (n != 0) {
    cnt += m / n;
    resistance(n, m % n);
  }
}
int main() {
  long long a, b;
  cin >> a >> b;
  resistance(a, b);
  cout << cnt << endl;
  return 0;
}
