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