#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
template <class T>
ostream &operator<<(std::ostream &o, const vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    o << (i > 0 ? " " : "") << v[i];
  }
  return o;
}
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  ll n, x;
  cin >> n >> x;
  ll g = gcd(n, x);
  if (g == 1)
    cout << (n - 1) * 3 << endl;
  else {
    cout << ((n / g) - 1) * 3 * g << endl;
  }
}
