#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

void swarp(long long *a,long long *b){
  long long c;
  c = *a;
  *a = *b;
  *b = c;
}

long long solve(long long l,long long s){
  if (l == (l/s)*s) return 2*(l-s);
  else return ((l/s) * 2-1)*s + l % s + solve(s,l%s);
}

int main(){
  long long n,x,sum=0;
  cin >> n >> x;
  sum += n+2*x;
  if (n==2*x) {
    cout << x*3 << endl;
    return 0;
  } else if (n>2*x && (n-2*x>=x) && n%x == 0){
    sum += 2*n-5*x;
    cout << sum << endl;
    return 0;
  } else if (n>2*x &&(x>n-2*x) x % (n-2*x)==0){
    sum += 2*x-(n-2*x);
    cout << sum << endl;
    return 0;
  } else if (n>2*x) {
      long long l=n-2*x;
      if(x>l) swarp(&l,&x);
      sum += 2*(n-2*x);
      sum += solve(x,(n-2*x)%x);
      cout << sum << endl;
      return 0;
  } else {
    sum += solve(n,n-x);
    cout << sum << endl;
    return 0;
  }

}