#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x, a, b;
  cin >> n >> x;
  a = n;
  b = x;
  while (a) {
    if (a >= b)
      a -= b;
    else {
      int t = a;
      a = b;
      b = t;
    }
  }
  cout << 3 * b * (n / b - 1) << endl;
  return 0;
}
