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