#include <bits/stdc++.h>
using namespace std;
long long a, b, res;
void thuchien() {
  res = 0;
  while (a != 1) {
    if (a > b) res += a / b;
    a %= b;
    swap(a, b);
  }
  res += b;
  cout << res << endl;
}
int main() {
  cin >> a >> b;
  thuchien();
  return 0;
}
