#define qwe if(x % 3 == 0){x /= 3;}
using namespace std;
long long x, k;
int main()
{
cin >> x >> k;
while(1)
{
int p = 3 - (x % 3);
if(k - p >= 0)
{
x = x / 3 + 1;
k -= p;
if(k == 0)
{
cout << x;
return 0;
}
}
else
{
if(k == 0)
{
cout << x;
return 0;
}
else if(k == 1)
{
cout << x + 1;
return 0;
}
else if(k == 2)
{
if(x % 3 == 0)
{
x /= 3;
}
else
{
x ++;
}
if(x % 3 == 0)
{
x /= 3;
}
else
{
x ++;
}
cout << x;
return 0;
}
else
{
if(x % 3 == 0)
{
x /= 3;
}
else
{
x ++;
}
if(x % 3 == 0)
{
x /= 3;
}
else
{
x ++;
}
if(x % 3 == 0)
{
x /= 3;
}
else
{
x ++;
}
cout << x;
return 0;
}
}
}
return 0;
}