#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = (1e18);
const ll mod = 1000000007;
const ll mod2 = 998244353;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
int main() {
  ll n, x;
  cin >> n >> x;
  ll ans = 0;
  ans = (n - x) + ((n - x) / x) * 2 * x +
        ((n - x) % x > 0 ? (x / ((n - x) % x) * 2 - 1) * ((n - x) % x) : -x) +
        x;
  cout << ans << endl;
}
