#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long LINF = 1LL << 62;
const int INF = 10000;
long long dfs(long long N, long long x, long long sum) {
  if (N % x == 0) {
    return sum += N * 2 - x;
  }
  if (N > x)
    return dfs(x, N % x, sum + N / x * 2 * x);
  else
    return dfs(x, N, sum + x);
}
int main() {
  long long N, x;
  cin >> N >> x;
  if (x % (N - x) == 0) {
    cout << x * 3 << endl;
  } else
    cout << dfs(x, N - x, x) << endl;
  return 0;
}
