#include <bits/stdc++.h>
long long int N, X;
long long int gcd(long long int m, long long int n) {
  if (0 == m || 0 == n) {
    return 0;
  }
  while (m != n) {
    if (m > n) {
      m = m - n;
    } else {
      n = n - m;
    }
  }
  return m;
}
int main(void) {
  scanf("%lld%lld", &N, &X);
  printf("%lld\n", 3 * (N - gcd(N, X)));
  return 0;
}
