import itertools,sys

def I(): return int(sys.stdin.readline().rstrip())

N = I()

if N<=5:

    print(N)

    exit()

dp = [float('INF')]*(N+1)

dp[0] = 0

num6,num9 = 6,9

d6d9 = set()

while num6<=N:

    d6d9.add(num6)

    num6 *= 6

while num9<=N:

    d6d9.add(num9)

    num9 *= 9

for i,x in itertools.product(list(range(N+1)),d6d9):

    if i+x>=N+1:

        continue

    dp[i+1] = min(dp[i+1],dp[i]+1)

    dp[i+x] = min(dp[i+x],dp[i]+1)

print((dp[-1]))

import itertools,sys

def I(): return int(sys.stdin.readline().rstrip())

N = I()

dp = [float('INF')]*(N+1)

dp[0] = 0

for i in range(N+1):

    if i+1<=N:

        dp[i+1] = min(dp[i+1],dp[i]+1)

    num6,num9 = 6,9

    while i+num6<=N:

        dp[i+num6] = min(dp[i+num6],dp[i]+1)

        num6 *= 6

    while i+num9<=N:

        dp[i+num9] = min(dp[i+num9],dp[i]+1)

        num9 *=9

print((dp[-1]))

import itertools, sys


def I():
    return int(sys.stdin.readline().rstrip())


N = I()
if N <= 5:
    print(N)
    exit()
dp = [float("INF")] * (N + 1)
dp[0] = 0
num6, num9 = 6, 9
d6d9 = set()
while num6 <= N:
    d6d9.add(num6)
    num6 *= 6
while num9 <= N:
    d6d9.add(num9)
    num9 *= 9
for i, x in itertools.product(list(range(N + 1)), d6d9):
    if i + x >= N + 1:
        continue
    dp[i + 1] = min(dp[i + 1], dp[i] + 1)
    dp[i + x] = min(dp[i + x], dp[i] + 1)
print((dp[-1]))

import itertools, sys


def I():
    return int(sys.stdin.readline().rstrip())


N = I()
dp = [float("INF")] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    if i + 1 <= N:
        dp[i + 1] = min(dp[i + 1], dp[i] + 1)
    num6, num9 = 6, 9
    while i + num6 <= N:
        dp[i + num6] = min(dp[i + num6], dp[i] + 1)
        num6 *= 6
    while i + num9 <= N:
        dp[i + num9] = min(dp[i + num9], dp[i] + 1)
        num9 *= 9
print((dp[-1]))

-if N <= 5: -    print(N) -    exit() -num6, num9 = 6, 9 -d6d9 = set() -while num6 <= N: -    d6d9.add(num6) -    num6 *= 6 -while num9 <= N: -    d6d9.add(num9) -    num9 *= 9 -for i, x in itertools.product(list(range(N + 1)), d6d9): -    if i + x >= N + 1: -        continue -    dp[i + 1] = min(dp[i + 1], dp[i] + 1) -    dp[i + x] = min(dp[i + x], dp[i] + 1) +for i in range(N + 1): +    if i + 1 <= N: +        dp[i + 1] = min(dp[i + 1], dp[i] + 1) +    num6, num9 = 6, 9 +    while i + num6 <= N: +        dp[i + num6] = min(dp[i + num6], dp[i] + 1) +        num6 *= 6 +    while i + num9 <= N: +        dp[i + num9] = min(dp[i + num9], dp[i] + 1) +        num9 *= 9