#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 31;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const double eps = 1e-6;
const double pi = atan2(0, -1);
long long solve(long long n, long long x) {
  if (x == 0)
    return 0;
  else if (x == n)
    return n * 3;
  else {
    if (x * 2 <= n)
      return x + (n - x) + x + solve(n - x, n - x * 2) - (n - x * 2);
    else
      return x + (n - x) + (n - x) + solve(x, x * 2 - n) - (x * 2 - n);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  long long n, x;
  cin >> n >> x;
  cout << solve(n, x) << endl;
  return 0;
}
