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