#include <bits/stdc++.h>
using namespace std;
long long para(long long a, long long b) {
  if (a % b == 0)
    return b * (a / b * 2 - 1);
  else
    return 2 * b + para(b, a % b);
}
signed main() {
  long long n, x;
  cin >> n >> x;
  long long ret = 0;
  ret = n + para(max(x, n - x), min(x, n - x));
  cout << ret << endl;
}
