# DP(配るDP)

INF = float('inf')



N = int(eval(input()))



c = [1]

t = 6

while t <= N:

    c.append(t)

    t *= 6

t = 9

while t <= N:

    c.append(t)

    t *= 9



dp = [INF] * (N + 1)

dp[0] = 0

for i in range(N):

    for j in c:

        if i + j <= N:

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

        if i + j == N:

            break

print((dp[N]))

# DP(配るDP)

INF = float('inf')



N = int(eval(input()))



c = [1]

t = 6

while t <= N:

    c.append(t)

    t *= 6

t = 9

while t <= N:

    c.append(t)

    t *= 9



dp = [INF] * (N + 1)

dp[0] = 0

for i in range(N):

    for j in c:

        t = i + j

        if t <= N and dp[t] > dp[i] + 1:

            dp[t] = dp[i] + 1

        if t == N:

            break

print((dp[N]))

# DP(配るDP)
INF = float("inf")
N = int(eval(input()))
c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    for j in c:
        if i + j <= N:
            dp[i + j] = min(dp[i + j], dp[i] + 1)
        if i + j == N:
            break
print((dp[N]))

# DP(配るDP)
INF = float("inf")
N = int(eval(input()))
c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    for j in c:
        t = i + j
        if t <= N and dp[t] > dp[i] + 1:
            dp[t] = dp[i] + 1
        if t == N:
            break
print((dp[N]))

-        if i + j <= N: -            dp[i + j] = min(dp[i + j], dp[i] + 1) -        if i + j == N: +        t = i + j +        if t <= N and dp[t] > dp[i] + 1: +            dp[t] = dp[i] + 1 +        if t == N: