#include <bits/stdc++.h>
long long dist(long long M, long long N) {
  long long D;
  if (M < N) {
    D = M;
    M = N;
    N = D;
  }
  if (N == 0) {
    return -1;
  }
  D = (M / N) * 2 * N;
  return D + dist(N, M % N);
}
int main() {
  long long L, N, P, X;
  scanf("%lld%lld", &N, &X);
  if (2 * X == N)
    L = 3 * X;
  else
    L = N + dist(X, N - X);
  printf("%lld\n", L);
  return 0;
}
