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