#include <bits/stdc++.h>
int lgc(long long int a, long long int b) {
  long long int r, tmp;
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main(void) {
  long long int n, x;
  scanf("%lld %lld", &n, &x);
  long long int p, q;
  p = lgc(n, x);
  q = n / p;
  long long int ans;
  ans = p * (q - 1) * 3;
  printf("%lld\n", ans);
  return 0;
}
