#include <bits/stdc++.h>
using namespace std;
template <class Type>
long long quick_pow(Type x, Type y, Type Mod) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % Mod;
    x = x * x % Mod, y >>= 1;
  }
  return res;
}
template <class Type>
long long inv(Type x, Type Mod) {
  return quick(x, Mod - 2, Mod);
}
namespace IO {
template <class T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
};  // namespace IO
using namespace IO;
long long f(long long x, long long y) {
  if (y % x == 0)
    return 2 * (y / x) * x;
  else
    return 2 * (y / x) * x + f(y % x, x);
}
int main() {
  long long n, x;
  cin >> n >> x;
  cout << f(x, n) << endl;
  return 0;
}
