n=int(eval(input()))

dp=[float("INF")]*(n+1)

dp[0]=0

temp=6

cnt=0

while temp<=n:

  cnt+=1

  dp[temp]=1

  temp*=6

l=[0,0]

l[0]=cnt

temp=9

cnt=0

while temp<=n:

  cnt+=1

  dp[temp]=1

  temp*=9

l[1]=cnt

for j,v in zip(l,[6,9]):

  for i in range(j+1):

    h=pow(v,i)

    for d in range(h,n+1):

      dp[d]=min(dp[d],dp[d-h]+1)

print((dp[-1]))
n=int(eval(input()))

ans=float("INF")

for i in range(n+1):

  d=0

  temp=i

  while (temp>0):

    d+=temp%6

    temp//=6

  temp=n-i

  while(temp>0):

    d+=temp%9

    temp//=9

  ans=min(d,ans)

print(ans)
n = int(eval(input()))
dp = [float("INF")] * (n + 1)
dp[0] = 0
temp = 6
cnt = 0
while temp <= n:
    cnt += 1
    dp[temp] = 1
    temp *= 6
l = [0, 0]
l[0] = cnt
temp = 9
cnt = 0
while temp <= n:
    cnt += 1
    dp[temp] = 1
    temp *= 9
l[1] = cnt
for j, v in zip(l, [6, 9]):
    for i in range(j + 1):
        h = pow(v, i)
        for d in range(h, n + 1):
            dp[d] = min(dp[d], dp[d - h] + 1)
print((dp[-1]))

n = int(eval(input()))
ans = float("INF")
for i in range(n + 1):
    d = 0
    temp = i
    while temp > 0:
        d += temp % 6
        temp //= 6
    temp = n - i
    while temp > 0:
        d += temp % 9
        temp //= 9
    ans = min(d, ans)
print(ans)

-dp = [float("INF")] * (n + 1) -dp[0] = 0 -temp = 6 -cnt = 0 -while temp <= n: -    cnt += 1 -    dp[temp] = 1 -    temp *= 6 -l = [0, 0] -l[0] = cnt -temp = 9 -cnt = 0 -while temp <= n: -    cnt += 1 -    dp[temp] = 1 -    temp *= 9 -l[1] = cnt -for j, v in zip(l, [6, 9]): -    for i in range(j + 1): -        h = pow(v, i) -        for d in range(h, n + 1): -            dp[d] = min(dp[d], dp[d - h] + 1) -print((dp[-1])) +ans = float("INF") +for i in range(n + 1): +    d = 0 +    temp = i +    while temp > 0: +        d += temp % 6 +        temp //= 6 +    temp = n - i +    while temp > 0: +        d += temp % 9 +        temp //= 9 +    ans = min(d, ans) +print(ans)