N = int(eval(input()))

A = [1, 6, 6**2, 6**3, 6**4, 6**5, 6**6, 9, 9**2, 9**3, 9**4, 9**5]

dp = [1000000] * 300000

dp[0] = 0



for i in range(N):

    for a in A:

        d = dp[i] + 1

        dp[i + a] = min(dp[i + a], d)



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

INF = 10 ** 9

dp = [INF] * (N + 1)

dp[0] = 0

M = set()

M.add(1)

i = 1

while True:

    if 6 ** i > N:

        break

    M.add(6 ** i)

    i += 1

i = 1

while True:

    if 9 ** i > N:

        break

    M.add(9 ** i)

    i += 1

for i in range(N):

    for m in M:

        if i + m > N:

            continue

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

print((dp[N]))
N = int(eval(input()))
A = [1, 6, 6**2, 6**3, 6**4, 6**5, 6**6, 9, 9**2, 9**3, 9**4, 9**5]
dp = [1000000] * 300000
dp[0] = 0
for i in range(N):
    for a in A:
        d = dp[i] + 1
        dp[i + a] = min(dp[i + a], d)
print((dp[N]))

N = int(eval(input()))
INF = 10**9
dp = [INF] * (N + 1)
dp[0] = 0
M = set()
M.add(1)
i = 1
while True:
    if 6**i > N:
        break
    M.add(6**i)
    i += 1
i = 1
while True:
    if 9**i > N:
        break
    M.add(9**i)
    i += 1
for i in range(N):
    for m in M:
        if i + m > N:
            continue
        dp[i + m] = min(dp[i + m], dp[i] + 1)
print((dp[N]))

-A = [1, 6, 6**2, 6**3, 6**4, 6**5, 6**6, 9, 9**2, 9**3, 9**4, 9**5] -dp = [1000000] * 300000 +INF = 10**9 +dp = [INF] * (N + 1) +M = set() +M.add(1) +i = 1 +while True: +    if 6**i > N: +        break +    M.add(6**i) +    i += 1 +i = 1 +while True: +    if 9**i > N: +        break +    M.add(9**i) +    i += 1 -    for a in A: -        d = dp[i] + 1 -        dp[i + a] = min(dp[i + a], d) +    for m in M: +        if i + m > N: +            continue +        dp[i + m] = min(dp[i + m], dp[i] + 1)