#include <bits/stdc++.h>
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
using namespace std;
bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }
int takemod(int x) { return (x % 1000000007 + 1000000007) % 1000000007; }
int fpow(int base, int power, int MOD) {
  int result = 1;
  base = base % MOD;
  while (power) {
    if (power & 1) result = (result * base) % MOD;
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result;
}
int modinv(int x) { return fpow(x, 1000000007 - 2, 1000000007); }
void solve() {
  long long int a, b;
  cin >> a >> b;
  long long int ans = 0;
  if (a == 1 && b == 1) {
    cout << 1 << "\n";
    return;
  }
  while (a != 1 || b != 1) {
    if (a < b) {
      a = a;
      ans += (b / a);
      b = b % a;
    } else {
      ans += a / b;
      a = (a % b);
      b = b;
    }
    if (a == 1) {
      ans += b;
      break;
    }
    if (b == 1) {
      ans += a;
      break;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
