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