#include <bits/stdc++.h>
using namespace std;
long long int f_shikaku(long long int A, long long int B) {
  long long int a = max(A, B);
  long long int b = min(A, B);
  if (a % b == 0) return 2 * (int)(a / b) * b - b;
  return f_shikaku(a % b, b) + 2 * (int)(a / b) * b;
}
int main(void) {
  long long int N, X;
  scanf("%lld %lld", &N, &X);
  long long int ans;
  ans = f_shikaku(X, N - X) + N;
  printf("%lld\n", ans);
}
