#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<unordered_map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define mp make_pair
#define pb push_back
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define drep(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
inline void gn(long long&x){
    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long LL;
typedef double db;
inline db sqrf(db a){return a*a;}

using namespace std;

//const double PI = 3.1415926535897932384626433832795028842;

/**const int N = 1005, M = 100005, MOD = 1000000007;

int n, m;
int a[N][N];

int lowbit(int x) {
    return x & (-x);
}

void add(int &x, int y) {
    x = ((LL) x + y) % MOD;
}

void update(int x, int y, int val) {
    int z;
    for (; x <= n; x += lowbit(x)) {
        for (z = y; z <= n; z += lowbit(z)) add(a[x][z], val);
    }
}

int cal(int x, int y) {
    int z, ans = 0;
    for (; x > 0; x -= lowbit(x)) {
        for (z = y; z > 0; z -= lowbit(z)) add(ans, a[x][z]);
    }
    return ans;
}

int main(int argc, const char * argv[]) {
    int x0, x1, y0, y1, x, y, v;
    char cmd[10];
    while (~scanf("%d %d", &n, &m)) {
        //printf("DDDD\n");
        memset(a, 0, sizeof(a));
        while (m--) {
            scanf("%s", cmd);
            if (cmd[0] == 'A') {
                scanf("%d %d %d", &x, &y, &v);
                x++, y++;
                update(x, y, v);
            } else {
                scanf("%d %d %d %d", &x0, &y0, &x1, &y1);
                x0++, y0++, x1++, y1++;
                LL ans = (LL) cal(x1, y1) - cal(x1, y0 - 1) - cal(x0 - 1, y1) + cal(x0 - 1, y0 - 1);
                int res = ans % MOD;
                printf("%d\n", res);
            }
        }
    }
    return 0;
}

const int N = 1000000007;

LL k1, k2, x, y, z, a1, a2;

double rstt[2][60];
LL b[60];
map<LL, int> imap;

double cal(LL t, int k) {
    double rst = 1.0;
    LL tem = (t & ~(t - 1));
    while (t) {
        t -= tem;
        rst *= rstt[k][imap[tem]];
        tem = (t & ~(t - 1));
    }
    tem = rst;
    tem /= N;
    rst -= tem * N;
    return rst;
}

int main(int argc, const char * argv[]) {
    gn(k1), gn(k2), gn(x), gn(y), gn(a1), gn(a2), gn(z);
    
    double r1, r2;
    
    double del = sqrt(k1 * k1 + 4 * k2);
    
    r1 = (k1 + del) / 2;
    r2 = (k1 - del) / 2;
    
    rstt[0][0] = rstt[1][0] = 1.0;
    rstt[0][0] = r1;
    rstt[1][0] = r2;
    b[0] = 1;
    b[1] = 2;
    imap[1] = 0;
    rep(i, 1, 60) {
        rstt[0][i] = rstt[0][i - 1] * rstt[0][i - 1];
        rstt[1][i] = rstt[1][i - 1] * rstt[1][i - 1];
        LL tem1 = rstt[0][i];
        LL tem2 = rstt[1][i];
        tem1 /= N;
        tem2 /= N;
        rstt[0][i] -= tem1 * N;
        rstt[1][i] -= tem2 * N;
        b[i] = 2 * b[i - 1];
        imap[b[i]] = i;
    }
    
    if (del == 0) {
        double f = cal(x, 0);
        double p = a1 / f;
        double g = cal(z, 0);
        LL rst = p * g;
        rst %= N;
        printf("%lld", rst);
        return 0;
    }
    
    double f1 = cal(x, 0), f2 = cal(x, 1);
    double g1 = cal(y, 0), g2 = cal(y, 1);
    
    double p = (a1 * g2 - a2 * f2) / (f1 * g2 - g1 * f2);
    double q = (a1 * g1 - a2 * f1) / (f2 * g1 - g2 * f1);
    
    double h1 = cal(z, 0), h2 = cal(z, 1);
    
    double rst = p * h1 + q * h2;
    LL tem = rst;
    tem /= N;
    rst -= tem * N;
    int t = (rst + 0.5);
    printf("%d", t);
    
    return 0;
}**/

LL n, x;

LL cal(LL a, LL b) {
    if (a % b == 0) {
        return 3 * a;
    }
    LL t1 = a / b;
    LL t2 = a % b;
    return t1 * b * 3 + cal(b, t2);
    
}

int main() {
    gn(n), gn(x);
    
    printf("%lld", cal(x, n - x));
    
    return 0;
}



















Measured memory_bytes: 256