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