#include<bits/stdc++.h>
using namespace std;
const int ans[3]={1,2,3};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long x,k;
cin>>x>>k;
while(x!=1)
{
x++;
if(x%3==0)x/=3;
k--;
if(k==0)break;
}
if(k==0)cout<<x<<"\n";
else cout<<ans[k%3+1]<<"\n";
return 0;
}