n=int(eval(input()))



ls=set([1])

i=1

while 6**i<=100000:

  ls.add(6**i)

  i+=1

i=1

while 9**i<=100000:

  ls.add(9**i)

  i+=1



dp = [float("inf") for i in range(n+2)]

dp[0] = 0

ls=list(ls)

#円の総合計

for i in range(n+2):

  #額面のコインの種類

  for j in range(len(ls)):

    if i+ls[j]<=n:

      dp[i+ls[j]] = min(dp[i]+1, dp[i+ls[j]-1]+1,dp[i+ls[j]])

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

ls=[1]



for i in range(1,7):

  ls.append(6**i)

  ls.append(9**i)

dp = [float('inf') for i in range(n+2)]

dp[0] = 0



#円の総合計

for i in range(1,n+1):

  for j in ls:

    if i-j>=0:

      dp[i] = min(dp[i-1]+1, dp[i-j]+1,dp[i])

    else:

      dp[i] = min(dp[i-1]+1,dp[i])

print((dp[n]))
n = int(eval(input()))
ls = set([1])
i = 1
while 6**i <= 100000:
    ls.add(6**i)
    i += 1
i = 1
while 9**i <= 100000:
    ls.add(9**i)
    i += 1
dp = [float("inf") for i in range(n + 2)]
dp[0] = 0
ls = list(ls)
# 円の総合計
for i in range(n + 2):
    # 額面のコインの種類
    for j in range(len(ls)):
        if i + ls[j] <= n:
            dp[i + ls[j]] = min(dp[i] + 1, dp[i + ls[j] - 1] + 1, dp[i + ls[j]])
print((dp[n]))

n = int(eval(input()))
ls = [1]
for i in range(1, 7):
    ls.append(6**i)
    ls.append(9**i)
dp = [float("inf") for i in range(n + 2)]
dp[0] = 0
# 円の総合計
for i in range(1, n + 1):
    for j in ls:
        if i - j >= 0:
            dp[i] = min(dp[i - 1] + 1, dp[i - j] + 1, dp[i])
        else:
            dp[i] = min(dp[i - 1] + 1, dp[i])
print((dp[n]))

-ls = set([1]) -i = 1 -while 6**i <= 100000: -    ls.add(6**i) -    i += 1 -i = 1 -while 9**i <= 100000: -    ls.add(9**i) -    i += 1 +ls = [1] +for i in range(1, 7): +    ls.append(6**i) +    ls.append(9**i) -ls = list(ls) -for i in range(n + 2): -    # 額面のコインの種類 -    for j in range(len(ls)): -        if i + ls[j] <= n: -            dp[i + ls[j]] = min(dp[i] + 1, dp[i + ls[j] - 1] + 1, dp[i + ls[j]]) +for i in range(1, n + 1): +    for j in ls: +        if i - j >= 0: +            dp[i] = min(dp[i - 1] + 1, dp[i - j] + 1, dp[i]) +        else: +            dp[i] = min(dp[i - 1] + 1, dp[i])