#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int tt = 1;
  while (tt--) {
    long long n, x = 0, m = 0, y = 0, k = (2e+18);
    long long a, b;
    cin >> a >> b;
    while (a > 1) {
      x += a / b;
      a = a % b;
      swap(a, b);
    }
    x += b;
    cout << x << '\n';
  }
}
