#include <bits/stdc++.h>
using namespace std;
long long int n, x, y, ans;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> x;
  while (n >= 2 && x >= 1) {
    x = max(x, n - x);
    y = n - x;
    ans += 3 * (x / y) * y;
    x %= y;
    n -= y;
  }
  cout << ans;
  return 0;
}
