#include <bits/stdc++.h>
using namespace std;
vector<int> G[100005];
queue<int> C;
int used[100005];
int bfs(int u) {
  C.push(u);
  used[u] = 1;
  while (C.size() > 0) {
    int x = C.front();
    C.pop();
    for (int i = 0; i < G[x].size(); i++) {
      if (used[G[x][i]] == 0) C.push(G[x][i]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  long long a, b;
  cin >> a >> b;
  long long br = 0;
  while (a != 0 && b != 0) {
    if (a > b) {
      br = br + a / b;
      a %= b;
      swap(a, b);
    } else {
      swap(a, b);
      br = br + a / b;
      a %= b;
      swap(a, b);
    }
  }
  cout << br << endl;
}
