#include <bits/stdc++.h>
const double EPS = 1e-9;
using namespace std;
void dbg() { cout << "\n"; }
template <typename T, typename... A>
void dbg(T a, A... x) {
  cout << a << ' ';
  dbg(x...);
}
long long solve(long long n, long long x) {
  long long ans = n;
  long long h1 = x, h2 = n - x;
  while (1) {
    if (h1 == h2) {
      ans += h1;
      break;
    }
    if (h1 < h2) {
      ans += 2 * h1;
      h2 -= h1;
    } else {
      ans += 2 * h2;
      h1 -= h2;
    }
  }
  return ans;
}
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
long long n, x;
int main() {
  scanf("%lld%lld", &n, &x);
  printf("%lld\n", solve(n, x));
  return 0;
}
