#include<stdio.h>
long long s(long long n,long long x)
{
if(n%x==0)
return {2*(n/x)*x-x};
else{return {2*(n/x)*x+s(x,n%x)};
}
}
int main()
{
long long n,i;
scanf("%lld%lld",&n,&i);
printf("%lld",s(n-i,i)+n);
}