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