#include <bits/stdc++.h>
using namespace std;
void omajinai() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(15);
}
int pp[3] = {2, 0, 1};
int nn[3] = {1, 2, 0};
vector<long long> t;
long long f(int choten, bool reverse) {
  if (t[0] == 0) return 0;
  if (!reverse) {
    if (t[choten] == pp[choten]) return pp[choten];
    if (t[pp[choten]] < t[choten]) {
      t[choten] -= t[pp[choten]];
      return f(nn[choten], false);
    }
  }
  t[pp[choten]] -= t[choten];
  return t[choten] + f(choten, !reverse);
}
int main() {
  omajinai();
  long long n, x;
  cin >> n >> x;
  t.resize(3, n);
  t[0] = n - x;
  long long ans = f(nn[0], false);
  cout << ans << endl;
}
