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