#include <bits/stdc++.h>
using namespace std;
long long N, X;
int func(int a, int b) {
  if (a == 1 && b == 1) return 1;
  return 2 * a + func(b - a, a);
}
int main() {
  long long N, X, answer;
  scanf("%lld %lld", &N, &X);
  answer = N + func(X, N - X);
  cout << answer << endl;
}
