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