coin = [1,

6,6**2,6**3,6**4,6**5,6**6,

9,9**2,9**3,9**4,9**5]

n = int(eval(input()))

inf = 10**9

dp = [inf]*(n+1)

dp[0] = 0

for c in coin:

    for yen in range(c, n+1):

        dp[yen] = min(dp[yen], dp[yen-c] + 1)

print((dp[n]))

n = int(eval(input()))

dp = list(i for i in range(n+1))



def f(yen, p):

    c = p

    while yen >= c:

        dp[yen] = min(dp[yen], dp[yen-c] + 1)

        c *= p



for yen in range(6, n+1):

    f(yen, 6)

    f(yen, 9)

print((dp[n]))

coin = [1, 6, 6**2, 6**3, 6**4, 6**5, 6**6, 9, 9**2, 9**3, 9**4, 9**5]
n = int(eval(input()))
inf = 10**9
dp = [inf] * (n + 1)
dp[0] = 0
for c in coin:
    for yen in range(c, n + 1):
        dp[yen] = min(dp[yen], dp[yen - c] + 1)
print((dp[n]))

n = int(eval(input()))
dp = list(i for i in range(n + 1))


def f(yen, p):
    c = p
    while yen >= c:
        dp[yen] = min(dp[yen], dp[yen - c] + 1)
        c *= p


for yen in range(6, n + 1):
    f(yen, 6)
    f(yen, 9)
print((dp[n]))

-coin = [1, 6, 6**2, 6**3, 6**4, 6**5, 6**6, 9, 9**2, 9**3, 9**4, 9**5] -inf = 10**9 -dp = [inf] * (n + 1) -dp[0] = 0 -for c in coin: -    for yen in range(c, n + 1): +dp = list(i for i in range(n + 1)) + + +def f(yen, p): +    c = p +    while yen >= c: +        c *= p + + +for yen in range(6, n + 1): +    f(yen, 6) +    f(yen, 9)