#include <bits/stdc++.h>
int gcd(int m, int n) {
  while (m != n) {
    if (m > n) {
      m = m - n;
    } else {
      n = n - m;
    }
  }
  return m;
}
int divided(int n, int x) {
  if (n % 2 == 0 && x == n / 2) {
    return 3 * n / 2;
  } else {
    return 3 * n - 3;
  }
}
int main() {
  int N, X;
  std::cin >> N >> X;
  int g = gcd(N, X);
  std::cout << g * divided(N / g, X / g) << std::endl;
  return 0;
}
