#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, ans = 0;
  cin >> a >> b;
  bool haveToReduce = true;
  while (haveToReduce) {
    if (a > b) {
      if (b == 1) {
        ans += a;
        haveToReduce = false;
      } else {
        ans += (a / b);
        a %= b;
      }
    } else if (b > a) {
      if (a == 1) {
        ans += b;
        haveToReduce = false;
      } else {
        ans += (b / a);
        b %= a;
      }
    } else {
      ans++;
      haveToReduce = false;
    }
  }
  cout << ans;
  return 0;
}
