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