#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t N, X;
  cin >> N >> X;
  int64_t m, M, q, r;
  m = min(X, N - X);
  M = max(X, N - X);
  q = M / m;
  r = M % m;
  int64_t answer;
  answer = N + 2 * m * q + 2 * m - r;
  cout << answer << endl;
}
