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