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