#include <bits/stdc++.h>
using namespace std;
long long N, X;
long long solve(long long N, long long X) {
  if (X == 0) return 0;
  if (X * 2 == N) return 3 * X;
  if (X * 2 < N) {
    long long rem = N % X;
    if (rem == 0) rem += X;
    return (N / X - 1) * 3 * X + solve(rem + X, X);
  } else {
    long long rem = X % (N - X);
    if (rem == 0) rem += N - X;
    return 3 * (N - X) * (X / (N - X) - 1) + solve(rem + (N - X), rem);
  }
}
int main() {
  scanf("%lld%lld", &N, &X);
  printf("%lld\n", solve(N, X));
  return 0;
}
