#include <bits/stdc++.h>
int main() {
  std::string line;
  std::getline(std::cin, line);
  char* p;
  std::size_t N = std::strtol(line.data(), &p, 10);
  std::size_t X = std::strtol(p, NULL, 10);
  std::size_t T = N;
  std::size_t A = N - X;
  std::size_t B = X;
  while (true) {
    if (B == 0) {
      break;
    }
    std::size_t n = A / B;
    if (A % B == 0) {
      T += (2 * n - 1) * B;
      break;
    }
    T += 2 * n * B + 2 * (A % B);
    std::size_t a = A % B;
    std::size_t b = B - (A % B);
    A = a;
    B = b;
  }
  std::cout << T << std::endl;
}
