#include <bits/stdc++.h>
using ll = long long;
ll N, X;
ll f(ll a, ll b) { return b ? 2 * b * (a / b) + f(b, a % b) : -1; }
int main() {
  scanf("%lld%lld", &N, &X);
  ll a = N - X, b = X;
  if (a < b) std::swap(a, b);
  printf("%lld\n", N + f(a, b));
  return 0;
}
