#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int d;
  istringstream iss(s);
  iss >> d;
  return d;
}
template <class T>
inline string toString(T d) {
  ostringstream oss;
  oss << d;
  return oss.str();
}
template <class T>
inline T pow2(T x) {
  return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1000000000;
const long long LLINF = 10000000000000000LL;
long long N, X;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> X;
  long long g = gcd(N, X);
  long long ans = (N - 1) * 3 * g;
  cout << ans << endl;
  return 0;
}
