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