#include<bits/stdc++.h> using namespace std; long long x,k; int main() { cin>>x>>k; int a=x%3; for(int i=1;i<=k;i++) { x+=1; if(x%3==0) x/=3; if(x==0) { x++; k--; } if(x<3) { cout<<(k-i)%2+x; return 0; } } cout<<x; return 0; }