#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long n, x;
  cin >> n >> x;
  long long ans = n;
  long long a = x, b = n - x;
  do {
    ans += 2 * a * (b / a);
    int tmp = a;
    a = b % a;
    b = tmp;
  } while (b % a != 0);
  ans += a * (2 * (b / a) - 1);
  cout << ans << endl;
}
