#include <bits/stdc++.h>
using namespace std;
long long N, X;
long long res;
int main() {
  cin >> N >> X;
  long long A = N - X, B = X;
  res = N;
  while (true) {
    if (A == B) {
      res += A;
      break;
    } else if (A > B) {
      res += 2 * B;
      A -= B;
    } else {
      res += 2 * A;
      B -= A;
    }
  }
  cout << res << endl;
  return 0;
}
