#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const char ENDL = '\n';
long long solve(long long a, long long b) {
  if (b > a) {
    swap(a, b);
  }
  if (b == 0) {
    return 0;
  } else if (b == 1) {
    return a;
  } else {
    return b + solve(a - b, b);
  }
}
signed main(void) {
  ios::sync_with_stdio(false);
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(10);
  cin.tie(0);
  long long n, x;
  cin >> n >> x;
  if (x > n / 2) {
    x = n - x;
  }
  cout << 3 * solve(n - x, x) << ENDL;
  return 0;
}
