#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-11;
int vex[4] = {1, 0, -1, 0};
int vey[4] = {0, 1, 0, -1};
int gcd(int a, int b) {
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
int main() {
  int N, X;
  cin >> N >> X;
  cout << 3 * (N - gcd(N, X)) << endl;
  return 0;
}
