#include <bits/stdc++.h>
int parallelogram_len(unsigned long long A, unsigned long long B);
int parallelogram_len(unsigned long long A, unsigned long long B) {
  unsigned long long a, b, y;
  if (A < B) {
    a = A;
    b = B;
  } else {
    a = B;
    b = A;
  }
  if (b % a == 0) {
    y = 2 * std::floor(b / a) * a - a;
  } else {
    y = 2 * std::floor(b / a) * a + parallelogram_len(a, b % a);
  }
  return y;
}
int main(void) {
  unsigned long long N, X;
  std::cin >> N >> X;
  std::cout << N + parallelogram_len(X, N - X) << std::endl;
  return 0;
}
