#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  long long res = x;
  long long a = x, b = n - x;
  int phase = 0;
  while (b > 0) {
    long long syo = a / b, mod = a % b;
    long long toadd = b * (2 * syo + phase);
    res += toadd;
    a = b, b = mod;
    phase = (phase + 1) % 2;
  }
  cout << res << endl;
  return 0;
}
