from bisect import bisect_right

N = int(eval(input()))

unit = [1]

for b in [6,9]:

    u = b

    while u <= N:

        unit.append(u)

        u *= b

unit.sort()

Nu = len(unit)



ans = N

state_pool = [(0, 0, Nu-1)]

while state_pool:

    n, i, pk = state_pool.pop()

    if N - n >= (ans-i)*unit[pk]:

        continue

    sk = bisect_right(unit, N-n, 0, pk+1)

    for k in range(sk):

        u = unit[k]

        c = n + u

        if c == N:

            if i + 1 < ans:

                ans = i + 1

        else:

            state_pool.append((c, i+1, k))

print(ans)
from bisect import bisect_right

N = int(eval(input()))

unit = [1]

for b in [6,9]:

    u = b

    while u <= N:

        unit.append(u)

        u *= b

unit.sort()

Nu = len(unit)



ans = N

state_pool = [(0, 0, 0, Nu-1)]

while state_pool:

    n, i, parent, pk = state_pool.pop()

    if N - n >= (ans-i)*unit[pk]:

        continue

    elif pk > 0 and i > 0:

        state_pool.append((parent+unit[pk-1], i, parent, pk-1))



    k = bisect_right(unit, N-n, 0, pk+1) - 1

    c = n + unit[k]

    if c == N:

        if i + 1 < ans:

            ans = i + 1

    else:

        state_pool.append((c, i+1, n, k))

print(ans)
from bisect import bisect_right

N = int(eval(input()))
unit = [1]
for b in [6, 9]:
    u = b
    while u <= N:
        unit.append(u)
        u *= b
unit.sort()
Nu = len(unit)
ans = N
state_pool = [(0, 0, Nu - 1)]
while state_pool:
    n, i, pk = state_pool.pop()
    if N - n >= (ans - i) * unit[pk]:
        continue
    sk = bisect_right(unit, N - n, 0, pk + 1)
    for k in range(sk):
        u = unit[k]
        c = n + u
        if c == N:
            if i + 1 < ans:
                ans = i + 1
        else:
            state_pool.append((c, i + 1, k))
print(ans)

from bisect import bisect_right

N = int(eval(input()))
unit = [1]
for b in [6, 9]:
    u = b
    while u <= N:
        unit.append(u)
        u *= b
unit.sort()
Nu = len(unit)
ans = N
state_pool = [(0, 0, 0, Nu - 1)]
while state_pool:
    n, i, parent, pk = state_pool.pop()
    if N - n >= (ans - i) * unit[pk]:
        continue
    elif pk > 0 and i > 0:
        state_pool.append((parent + unit[pk - 1], i, parent, pk - 1))
    k = bisect_right(unit, N - n, 0, pk + 1) - 1
    c = n + unit[k]
    if c == N:
        if i + 1 < ans:
            ans = i + 1
    else:
        state_pool.append((c, i + 1, n, k))
print(ans)

-state_pool = [(0, 0, Nu - 1)] +state_pool = [(0, 0, 0, Nu - 1)] -    n, i, pk = state_pool.pop() +    n, i, parent, pk = state_pool.pop() -    sk = bisect_right(unit, N - n, 0, pk + 1) -    for k in range(sk): -        u = unit[k] -        c = n + u -        if c == N: -            if i + 1 < ans: -                ans = i + 1 -        else: -            state_pool.append((c, i + 1, k)) +    elif pk > 0 and i > 0: +        state_pool.append((parent + unit[pk - 1], i, parent, pk - 1)) +    k = bisect_right(unit, N - n, 0, pk + 1) - 1 +    c = n + unit[k] +    if c == N: +        if i + 1 < ans: +            ans = i + 1 +    else: +        state_pool.append((c, i + 1, n, k))