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