#include <iostream>
#include <algorithm>
using namespace std;
long long n , x;

int main(){
  cin >> n >> x;
  if(__gcd(n , x) == 1) cout << 3 * (n - 1) << endl;
  else cout << 3 * max(x , n - x) <<endl;
  return 0;
}
