#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long n, x;
  cin >> n >> x;
  long long result = 0;
  if (x > n / 2) {
    x = x - n / 2;
  }
  while (x != 0) {
    int tmp = x;
    result += (n / x - 1) * 3 * x;
    n = (n - x * (n / x - 1));
    x = n % tmp;
  }
  cout << result << endl;
  return 0;
}
