#include <bits/stdc++.h>
int main() {
  long long N, X;
  std::cin >> N >> X;
  long long L = 2 * X + N - N % (N - X);
  if (N % (N - X) == 0) {
    L -= N - X;
    std::cout << L << std::endl;
    return 0;
  }
  int a = (N - X) - N % (N - X);
  int b = N - X;
  while (1) {
    L += 3 * (b - a) - (b - a) % a;
    if ((b - a) % a == 0) {
      L -= a;
      std::cout << L << std::endl;
      return 0;
    }
    int tmp = a;
    a -= (b - a) % a;
    b = tmp;
  }
}
