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