#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 1e6;
const int inf = 1e9 + 7;
long long n, x, ans;
int main() {
  boost();
  cin >> n >> x;
  if ((n - x - (n - x) / x * x) != 0) {
    ans += x / (n - x - (n - x) / x * x) * (n - x - (n - x) / x * x) * 2 -
           (n - x - (n - x) / x * x);
  }
  if ((n - x) % x == 0) {
    ans += (n - x) / x * x * 2 - x;
  } else {
    ans += (n - x) / x * x * 2;
  }
  cout << ans + n;
}
