#include <bits/stdc++.h>
long long solve1(long long n, long long m);
long long solve2(long long n, long long m);
long long solve3(long long n, long long m) {
  if (n % m == 0)
    return 3 * n - m;
  else {
    return n + n / m * 2 * m + solve3(m, n % m);
  }
}
long long solve2(long long n, long long m) {
  if (n > m) {
    if (n % m == 0) return n * 3;
    return solve3(m, n % m) + n + n / m * m * 2;
  } else {
    if (m % n == 0) return m * 3;
    return solve3(n, m % n) + m + m / n * n * 2;
  }
}
long long solve1(long long n, long long m) {
  if (n * 2 == m)
    return n * 3;
  else
    return solve2(n, m - n);
}
int main() {
  long long n, x;
  scanf("%lld%lld", &n, &x);
  printf("%lld\n", solve1(x, n));
  return 0;
}
