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