#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 405;
const long long mod = 1000000007;
const double PI = acos(-1.0);
long long n, x;
long long cal(long long n, long long x) {
  if (x <= 0) {
    return 3 * n;
  }
  if (n % x == 0) {
    return n * 3LL;
  } else {
    if (x < n - x) {
      return 3LL * x + n / x * cal(x, n % x);
    } else {
      return 3LL * (x) + x / (n - x) * cal(n - x, x % (n - x));
    }
  }
}
void solve() {
  long long res = 0;
  if (x * 2LL >= n) {
    res = cal(x, n - x);
  } else {
    res = cal(n - x, x);
  }
  printf("%lld\n", res);
}
int main() {
  while (cin >> n >> x) {
    solve();
  }
  return 0;
}
