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