/*
 Hey you out there in the cold
 Getting lonely getting old
 Can you feel me?
 Hey you standing in the aisles
 With itchy feet and fading smiles
 Can you feel me?
 Hey you don't help them to bury the light
 Don't give in without a fight
 
 Hey you out there on your own
 Sitting naked by the phone
 Would you touch me?
 Hey you with you ear against the wall
 Waiting for someone to call out
 Would you touch me?
 Hey you, would you help me to carry the stone?
 Open your heart, I'm coming home
 
 But it was only fantasy
 The wall was too high
 As you can see
 No matter how he tried
 He could not break free
 And the worms ate into his brain
 
 Hey you, out there on the road
 Always doing what you're told
 Can you help me?
 Hey you, out there beyond the wall
 Breaking bottles in the hall
 Can you help me?
 Hey you, don't tell me there's no hope at all
 Together we stand, divided we fall
 */
#include <vector>
#include <list>
#include <map>
#include <set>
#include "queue"
#include <deque>
#include <stack>
#include <numeric>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <cassert>
#include <unordered_map>
#include <regex>
#include <stdio.h>
#include <fstream>
using namespace std;
#define ll long long int
const int N = 270021;
#define lson l,mid,rt<<1
#define eps 1e-9
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
#define oo INT_MAX
#define noo INT_MIN
//if ( a > b || a > j || b < i ) {
//return ( ( sum ) { 0, 0 } );
//if ( a >= i && b <= j ) {
//return tree[ node ]
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
inline int scan(){
    char c = getchar();
    int x = 0;
    while(c<'0'||c>'9'){
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<1)+(x<<3)+c-'0';
        c=getchar();
    }
    return x;
}
//const int inf = 1 << 29;
#define pi 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
#define OUT_RT cerr << (float(clock()) / CLOCKS_PER_SEC) << endl
ll add(ll a, ll b){
    return a+b-(a+b>=mod)*mod;
}
ll sub(ll a, ll b){
    return a-b+(a-b<0)*mod;
}
ll mult(ll a, ll b){
    return (1LL*a*b)%mod;
}
inline ll gcd(ll a, ll b){
    ll t; while (b){ a = a%b; t = a; a = b; b = t; }return a;
}
ll exponentmod(ll a,ll b){
    ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;
}
int bin_1(ll x){
    int r=0; while(x) { r++; x&=x-1; } return r;
}
ll modpow(ll b,ll e,ll m){
    ll r=1; b=b%m; while(e>0) { if(e%2==1) r=(r*b)%m; e/=2; b=(b*b)%m; } return r;
}
ll modinverse(ll a){
    return modpow(a,mod-2,mod);
}
ll nCrmod(ll n,ll r,ll m){
    if(r>n) r=n-r; ll res=1; for(ll i=r;i>=1;i--) { res=(res*(n-i+1))%m; res=(res*modinverse(i)); } return res;
}
ll power(ll b,ll e){
    if(e==0) return 1; if (e%2==0) return power(b*b,e/2); return b*power(b*b,e/2);
}
double powerf(double b,int e){
    if(e==0) return 1; if(e%2==0)return powerf(b*b,e/2); else{ if(e>0)return b*powerf(b*b,e/2); else return powerf(b*b,e/2)/b;}
}
const int sievesize = (ll)1e6+10;
bool prime[sievesize+1];
void sieve(){
    ll x,y; prime[0]=prime[1]=true; for(x=2;x*x<=sievesize;x++){ if(!prime[x]) for(y=x*x;y<=sievesize;y+=x) prime[y]=true; }
}
ll nextPowerOf2(ll n){
    n--;n |= n >> 1;n |= n >> 2;n |= n >> 4;n |= n >> 8;n |= n >> 16;n++;return n;
}
bool isPerfectSquare(ll n){
    if (n < 0)return false;ll tst = (ll)(sqrt(n) + 0.5);return tst*tst == n;
}
bool triangle(ll a, ll b, ll c){
    return ((a+b)>c&&(b+c)>a&&(a+c)>b);
}
ll chartoint(char c){
    switch(c){case '1':return 1;break;case '2':return 2;break;case '3':return 3;break;case '4':return 4;break;case '5':return 5;break;case '6':return 6;break;case '7':return 7;break;case '8':return 8;break;case '9':return 9;break;default: return 0;break;}
}
ll isnum(char s){
    if(s=='1'||s=='2'||s=='3'||s=='4'||s=='5'||s=='6'||s=='7'||s=='8'||s=='9'||s=='0') return 1; return 0;
}
//std::bitset<8> foo (std::string("10110011"));
//int dx[] = {1, -1, 0, 0};
//int dy[] = {0, 0, 1, -1};
//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
//int dx[]= {0,0,1,-1};/*4 side move*/
//int dy[]= {-1,1,0,0};/*4 side move*/
//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
//int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
//int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/
//int x=(n & -n);
//int x=pow(2,floor(log(n)/log(2)));
// (val & (val - 1)) == 0 - - > check for power of two
ll n;
ll x;
int main(){
    cin >> n >> x;
    cout << 3 * ( n - gcd( n , x) ) << endl;
    return 0;
}

Measured memory_bytes: 256