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