#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  x = min(x, n - x);
  int ans = 0;
  if (n % x == 0) {
    ans = x * 2 + (n - x) + (n - 2 * x) * 2;
  } else {
    int tmp = n / x;
    int tmp2 = n - x * tmp;
    ans = x * 2 + (n - x);
    ans += (tmp - 2) * 2 * x;
    ans += ((x / tmp2) - 1) * 3 * tmp2;
    ans += x;
  }
  cout << ans << endl;
  return 0;
}
