N = int(eval(input()))

dp = [N + 1 for i in range(N+1)]

dp[0] = 0

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

    j = 0

    while i - pow(6,j) >= 0:

        if dp[i-pow(6,j)] + 1 < dp[i]:

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

        j += 1

    j = 0

    while i - pow(9,j) >= 0:

        if dp[i-pow(9,j)] + 1 < dp[i]:

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

        j += 1

print((dp[N]))
inf = float('inf')



N = int(eval(input()))



dp = [inf] * (N+1)

dp[0] = 0



for i in range(N):

    if i+1 < N+1:

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

    j = 6

    while i+j < N+1:

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

        j *= 6

    j = 9

    while i+j < N+1:

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

        j *= 9

    

ans = dp[N]

print(ans)
N = int(eval(input()))
dp = [N + 1 for i in range(N + 1)]
dp[0] = 0
for i in range(1, N + 1):
    j = 0
    while i - pow(6, j) >= 0:
        if dp[i - pow(6, j)] + 1 < dp[i]:
            dp[i] = dp[i - pow(6, j)] + 1
        j += 1
    j = 0
    while i - pow(9, j) >= 0:
        if dp[i - pow(9, j)] + 1 < dp[i]:
            dp[i] = dp[i - pow(9, j)] + 1
        j += 1
print((dp[N]))

inf = float("inf")
N = int(eval(input()))
dp = [inf] * (N + 1)
dp[0] = 0
for i in range(N):
    if i + 1 < N + 1:
        dp[i + 1] = min(dp[i + 1], dp[i] + 1)
    j = 6
    while i + j < N + 1:
        dp[i + j] = min(dp[i + j], dp[i] + 1)
        j *= 6
    j = 9
    while i + j < N + 1:
        dp[i + j] = min(dp[i + j], dp[i] + 1)
        j *= 9
ans = dp[N]
print(ans)

+inf = float("inf") -dp = [N + 1 for i in range(N + 1)] +dp = [inf] * (N + 1) -for i in range(1, N + 1): -    j = 0 -    while i - pow(6, j) >= 0: -        if dp[i - pow(6, j)] + 1 < dp[i]: -            dp[i] = dp[i - pow(6, j)] + 1 -        j += 1 -    j = 0 -    while i - pow(9, j) >= 0: -        if dp[i - pow(9, j)] + 1 < dp[i]: -            dp[i] = dp[i - pow(9, j)] + 1 -        j += 1 -print((dp[N])) +for i in range(N): +    if i + 1 < N + 1: +        dp[i + 1] = min(dp[i + 1], dp[i] + 1) +    j = 6 +    while i + j < N + 1: +        dp[i + j] = min(dp[i + j], dp[i] + 1) +        j *= 6 +    j = 9 +    while i + j < N + 1: +        dp[i + j] = min(dp[i + j], dp[i] + 1) +        j *= 9 +ans = dp[N] +print(ans)