#include <bits/stdc++.h>
const int INF = 2147483647;
const long long int MOD = 1000000007;
using namespace std;
int main() {
  long long int n, x;
  cin >> n >> x;
  if (x > n / 2) {
    x = n - x;
  }
  if ((n - x) % x == 0) {
    cout << n - x + (n - x) / x * 2 * x << endl;
  } else {
    cout << n - x + (n - x) / x * 2 * x + x +
                (2 * x * ((n - x) % x) - ((n - x) % x))
         << endl;
  }
}
