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