#include <bits/stdc++.h>
using namespace std;
unsigned long long solve(unsigned long long a, unsigned long long b) {
  if (a == 0 || b == 0) return 0;
  if (a > b) return 3 * b + solve(a - b, b);
  return 3 * a + solve(b - a, a);
}
int main() {
  unsigned long long N, X;
  scanf("%lld %lld", &N, &X);
  printf("%lld\n", solve(N - X, X));
}
