#include <bits/stdc++.h>
long f(long N, long X) {
  long A = 2 * X * ((N / X) - 1);
  long B = N - X * (N / X);
  if (N % X == 0) {
    return A + X + B;
  }
  return A + X + B + f(X, X - B);
}
int main() {
  std::string line;
  std::getline(std::cin, line);
  char* p;
  p = &line[0];
  long N = std::strtol(p, &p, 0);
  long X = std::strtol(p, &p, 0);
  std::cout << f(N, X) + N - X << std::endl;
}
