# -*- coding: utf-8 -*-

# AtCoder Beginner Contest

# Problem C





if __name__ == '__main__':

    n = int(eval(input()))

    dp = [0] + [100001 for _ in range(n + 1)]



    # See:

    # https://www.youtube.com/watch?v=I-8BnRkq6Ow

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

        power = 1



        while power <= i:

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

            power *= 6



        power = 1



        while power <= i:

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

            power *= 9



    print((dp[n]))

# -*- coding: utf-8 -*-





def main():

    n = int(eval(input()))

    dp = [float('inf') for _ in range(n + 100)]

    dp[0] = 0



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

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



        six = 6



        while six <= i:

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

            six *= 6



        nine = 9



        while nine <= i:

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

            nine *= 9



    print((dp[n]))





if __name__ == '__main__':

    main()

# -*- coding: utf-8 -*-
# AtCoder Beginner Contest
# Problem C
if __name__ == "__main__":
    n = int(eval(input()))
    dp = [0] + [100001 for _ in range(n + 1)]
    # See:
    # https://www.youtube.com/watch?v=I-8BnRkq6Ow
    for i in range(1, n + 1):
        power = 1
        while power <= i:
            dp[i] = min(dp[i], dp[i - power] + 1)
            power *= 6
        power = 1
        while power <= i:
            dp[i] = min(dp[i], dp[i - power] + 1)
            power *= 9
    print((dp[n]))

# -*- coding: utf-8 -*-
def main():
    n = int(eval(input()))
    dp = [float("inf") for _ in range(n + 100)]
    dp[0] = 0
    for i in range(1, n + 1):
        dp[i] = min(dp[i - 1] + 1, dp[i])
        six = 6
        while six <= i:
            dp[i] = min(dp[i - six] + 1, dp[i])
            six *= 6
        nine = 9
        while nine <= i:
            dp[i] = min(dp[i - nine] + 1, dp[i])
            nine *= 9
    print((dp[n]))


if __name__ == "__main__":
    main()

-# AtCoder Beginner Contest -# Problem C +def main(): +    n = int(eval(input())) +    dp = [float("inf") for _ in range(n + 100)] +    dp[0] = 0 +    for i in range(1, n + 1): +        dp[i] = min(dp[i - 1] + 1, dp[i]) +        six = 6 +        while six <= i: +            dp[i] = min(dp[i - six] + 1, dp[i]) +            six *= 6 +        nine = 9 +        while nine <= i: +            dp[i] = min(dp[i - nine] + 1, dp[i]) +            nine *= 9 +    print((dp[n])) + + -    n = int(eval(input())) -    dp = [0] + [100001 for _ in range(n + 1)] -    # See: -    # https://www.youtube.com/watch?v=I-8BnRkq6Ow -    for i in range(1, n + 1): -        power = 1 -        while power <= i: -            dp[i] = min(dp[i], dp[i - power] + 1) -            power *= 6 -        power = 1 -        while power <= i: -            dp[i] = min(dp[i], dp[i - power] + 1) -            power *= 9 -    print((dp[n])) +    main()