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