#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool flag = true, fl = true;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
string abc = "abcdefghijklmnopqrstuvwxyz";
struct edge {
  ll to, cost;
};
struct etree {
  ll from, to, cost;
};
double pai = 3.141592653589793;
bool comp(const etree& e1, const etree& e2) { return e1.cost < e2.cost; }
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int n, x;
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
}
