#include <bits/stdc++.h>

using namespace std;

#define REP(i,m) for(long long i=0; i<m; i++)
#define FOR(i,n,m) for(long long i=n; i<m; i++)
#define INF 1000000000
#define INFL 10000000000000000LL
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define ll long long int
#define P pair<ll, ll>

ll gac(ll a, ll b){
  if(b == 0) return a;
  else return gcd(b, a%b);
}

int main() {
  ll n, x;
  cin >> n >> x;
  cout << 3*(n-gcd(n, x)) << endl;
  return 0;
}