#include <bits/stdc++.h>
using namespace std;
int gcd(long long a, long long b) {
  long long c;
  if (a < b) swap(a, b);
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int main() {
  long long N, X;
  cin >> N >> X;
  cout << 3 * (N - gcd(N, X)) << endl;
}
