n = int(eval(input()))

c = [1]

for i in range(6):

    c.append(6**(i+1))

for i in range(5):

    c.append(9**(i+1))

INF = float("inf")

 

dp = [INF]*(n+1)

dp[0] = 0

 

for i in range(len(c)):

    for j in range(n+1):

        if j - c[i] < 0:

            continue

        else:

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

 

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

dp = [10 ** 18] * (n + 1)

dp[0] = 0



info = [[1, 1]]

for i in range(1, 10):

    info.append([6**i, 1])

for i in range(1, 10):

    info.append([9**i, 1])

    

for i in range(n):

    for w, v in info:

        if i + w >= n + 1:

            continue

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

print((dp[-1]))
n = int(eval(input()))
c = [1]
for i in range(6):
    c.append(6 ** (i + 1))
for i in range(5):
    c.append(9 ** (i + 1))
INF = float("inf")
dp = [INF] * (n + 1)
dp[0] = 0
for i in range(len(c)):
    for j in range(n + 1):
        if j - c[i] < 0:
            continue
        else:
            dp[j] = min(dp[j], dp[j - c[i]] + 1)
print((dp[n]))

n = int(eval(input()))
dp = [10**18] * (n + 1)
dp[0] = 0
info = [[1, 1]]
for i in range(1, 10):
    info.append([6**i, 1])
for i in range(1, 10):
    info.append([9**i, 1])
for i in range(n):
    for w, v in info:
        if i + w >= n + 1:
            continue
        dp[i + w] = min(dp[i] + 1, dp[i + w])
print((dp[-1]))

-c = [1] -for i in range(6): -    c.append(6 ** (i + 1)) -for i in range(5): -    c.append(9 ** (i + 1)) -INF = float("inf") -dp = [INF] * (n + 1) +dp = [10**18] * (n + 1) -for i in range(len(c)): -    for j in range(n + 1): -        if j - c[i] < 0: +info = [[1, 1]] +for i in range(1, 10): +    info.append([6**i, 1]) +for i in range(1, 10): +    info.append([9**i, 1]) +for i in range(n): +    for w, v in info: +        if i + w >= n + 1: -        else: -            dp[j] = min(dp[j], dp[j - c[i]] + 1) -print((dp[n])) +        dp[i + w] = min(dp[i] + 1, dp[i + w]) +print((dp[-1]))