#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int INF = 1 << 28;
const double EPS = 1e-10;
static const int MAX = 100000;
static const int INFTY = 1 << 29;
int main() {
  long long n, x, ans = 0;
  cin >> n >> x;
  if (n == 2 * x) {
    ans += 3 * x;
  } else {
    if (n - 2 * x == x) {
      ans += x;
    } else if (n - 2 * x > x) {
      ans = n;
      long long t = (n - x) / x;
      ans += (t * 2 - 1) * x;
    } else {
      ans += (x + n - x + x + x);
      long long t = x / (n - 2 * x);
      ans += (t * 2 - 1) * (n - 2 * x);
    }
  }
  cout << ans << endl;
  return 0;
}
