from math import log

N = int(eval(input()))



hiki9 = []

hiki6 = []

n = int(log(N) / log(9) + 1)  # log9の計算に誤差があるので一つ多めに取っておく

for i in range(n+1):

    hiki9.append(9**i)

n = int(log(N) / log(6) + 1)

for i in range(n+1):

    hiki6.append(6**i)

hiki9.sort(reverse=True)

hiki6.sort(reverse=True)

x = []

for i in range(N+1):

    n9 = i

    tmpx = 0

    for h in hiki9:

        while h <= n9:

            n9 -= h

            tmpx += 1

    n6 = N - i

    for h in hiki6:

        while h <= n6:

            n6 -= h

            tmpx += 1

    x.append(tmpx)

x.sort()

print((x[0]))
N = int(eval(input()))

koho = [1]

i=1

while True:

    if i > 1e6:

        break

    i *= 6

    koho.append(i)

i=1

while True:

    if i > 1e6:

        break

    i *= 9

    koho.append(i)



dp = [0] * (N+1)

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

    tmp = []

    for k in koho:

        if k > i:

            continue

        tmp.append(dp[i - k] + 1)

    dp[i] = min(tmp)

print((dp[N]))
from math import log

N = int(eval(input()))
hiki9 = []
hiki6 = []
n = int(log(N) / log(9) + 1)  # log9の計算に誤差があるので一つ多めに取っておく
for i in range(n + 1):
    hiki9.append(9**i)
n = int(log(N) / log(6) + 1)
for i in range(n + 1):
    hiki6.append(6**i)
hiki9.sort(reverse=True)
hiki6.sort(reverse=True)
x = []
for i in range(N + 1):
    n9 = i
    tmpx = 0
    for h in hiki9:
        while h <= n9:
            n9 -= h
            tmpx += 1
    n6 = N - i
    for h in hiki6:
        while h <= n6:
            n6 -= h
            tmpx += 1
    x.append(tmpx)
x.sort()
print((x[0]))

N = int(eval(input()))
koho = [1]
i = 1
while True:
    if i > 1e6:
        break
    i *= 6
    koho.append(i)
i = 1
while True:
    if i > 1e6:
        break
    i *= 9
    koho.append(i)
dp = [0] * (N + 1)
for i in range(1, N + 1):
    tmp = []
    for k in koho:
        if k > i:
            continue
        tmp.append(dp[i - k] + 1)
    dp[i] = min(tmp)
print((dp[N]))

-from math import log - -hiki9 = [] -hiki6 = [] -n = int(log(N) / log(9) + 1)  # log9の計算に誤差があるので一つ多めに取っておく -for i in range(n + 1): -    hiki9.append(9**i) -n = int(log(N) / log(6) + 1) -for i in range(n + 1): -    hiki6.append(6**i) -hiki9.sort(reverse=True) -hiki6.sort(reverse=True) -x = [] -for i in range(N + 1): -    n9 = i -    tmpx = 0 -    for h in hiki9: -        while h <= n9: -            n9 -= h -            tmpx += 1 -    n6 = N - i -    for h in hiki6: -        while h <= n6: -            n6 -= h -            tmpx += 1 -    x.append(tmpx) -x.sort() -print((x[0])) +koho = [1] +i = 1 +while True: +    if i > 1e6: +        break +    i *= 6 +    koho.append(i) +i = 1 +while True: +    if i > 1e6: +        break +    i *= 9 +    koho.append(i) +dp = [0] * (N + 1) +for i in range(1, N + 1): +    tmp = [] +    for k in koho: +        if k > i: +            continue +        tmp.append(dp[i - k] + 1) +    dp[i] = min(tmp) +print((dp[N]))