from collections import deque

def main():

    N = int(eval(input()))

    divs = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1]

    dp = [0]*(10**6+1)

    def f(x):

        if x == 0:

            return 0

        if x < 0:

            return 10**6

        if dp[x]:

            return dp[x]

        else:

            res = 10**6

            for d in divs:

                res = min(res,f(x-d)+1)

            dp[x] = res

            return res

            



    print((f(N)))

main()
def main():

    N = int(eval(input()))

    amounts = [1]

    lim = 10**6

    mul6 = 6

    while mul6 < lim:

        amounts.append(mul6)

        mul6 *= 6

    mul9 = 9

    while mul9 < lim:

        amounts.append(mul9)

        mul9 *= 9

    

    dp = [lim] * (N+1)

    dp[0] = 0

    for i in range(N):

        for a in amounts:

            if i+a <= N:

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

    print((dp[N]))



    







main()
from collections import deque


def main():
    N = int(eval(input()))
    divs = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1]
    dp = [0] * (10**6 + 1)

    def f(x):
        if x == 0:
            return 0
        if x < 0:
            return 10**6
        if dp[x]:
            return dp[x]
        else:
            res = 10**6
            for d in divs:
                res = min(res, f(x - d) + 1)
            dp[x] = res
            return res

    print((f(N)))


main()

def main():
    N = int(eval(input()))
    amounts = [1]
    lim = 10**6
    mul6 = 6
    while mul6 < lim:
        amounts.append(mul6)
        mul6 *= 6
    mul9 = 9
    while mul9 < lim:
        amounts.append(mul9)
        mul9 *= 9
    dp = [lim] * (N + 1)
    dp[0] = 0
    for i in range(N):
        for a in amounts:
            if i + a <= N:
                dp[i + a] = min(dp[i + a], dp[i] + 1)
    print((dp[N]))


main()

-from collections import deque - - -    divs = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1] -    dp = [0] * (10**6 + 1) - -    def f(x): -        if x == 0: -            return 0 -        if x < 0: -            return 10**6 -        if dp[x]: -            return dp[x] -        else: -            res = 10**6 -            for d in divs: -                res = min(res, f(x - d) + 1) -            dp[x] = res -            return res - -    print((f(N))) +    amounts = [1] +    lim = 10**6 +    mul6 = 6 +    while mul6 < lim: +        amounts.append(mul6) +        mul6 *= 6 +    mul9 = 9 +    while mul9 < lim: +        amounts.append(mul9) +        mul9 *= 9 +    dp = [lim] * (N + 1) +    dp[0] = 0 +    for i in range(N): +        for a in amounts: +            if i + a <= N: +                dp[i + a] = min(dp[i + a], dp[i] + 1) +    print((dp[N]))