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