N = int(eval(input()))

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

dp[1] = 1

roku = 0

kyu = 0

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

    if i == 6**(roku+1):

        roku += 1

    if i == 9**(kyu+1):

        kyu += 1

    kouho = [1]

    for j in range(roku):

        kouho.append(6**(j+1))

    for j in range(kyu):

        kouho.append(9**(j+1))

    size = len(kouho)



    small = 1000000

    for j in range(size):

        if dp[i - kouho[j]] + 1 < small:

            small = dp[i - kouho[j]] + 1

    dp[i] = small

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

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

dp[1] = 1

kazu = [1]

i = 6

while i <= N:

    kazu.append(i)

    i*=6

i = 9

while i <= N:

    kazu.append(i)

    i*=9

kazu.sort()

kazulazor = len(kazu)

temp = [1]

lentemp = 1

index = 1

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

    kouho = []

    if index < kazulazor and i == kazu[index]:

        temp.append(i)

        index += 1

        lentemp += 1

    for j in range(lentemp):

        kouho.append(dp[i-temp[j]]+1)

    dp[i] = min(kouho)

print((str(dp[N])))
N = int(eval(input()))
dp = [0 for i in range(N + 1)]
dp[1] = 1
roku = 0
kyu = 0
for i in range(2, N + 1):
    if i == 6 ** (roku + 1):
        roku += 1
    if i == 9 ** (kyu + 1):
        kyu += 1
    kouho = [1]
    for j in range(roku):
        kouho.append(6 ** (j + 1))
    for j in range(kyu):
        kouho.append(9 ** (j + 1))
    size = len(kouho)
    small = 1000000
    for j in range(size):
        if dp[i - kouho[j]] + 1 < small:
            small = dp[i - kouho[j]] + 1
    dp[i] = small
print((str(dp[N])))

N = int(eval(input()))
dp = [0 for i in range(N + 1)]
dp[1] = 1
kazu = [1]
i = 6
while i <= N:
    kazu.append(i)
    i *= 6
i = 9
while i <= N:
    kazu.append(i)
    i *= 9
kazu.sort()
kazulazor = len(kazu)
temp = [1]
lentemp = 1
index = 1
for i in range(2, N + 1):
    kouho = []
    if index < kazulazor and i == kazu[index]:
        temp.append(i)
        index += 1
        lentemp += 1
    for j in range(lentemp):
        kouho.append(dp[i - temp[j]] + 1)
    dp[i] = min(kouho)
print((str(dp[N])))

-roku = 0 -kyu = 0 +kazu = [1] +i = 6 +while i <= N: +    kazu.append(i) +    i *= 6 +i = 9 +while i <= N: +    kazu.append(i) +    i *= 9 +kazu.sort() +kazulazor = len(kazu) +temp = [1] +lentemp = 1 +index = 1 -    if i == 6 ** (roku + 1): -        roku += 1 -    if i == 9 ** (kyu + 1): -        kyu += 1 -    kouho = [1] -    for j in range(roku): -        kouho.append(6 ** (j + 1)) -    for j in range(kyu): -        kouho.append(9 ** (j + 1)) -    size = len(kouho) -    small = 1000000 -    for j in range(size): -        if dp[i - kouho[j]] + 1 < small: -            small = dp[i - kouho[j]] + 1 -    dp[i] = small +    kouho = [] +    if index < kazulazor and i == kazu[index]: +        temp.append(i) +        index += 1 +        lentemp += 1 +    for j in range(lentemp): +        kouho.append(dp[i - temp[j]] + 1) +    dp[i] = min(kouho)