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)

N = int(eval(input()))



def calc(x, base):

    r = 0

    while x:

        x, m = divmod(x, base)

        r += m

    return r



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



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)

N = int(eval(input()))


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


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

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