import numpy  as np



N = int(eval(input()))





def make_array(shape, defval=0, dtype=int):

    res = np.zeros(shape, dtype)

    res[:] = defval

    return res.tolist()





memo = make_array((15, N + 1), N * 2)

memo[0][0] = 0



values = []



i = 1

while i <= N:

    values.append(i)

    i *= 6

i = 9

while i <= N:

    values.append(i)

    i *= 9



for i in range(len(values)):

    v = values[i]

    for w in range(N + 1):

        if v > w:

            memo[i + 1][w] = memo[i][w]

        else:

            memo[i + 1][w] = min(memo[i][w], memo[i + 1][w - v] + 1)

print((memo[len(values)][N]))
N = int(eval(input()))



# def make_array(shape, defval=0, dtype=int):

#     res = np.zeros(shape, dtype)

#     res[:] = defval

#     return res.tolist()





# memo = make_array((15, N + 1), N * 2)

memo = [[N * 2] * (N + 1) for _ in range(15)]

memo[0][0] = 0



values = []



i = 1

while i <= N:

    values.append(i)

    i *= 6

i = 9

while i <= N:

    values.append(i)

    i *= 9



for i in range(len(values)):

    v = values[i]

    for w in range(N + 1):

        if v > w:

            memo[i + 1][w] = memo[i][w]

        else:

            memo[i + 1][w] = min(memo[i][w], memo[i + 1][w - v] + 1)

print((memo[len(values)][N]))
import numpy as np

N = int(eval(input()))


def make_array(shape, defval=0, dtype=int):
    res = np.zeros(shape, dtype)
    res[:] = defval
    return res.tolist()


memo = make_array((15, N + 1), N * 2)
memo[0][0] = 0
values = []
i = 1
while i <= N:
    values.append(i)
    i *= 6
i = 9
while i <= N:
    values.append(i)
    i *= 9
for i in range(len(values)):
    v = values[i]
    for w in range(N + 1):
        if v > w:
            memo[i + 1][w] = memo[i][w]
        else:
            memo[i + 1][w] = min(memo[i][w], memo[i + 1][w - v] + 1)
print((memo[len(values)][N]))

N = int(eval(input()))
# def make_array(shape, defval=0, dtype=int):
#     res = np.zeros(shape, dtype)
#     res[:] = defval
#     return res.tolist()
# memo = make_array((15, N + 1), N * 2)
memo = [[N * 2] * (N + 1) for _ in range(15)]
memo[0][0] = 0
values = []
i = 1
while i <= N:
    values.append(i)
    i *= 6
i = 9
while i <= N:
    values.append(i)
    i *= 9
for i in range(len(values)):
    v = values[i]
    for w in range(N + 1):
        if v > w:
            memo[i + 1][w] = memo[i][w]
        else:
            memo[i + 1][w] = min(memo[i][w], memo[i + 1][w - v] + 1)
print((memo[len(values)][N]))

-import numpy as np - - - -def make_array(shape, defval=0, dtype=int): -    res = np.zeros(shape, dtype) -    res[:] = defval -    return res.tolist() - - -memo = make_array((15, N + 1), N * 2) +# def make_array(shape, defval=0, dtype=int): +#     res = np.zeros(shape, dtype) +#     res[:] = defval +#     return res.tolist() +# memo = make_array((15, N + 1), N * 2) +memo = [[N * 2] * (N + 1) for _ in range(15)]