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]))

def f(yen, coin, cnt=0):

    while yen > 0: cnt += yen % coin; yen //= coin

    return cnt



ans = n = int(eval(input()))

for yen6 in range(0, n+1):

    ans = min(ans, f(n-yen6, 9, f(yen6, 6)))

print(ans)

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]))

def f(yen, coin, cnt=0):
    while yen > 0:
        cnt += yen % coin
        yen //= coin
    return cnt


ans = n = int(eval(input()))
for yen6 in range(0, n + 1):
    ans = min(ans, f(n - yen6, 9, f(yen6, 6)))
print(ans)

-n = int(eval(input())) -dp = list(i for i in range(n + 1)) +def f(yen, coin, cnt=0): +    while yen > 0: +        cnt += yen % coin +        yen //= coin +    return cnt -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])) +ans = n = int(eval(input())) +for yen6 in range(0, n + 1): +    ans = min(ans, f(n - yen6, 9, f(yen6, 6))) +print(ans)