#include using namespace std; using ll=int64_t; //参考元 //https://tjkendev.github.io/procon-library/python/math/stern-brocot-tree.html vector Stern_Brocot(ll p, ll q, ll n){ ll a=0,b=1,c=1,d=0; bool lu=true,ru=true; ll lx=0,ly=1,rx=1,ry=0; ll times=1000000010; ll s,t; while (times){ times--; s=a+c; t=b+d; if (t*ps*q){ a=s; b=t; if (t<=n){ lx=s; ly=t; }else{ ru=false; } } } return vector{lx,ly,rx,ry}; } int main(){ ll P,Q; cin >> P >> Q; vector v=Stern_Brocot(P,Q,Q); cout << v[0]+v[1]+v[2]+v[3] << endl; }