#include <bits/stdc++.h>
using namespace std;
const int DX[4] = {0, 1, 0, -1}, DY[4] = {-1, 0, 1, 0};
const int INF = 1e9;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
int gcd(int a, int b) {
  int r;
  if (a < b) swap(a, b);
  while ((r = a % b) != 0) a = b, b = r;
  return b;
}
int main(int argc, char const *argv[]) {
  long long N, X;
  cin >> N >> X;
  cout << 3 * (N - gcd(N, X)) << endl;
  return 0;
}
