import sys

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

N = I()

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

dp[0] = 0

for i in range(N+1):

    if i+1<=N:

        dp[i+1] = min(dp[i+1],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 sys

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

N = I()

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

dp[0] = 0

for i in range(N+1):

    if i-1>=0:

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

    num6,num9 = 6,9

    while i-num6>=0:

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

        num6 *= 6

    while i-num9>=0:

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

        num9 *= 9

print((dp[-1]))

import sys


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


N = I()
dp = [float("INF")] * (N + 1)  # 1_indexed
dp[0] = 0
for i in range(N + 1):
    if i + 1 <= N:
        dp[i + 1] = min(dp[i + 1], 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 sys


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


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

-    if i + 1 <= N: -        dp[i + 1] = min(dp[i + 1], dp[i + 1], dp[i] + 1) +    if i - 1 >= 0: +        dp[i] = min(dp[i], dp[i - 1] + 1) -    while i + num6 <= N: -        dp[i + num6] = min(dp[i + num6], dp[i] + 1) +    while i - num6 >= 0: +        dp[i] = min(dp[i], dp[i - num6] + 1) -    while i + num9 <= N: -        dp[i + num9] = min(dp[i + num9], dp[i] + 1) +    while i - num9 >= 0: +        dp[i] = min(dp[i], dp[i - num9] + 1)