#include <bits/stdc++.h>
using namespace std;
long long a, b, ans;
int main() {
  cin >> a >> b;
  while (b != 1) {
    ans += a / b;
    a %= b;
    swap(a, b);
  }
  ans += a;
  cout << ans << endl;
  return 0;
}
