# -*- coding: utf-8 -*-



N = int(eval(input()))



A = [1]

i = 1

while 6 ** i <= 100000:

    A.append(6 ** i)

    i += 1

j = 1

while 9 ** j <= 100000:

    A.append(9 ** j)

    j += 1

A.sort()



table = [1000000 for _ in range(N + 1)]

table[0] = 0



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

    a = 0

    while 6 ** a <= i:

        table[i] = min(table[i], table[i - 6 ** a] + 1)

        a += 1

    b = 0

    while 9 ** b <= i:

        table[i] = min(table[i], table[i - 9 ** b] + 1)

        b += 1



print((table[N]))

# -*- coding: utf-8 -*-



N = int(eval(input()))



A = [1]

i = 1

while 6 ** i <= 100000:

    A.append(6 ** i)

    i += 1

j = 1

while 9 ** j <= 100000:

    A.append(9 ** j)

    j += 1

A.sort()  # 引き出せる金額のリスト



table = [1000000 for _ in range(N + 1)]  # i円を引き出す最小の操作回数をメモ、参照するのは常に前の値なのでループを回せる

table[0] = 0



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

    a = 0

    while 6 ** a <= i:

        table[i] = min(table[i], table[i - 6 ** a] + 1)  # 1円,6円,36円,...を使って操作回数を減らす

        a += 1

    b = 0

    while 9 ** b <= i:

        table[i] = min(table[i], table[i - 9 ** b] + 1)  # 1円,9円,81円,...を使って操作回数を減らす

        b += 1



print((table[N]))

# -*- coding: utf-8 -*-
N = int(eval(input()))
A = [1]
i = 1
while 6**i <= 100000:
    A.append(6**i)
    i += 1
j = 1
while 9**j <= 100000:
    A.append(9**j)
    j += 1
A.sort()
table = [1000000 for _ in range(N + 1)]
table[0] = 0
for i in range(1, N + 1):
    a = 0
    while 6**a <= i:
        table[i] = min(table[i], table[i - 6**a] + 1)
        a += 1
    b = 0
    while 9**b <= i:
        table[i] = min(table[i], table[i - 9**b] + 1)
        b += 1
print((table[N]))

# -*- coding: utf-8 -*-
N = int(eval(input()))
A = [1]
i = 1
while 6**i <= 100000:
    A.append(6**i)
    i += 1
j = 1
while 9**j <= 100000:
    A.append(9**j)
    j += 1
A.sort()  # 引き出せる金額のリスト
table = [1000000 for _ in range(N + 1)]  # i円を引き出す最小の操作回数をメモ、参照するのは常に前の値なのでループを回せる
table[0] = 0
for i in range(1, N + 1):
    a = 0
    while 6**a <= i:
        table[i] = min(table[i], table[i - 6**a] + 1)  # 1円,6円,36円,...を使って操作回数を減らす
        a += 1
    b = 0
    while 9**b <= i:
        table[i] = min(table[i], table[i - 9**b] + 1)  # 1円,9円,81円,...を使って操作回数を減らす
        b += 1
print((table[N]))

-A.sort() -table = [1000000 for _ in range(N + 1)] +A.sort()  # 引き出せる金額のリスト +table = [1000000 for _ in range(N + 1)]  # i円を引き出す最小の操作回数をメモ、参照するのは常に前の値なのでループを回せる -        table[i] = min(table[i], table[i - 6**a] + 1) +        table[i] = min(table[i], table[i - 6**a] + 1)  # 1円,6円,36円,...を使って操作回数を減らす -        table[i] = min(table[i], table[i - 9**b] + 1) +        table[i] = min(table[i], table[i - 9**b] + 1)  # 1円,9円,81円,...を使って操作回数を減らす