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