#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int a, b;
  cin >> a >> b;
  long long int ans = 0;
  while (a != 1 && b != 1) {
    long long int lar = max(a, b);
    long long int sma = min(a, b);
    ans += (lar / sma);
    lar %= sma;
    a = lar;
    b = sma;
  }
  ans += max(a, b);
  cout << ans << endl;
  return 0;
}
