#include <bits/stdc++.h>
using namespace std;
const long long LLINF = (long long)(1e18);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  int T = 1;
  while (T--) {
    long long a, b;
    cin >> a >> b;
    long long ans = 0;
    while (min(a, b) != 1) {
      ans += a / b;
      long long temp = b;
      b = a - (a / b) * b;
      a = temp;
    }
    ans += max(a, b);
    cout << ans << "\n";
  }
}
