#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back 
#define pf push_front 
#define pbp pop_back
#define pfp pop_front 
#define sz(x) x.size()
#define all(x) x.begin(),x.end() 
#define ios_base ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define open(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define F first
#define S second

using namespace std;

typedef unsigned long long ul;
typedef long long ll;
typedef long double ld;

const int mod = 1e9+7;
const int N = 1e5+123;
const int inf = 1e9+123;
const long long INF = 1e18;
const double eps = 1e-5;
ll n, x, ans;

void rec(ll a, ll b){
 if(b % a == 0){
  ans += b/a * (2*a) - a;
  return ;
 }else{
  ans += b/a * (2*a);
  rec(b % a, a);
 }
}

int main (){
 cin >> n >> x;
 rec(x, n-x);
 cout << ans + n;