#include <bits/stdc++.h>
using namespace std;
long long n, x, ans;
int main() {
  cin >> n >> x;
  x = x < (n - x) ? x : (n - x);
  if (n % x == 0) {
    ans = (n / x - 1) * 3 * x;
    printf("%lld\n", ans);
  } else {
    ans = 3 * x + (n / x - 1) * x * 3;
    printf("%lld\n", ans);
  }
  return 0;
}
