#include <bits/stdc++.h>
using namespace std;
long long a, b, sol;
int main() {
  scanf("%lld%lld", &a, &b);
  sol += a;
  a -= b;
  while (a > 0 && b > 0) {
    if (a == 1 && b == 1) {
      sol++;
      break;
    }
    if (b > a) swap(a, b);
    sol += 2 * (a / b);
    a -= b;
  }
  printf("%lld\n", sol);
  return 0;
}
