#include <bits/stdc++.h>
long getgcd(long N, long X);
int main(void) {
  long N, X;
  scanf("%ld %ld", &N, &X);
  printf("%ld\n", 3 * (N - (getgcd(N, X))));
}
long getgcd(long N, long X) {
  long i;
  long num;
  for (i = 0; i < (long)sqrt(N) + 1; i++) {
    if ((fmod(N, i) == 0) && (fmod(X, i) == 0)) {
      num = i;
    }
  }
  return num;
}
