n = int(eval(input()))

dp = [float('Inf')] * (100000 + 1)

dp[0] = 0

for i in range(1, n + 1):

    dp[i] = dp[i - 1] + 1

    cnt = 1

    for j in range(0, 7):

        cnt *= 6 

        if i - cnt >= 0:

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

    cnt = 1

    for j in range(0, 6):

        cnt *= 9

        if i - cnt >= 0:

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

print((dp[n]))
n = int(eval(input()))

dp = [float('Inf')] * (n + 1)

dp[0] = 0

for i in range(1, n + 1):

    j = 1

    while j <= i:

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

        j *= 6

    j = 1

    while j <= i:

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

        j *= 9

print((dp[n]))
n = int(eval(input()))
dp = [float("Inf")] * (100000 + 1)
dp[0] = 0
for i in range(1, n + 1):
    dp[i] = dp[i - 1] + 1
    cnt = 1
    for j in range(0, 7):
        cnt *= 6
        if i - cnt >= 0:
            dp[i] = min(dp[i], dp[i - cnt] + 1)
    cnt = 1
    for j in range(0, 6):
        cnt *= 9
        if i - cnt >= 0:
            dp[i] = min(dp[i], dp[i - cnt] + 1)
print((dp[n]))

n = int(eval(input()))
dp = [float("Inf")] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
    j = 1
    while j <= i:
        dp[i] = min(dp[i], dp[i - j] + 1)
        j *= 6
    j = 1
    while j <= i:
        dp[i] = min(dp[i], dp[i - j] + 1)
        j *= 9
print((dp[n]))

-dp = [float("Inf")] * (100000 + 1) +dp = [float("Inf")] * (n + 1) -    dp[i] = dp[i - 1] + 1 -    cnt = 1 -    for j in range(0, 7): -        cnt *= 6 -        if i - cnt >= 0: -            dp[i] = min(dp[i], dp[i - cnt] + 1) -    cnt = 1 -    for j in range(0, 6): -        cnt *= 9 -        if i - cnt >= 0: -            dp[i] = min(dp[i], dp[i - cnt] + 1) +    j = 1 +    while j <= i: +        dp[i] = min(dp[i], dp[i - j] + 1) +        j *= 6 +    j = 1 +    while j <= i: +        dp[i] = min(dp[i], dp[i - j] + 1) +        j *= 9