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