# 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):

    t0 = dp[i] + 1

    for j in c:

        t1 = i + j

        if t1 <= N and dp[t1] > t0:

            dp[t1] = t0

        if t1 == 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):

    t0 = dp[i] + 1

    for j in c:

        t1 = i + j

        if t1 <= N and dp[t1] > t0:

            dp[t1] = t0

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):
    t0 = dp[i] + 1
    for j in c:
        t1 = i + j
        if t1 <= N and dp[t1] > t0:
            dp[t1] = t0
        if t1 == 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):
    t0 = dp[i] + 1
    for j in c:
        t1 = i + j
        if t1 <= N and dp[t1] > t0:
            dp[t1] = t0
print((dp[N]))

-        if t1 == N: -            break