#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, w = 0;
  char c = getchar();
  while (!isdigit(c)) w |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return w ? -x : x;
}
long long a, b, ans;
signed main() {
  a = ans = read();
  b = read();
  do {
    ans += b * 2 * (a - b) / b;
    long long t = a;
    a = b;
    b = (t - b) % b;
  } while (b);
  printf("%lld", ans - a);
  return 0;
}
