#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T, class U>
inline T tmin(T a, U b) {
  return (a < b) ? a : b;
}
template <class T, class U>
inline T tmax(T a, U b) {
  return (a > b) ? a : b;
}
template <class T, class U>
inline void amax(T &a, U b) {
  if (b > a) a = b;
}
template <class T, class U>
inline void amin(T &a, U b) {
  if (b < a) a = b;
}
template <class T>
inline T tabs(T a) {
  return (a > 0) ? a : -a;
}
template <class T>
T gcd(T a, T b) {
  while (b != 0) {
    T c = a;
    a = b;
    b = c % b;
  }
  return a;
}
long long f(long long N, long long X) {
  if (N <= 1 || X == 0 || X == N) return 0;
  if (X + X >= N) {
    return f(X, X + X - N) + 3 * (N - X);
  } else {
    long long k = N / X - 1;
    return f(N - k * X, X) + 3 * k * X;
  }
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  long long N, X;
  cin >> N >> X;
  cout << f(N, X) << endl;
  return 0;
}
