import sys



input = sys.stdin.readline





def main():

    N = int(eval(input()))



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

    dp[1] = 1



    x = [1]

    x.extend([6 ** x for x in range(1, 8)])

    x.extend([9 ** x for x in range(1, 6)])





    for i in range(2, 10 ** 5 + 1):

        tmp = []

        for v in x:

            if i >= v:

                tmp.append(dp[i - v] + 1)



        dp[i] = min(tmp)



    print((dp[N]))





if __name__ == '__main__':

    main()

N = int(eval(input()))



dp = [0] * 100001



dp[1] = 1



for i in range(2, N + 1):

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

    j = 1

    while 6 ** j <= i:

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

        j += 1

    j = 1

    while 9 ** j <= i:

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

        j += 1



print((dp[N]))

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    dp = [0] * (10**5 + 1)
    dp[1] = 1
    x = [1]
    x.extend([6**x for x in range(1, 8)])
    x.extend([9**x for x in range(1, 6)])
    for i in range(2, 10**5 + 1):
        tmp = []
        for v in x:
            if i >= v:
                tmp.append(dp[i - v] + 1)
        dp[i] = min(tmp)
    print((dp[N]))


if __name__ == "__main__":
    main()

N = int(eval(input()))
dp = [0] * 100001
dp[1] = 1
for i in range(2, N + 1):
    dp[i] = dp[i - 1] + 1
    j = 1
    while 6**j <= i:
        dp[i] = min(dp[i], dp[i - (6**j)] + 1)
        j += 1
    j = 1
    while 9**j <= i:
        dp[i] = min(dp[i], dp[i - (9**j)] + 1)
        j += 1
print((dp[N]))

-import sys - -input = sys.stdin.readline - - -def main(): -    N = int(eval(input())) -    dp = [0] * (10**5 + 1) -    dp[1] = 1 -    x = [1] -    x.extend([6**x for x in range(1, 8)]) -    x.extend([9**x for x in range(1, 6)]) -    for i in range(2, 10**5 + 1): -        tmp = [] -        for v in x: -            if i >= v: -                tmp.append(dp[i - v] + 1) -        dp[i] = min(tmp) -    print((dp[N])) - - -if __name__ == "__main__": -    main() +N = int(eval(input())) +dp = [0] * 100001 +dp[1] = 1 +for i in range(2, N + 1): +    dp[i] = dp[i - 1] + 1 +    j = 1 +    while 6**j <= i: +        dp[i] = min(dp[i], dp[i - (6**j)] + 1) +        j += 1 +    j = 1 +    while 9**j <= i: +        dp[i] = min(dp[i], dp[i - (9**j)] + 1) +        j += 1 +print((dp[N]))