#include <bits/stdc++.h>
using namespace std;
long long check(long long a, long long b) {}
int main() {
  long long n, x;
  long long ans = x;
  cin >> n >> x;
  long long a;
  if (2 * x > n)
    a = n - x;
  else {
    ans += n - x;
    ans += x;
    ans += x;
    a = n - 2 * x;
  }
  long long px = x;
  while (x % a)
    ans += 2 * (a * (x / a)) - a, px = x, x = a, a = px - 2 * (a * (px / a));
  cout << ans + 2 * (a * (x / a)) + a << endl;
  return 0;
}
