#include <bits/stdc++.h>
int main() {
  long long n, x;
  std::cin >> n >> x;
  long long h = n - x, l = x;
  long long sum = 0;
  while (h) {
    sum += l;
    sum += std::max(0ll, (l / h * 2ll - 1ll) * h);
    auto tmp = h;
    h = l % h;
    l = tmp;
  }
  std::cout << sum << std::endl;
  return 0;
}
