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