#include <bits/stdc++.h>
const int INF = 100000000;
const double EPS = 1e-10;
const int MOD = 1000000007;
using namespace std;
long long f(long long a, long long b) {
  if (b == 0) {
    return 0;
  } else if (2 * b <= a) {
    return 3 * b + f(a - b, a - 2 * b);
  } else {
    return 3 * (a - b) + f(b, 2 * b - a);
  }
}
long long n, x;
int main() {
  cin >> n >> x;
  cout << f(n, x) << endl;
}
