#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main(){
int a,b;
while(cin>>a>>b){
vector<bool> steps;
while(b!=1){
if(a>b){
a-=b;
steps.push_back(true);
}
else{
swap(a,b);
steps.push_back(false);
}
}
int ans = pow(2,a-1);
for(int i = steps.size()-1 ; i>=0 ; i--){
if(steps[i]){
ans*=2;
}
else{
ans+=1;
}
}
cout<<ans<<endl;
}
return 0;
}