#include <bits/stdc++.h>
using namespace std;
long long int N;
long long int X;
long long int sum = 0;
long long int now[2];
long long int l;
long long int s;
int main() {
  scanf("%lld %lld", &N, &X);
  sum = N;
  now[0] = min(X, N - X);
  now[1] = max(X, N - X);
  while (now[0] != now[1]) {
    sum += now[0] * 2;
    s = min(now[0], now[1] - now[0]);
    l = max(now[0], now[1] - now[0]);
    if (s == l) sum += s;
    now[0] = s;
    now[1] = l;
  }
  printf("%lld", sum);
  return 0;
}
