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