N = int(eval(input()))



A = []

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

    if 6**i > N:

        break

    else:

        A.append(6**i)

    if 9**i <= N:

        A.append(9**i)

A.sort(reverse=True)



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

for i in A:

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

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

print((money[N]))

n = int(eval(input()))

ans = float('INF')

hand = []

tmp = 1

while tmp * 6 <= n:

    tmp *= 6

    hand.append(tmp)

tmp = 1

while tmp * 9 <= n:

    tmp *= 9

    hand.append(tmp)

 

hand.sort()

hand = hand[::-1]

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

for v in hand:

    for i in range(n+1-v):

        if dp[i] + 1 < dp[i+v]:

            dp[i+v] = dp[i] + 1

print((dp[n]))

N = int(eval(input()))
A = []
for i in range(1, N + 1):
    if 6**i > N:
        break
    else:
        A.append(6**i)
    if 9**i <= N:
        A.append(9**i)
A.sort(reverse=True)
money = [i for i in range(N + 1)]
for i in A:
    for j in range(i, N + 1):
        money[j] = min(money[j], money[j - i] + 1)
print((money[N]))

n = int(eval(input()))
ans = float("INF")
hand = []
tmp = 1
while tmp * 6 <= n:
    tmp *= 6
    hand.append(tmp)
tmp = 1
while tmp * 9 <= n:
    tmp *= 9
    hand.append(tmp)
hand.sort()
hand = hand[::-1]
dp = [i for i in range(n + 1)]
for v in hand:
    for i in range(n + 1 - v):
        if dp[i] + 1 < dp[i + v]:
            dp[i + v] = dp[i] + 1
print((dp[n]))

-N = int(eval(input())) -A = [] -for i in range(1, N + 1): -    if 6**i > N: -        break -    else: -        A.append(6**i) -    if 9**i <= N: -        A.append(9**i) -A.sort(reverse=True) -money = [i for i in range(N + 1)] -for i in A: -    for j in range(i, N + 1): -        money[j] = min(money[j], money[j - i] + 1) -print((money[N])) +n = int(eval(input())) +ans = float("INF") +hand = [] +tmp = 1 +while tmp * 6 <= n: +    tmp *= 6 +    hand.append(tmp) +tmp = 1 +while tmp * 9 <= n: +    tmp *= 9 +    hand.append(tmp) +hand.sort() +hand = hand[::-1] +dp = [i for i in range(n + 1)] +for v in hand: +    for i in range(n + 1 - v): +        if dp[i] + 1 < dp[i + v]: +            dp[i + v] = dp[i] + 1 +print((dp[n]))