#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int ans = 0;
  if (x == n / 2.0) {
    ans = 3 * x;
  } else if (x > n / 2.0) {
    long long int diff = n - x;
    long long int n_of_tria = x / n - x;
    ans = x + 2 * n_of_tria * diff;
  } else if (x < n / 2.0) {
    long long int diff = n - 2 * x;
    if (diff <= x) {
      long long int n_of_tria = x / diff;
      ans = x + (n - x) + x + x + 2 * (n_of_tria - 1) + diff;
    } else {
      long long int no_of_lines = 0;
      while (diff > x) {
        no_of_lines = no_of_lines + 2;
        diff = diff - x;
      }
      long long int n_of_tria = x / diff;
      ans = x + (n - x) + x + x + 2 * (n_of_tria - 1) + diff + no_of_lines * x;
    }
  }
  cout << ans;
  return 0;
}
