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