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