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