#include <bits/stdc++.h>
auto& in = std::cin;
auto& out = std::cout;
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
inline int64_t func(int32_t a, int32_t b) {
  if (a > b) {
    std::swap(a, b);
  }
  if (a == 0) {
    return -b;
  }
  return (int64_t)(b / a) * 2 * (int64_t)(a) + func(a, b % a);
}
int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);
  int32_t N, X;
  in >> N >> X;
  out << func(N - X, X) + N << endl;
  return 0;
}
