from heapq import *



N = int(eval(input()))



result = 100000



ns = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]



h = []



xs = {}



heappush(h, (0, 0))



while 1:

    r, m = heappop(h)

    if m == N:

        result = r

        break

    for n in ns:

        s = r + 1

        x = m + n

        if x <= N and not x in xs:

            xs[x] = s

            heappush(h, (s, x))



print(result)



N = int(eval(input()))



result = 100000000000



def calc(x, base):

    r = 0

    while x:

        x, m = divmod(x, base)

        r += m

    return r



for i in range(N + 1):

    result = min(result, calc(i, 6) + calc(N - i, 9))



print(result)

from heapq import *

N = int(eval(input()))
result = 100000
ns = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
h = []
xs = {}
heappush(h, (0, 0))
while 1:
    r, m = heappop(h)
    if m == N:
        result = r
        break
    for n in ns:
        s = r + 1
        x = m + n
        if x <= N and not x in xs:
            xs[x] = s
            heappush(h, (s, x))
print(result)

N = int(eval(input()))
result = 100000000000


def calc(x, base):
    r = 0
    while x:
        x, m = divmod(x, base)
        r += m
    return r


for i in range(N + 1):
    result = min(result, calc(i, 6) + calc(N - i, 9))
print(result)

-from heapq import * +N = int(eval(input())) +result = 100000000000 -N = int(eval(input())) -result = 100000 -ns = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049] -h = [] -xs = {} -heappush(h, (0, 0)) -while 1: -    r, m = heappop(h) -    if m == N: -        result = r -        break -    for n in ns: -        s = r + 1 -        x = m + n -        if x <= N and not x in xs: -            xs[x] = s -            heappush(h, (s, x)) + +def calc(x, base): +    r = 0 +    while x: +        x, m = divmod(x, base) +        r += m +    return r + + +for i in range(N + 1): +    result = min(result, calc(i, 6) + calc(N - i, 9))