#include <bits/stdc++.h>
using namespace std;
signed main(void) {
  long long N, X;
  cin >> N >> X;
  long long Ans = N;
  long long a = N - X, b = X;
  if (a > b) swap(a, b);
  while (1) {
    if (a == 0) {
      break;
    }
    if (a == 1) {
      Ans += 2 * b - 1;
      break;
    }
    if (a == b) {
      Ans += a;
      break;
    }
    Ans += 2 * a * (long long)(b / a);
    b = b % a;
    if (a > b) swap(a, b);
  }
  cout << Ans << endl;
  return 0;
}
