#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  if (x > n - x) x = n - x;
  long long ans = x;
  long long b1 = n - x, b2 = x;
  long long c1, c2;
  while (1) {
    ans += b1 + b2;
    if (b1 == b2) break;
    c1 = b1;
    c2 = b2;
    b1 = c2;
    b2 = c1 - c2;
    if (b2 > b1) {
      ans += (c1 - c2) * 2;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
