#include<bits/stdc++.h>
using namespace std;
typedef long long int64;

int main()
{
  int64 N, X;
  cin >> N >> X;
  int64 Line = (N - 1) * 3;
  X = min(N - X, X);
  if(__gcd(N, X) != 1) {
    cout << Line - (X - 1) * 3 << endl;
  } else {
    cout << Line << endl;
  }
}
