#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline int pls(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}
inline int dec(int x, int y) {
  x -= y;
  return x < 0 ? x + mod : x;
}
inline int mul(int x, int y) { return 1ll * x * y % mod; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
inline int mul(int x, int y, int z, int p) { return mul(mul(x, y), mul(z, p)); }
inline void add(int &x, int y) {
  x += y;
  x = (x >= mod) ? x - mod : x;
}
inline int fpw(int x, int y, int r = 1) {
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1) r = mul(r, x);
  return r;
}
inline int inv(int x) { return fpw(x, mod - 2); }
inline int gi() {
  int x;
  int _w = scanf("%d", &x);
  return x;
}
inline void gi(int &x) { int _w = scanf("%d", &x); }
inline void write(int x) { printf("%d\n", x); }
void chkmax(int &x, int y) { x = (x > y) ? x : y; }
void chkmin(int &x, int y) { x = (x < y) ? x : y; }
const int INF = 0x3f3f3f3f;
template <typename T>
inline void debug(T x) {
  cerr << x;
  return;
}
long long ans, n, x;
void work(int a, int b) {
  if (!a) {
    ans += -b;
    return;
  }
  ans += 2 * (b / a) * a;
  work(b % a, a);
}
int main() {
  scanf("%lld%lld", &n, &x);
  ans += n;
  work(x, n - x);
  cout << ans << endl;
  return 0;
}
