#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  long long x1 = n - x, x2 = x;
  while (x1 > x2) {
    ans += x2;
    x1 = x1 - x2;
  }
  ans += x2;
  ans = ans * 3;
  cout << ans;
}
