max_n = 100000



N = int(eval(input()))



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

dp[0] = 0

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

    for pow6 in range(n+1):

        if 6 ** pow6 <= n:

            dp[n] = min(dp[n], dp[n - 6 ** pow6] + 1)

        else:

            break



    for pow9 in range(n+1):

        if 9 ** pow9 <= n:

            dp[n] = min(dp[n], dp[n - 9 ** pow9] + 1)

        else:

            break

print((dp[N]))

N = int(eval(input()))



one = [1]

six_multi = [6, 6**2, 6**3, 6**4, 6**5, 6**6]

nine_multi = [9, 9**2, 9**3, 9**4, 9**5]

deposit = one + six_multi + nine_multi



dp = [10**10 for _ in range(N+1)] 

dp[0] = 0



for i in range(0, N):

    for d in range(len(deposit)):

        if i+deposit[d] <= N:

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



print((dp[N]))



max_n = 100000
N = int(eval(input()))
dp = [N for _ in range(N + 1)]
dp[0] = 0
for n in range(1, N + 1):
    for pow6 in range(n + 1):
        if 6**pow6 <= n:
            dp[n] = min(dp[n], dp[n - 6**pow6] + 1)
        else:
            break
    for pow9 in range(n + 1):
        if 9**pow9 <= n:
            dp[n] = min(dp[n], dp[n - 9**pow9] + 1)
        else:
            break
print((dp[N]))

N = int(eval(input()))
one = [1]
six_multi = [6, 6**2, 6**3, 6**4, 6**5, 6**6]
nine_multi = [9, 9**2, 9**3, 9**4, 9**5]
deposit = one + six_multi + nine_multi
dp = [10**10 for _ in range(N + 1)]
dp[0] = 0
for i in range(0, N):
    for d in range(len(deposit)):
        if i + deposit[d] <= N:
            dp[i + deposit[d]] = min(dp[i + deposit[d]], dp[i] + 1)
print((dp[N]))

-max_n = 100000 -dp = [N for _ in range(N + 1)] +one = [1] +six_multi = [6, 6**2, 6**3, 6**4, 6**5, 6**6] +nine_multi = [9, 9**2, 9**3, 9**4, 9**5] +deposit = one + six_multi + nine_multi +dp = [10**10 for _ in range(N + 1)] -for n in range(1, N + 1): -    for pow6 in range(n + 1): -        if 6**pow6 <= n: -            dp[n] = min(dp[n], dp[n - 6**pow6] + 1) -        else: -            break -    for pow9 in range(n + 1): -        if 9**pow9 <= n: -            dp[n] = min(dp[n], dp[n - 9**pow9] + 1) -        else: -            break +for i in range(0, N): +    for d in range(len(deposit)): +        if i + deposit[d] <= N: +            dp[i + deposit[d]] = min(dp[i + deposit[d]], dp[i] + 1)