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