#include <bits/stdc++.h>
using namespace std;
long long N, X;
long long sum;
int rec(int a, int b) {
  if (b == 0) return a;
  sum += (a / b) * 2 * b;
  return rec(b, a % b);
}
int main() {
  cin >> N >> X;
  sum = N;
  sum -= rec(N - X, X);
  cout << sum << endl;
  return 0;
}
