N = int(eval(input()))



answer = N

for i in range(N+1):

    count = 0

    t = i

    # i円を6円で払い、残りを9円で払う

    while t:

        count += t%6

        t //= 6

    t = N-i

    while t:

        count += t%9

        t //= 9

    answer = min(answer, count)

print(answer)



# coding: utf-8

import sys



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



# dp

N = ir()

INF = 10 ** 8

dp = [INF] * (N+1)

dp[0] = 0

for i in range(5, 0, -1):

    num = 9 ** i

    for j in range(N):

        if j+num <= N:

            dp[j+num] = min(dp[j+num], dp[j]+1)



for i in range(6, 0, -1):

    num = 6 ** i

    for j in range(N):

        if j+num <= N:

            dp[j+num] = min(dp[j+num], dp[j]+1)



num = 1

for j in range(N):

    dp[j+num] = min(dp[j+num], dp[j]+1)



answer = dp[N]

print(answer)

N = int(eval(input()))
answer = N
for i in range(N + 1):
    count = 0
    t = i
    # i円を6円で払い、残りを9円で払う
    while t:
        count += t % 6
        t //= 6
    t = N - i
    while t:
        count += t % 9
        t //= 9
    answer = min(answer, count)
print(answer)

# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# dp
N = ir()
INF = 10**8
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(5, 0, -1):
    num = 9**i
    for j in range(N):
        if j + num <= N:
            dp[j + num] = min(dp[j + num], dp[j] + 1)
for i in range(6, 0, -1):
    num = 6**i
    for j in range(N):
        if j + num <= N:
            dp[j + num] = min(dp[j + num], dp[j] + 1)
num = 1
for j in range(N):
    dp[j + num] = min(dp[j + num], dp[j] + 1)
answer = dp[N]
print(answer)

-N = int(eval(input())) -answer = N -for i in range(N + 1): -    count = 0 -    t = i -    # i円を6円で払い、残りを9円で払う -    while t: -        count += t % 6 -        t //= 6 -    t = N - i -    while t: -        count += t % 9 -        t //= 9 -    answer = min(answer, count) +# coding: utf-8 +import sys + +sr = lambda: sys.stdin.readline().rstrip() +ir = lambda: int(sr()) +lr = lambda: list(map(int, sr().split())) +# dp +N = ir() +INF = 10**8 +dp = [INF] * (N + 1) +dp[0] = 0 +for i in range(5, 0, -1): +    num = 9**i +    for j in range(N): +        if j + num <= N: +            dp[j + num] = min(dp[j + num], dp[j] + 1) +for i in range(6, 0, -1): +    num = 6**i +    for j in range(N): +        if j + num <= N: +            dp[j + num] = min(dp[j + num], dp[j] + 1) +num = 1 +for j in range(N): +    dp[j + num] = min(dp[j + num], dp[j] + 1) +answer = dp[N]