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