def main():

    n=int(eval(input()))

    mon6=[6]

    mon9=[1]

    for i in range(5):

        mon6.append(6*mon6[i])

        mon9.append(9*mon9[i])

    mon=mon6+mon9

    mon.sort()

    

    

    inf=10**9

    dp=[[inf]*(n+1) for i in range(13)]

    

    for i in range(13):

      dp[i][0]=0

    for i in range(12):

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

            if j-mon[i]>=0:

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

            else:

                dp[i+1][j]=dp[i][j]

    print((dp[-1][-1])) 

if __name__=='__main__':

    main()
import sys

sys.setrecursionlimit(10000000)

MOD = 10 ** 9 + 7

INF = 10 ** 15



def main():

    N = int(eval(input()))

    money = [1]

    for i in range(10):

        money.append(money[-1]*6)

    money.append(9)

    for i in range(9):

        money.append(money[-1]*9)

    

    dp = [INF] * (1 + N)

    dp[0] = 0

    for m in money:

        for j in range(m,N + 1):

            dp[j] = min(dp[j],dp[j - m] + 1)

    ans = dp[-1]

    print(ans)

if __name__ == '__main__':

    main()
def main():
    n = int(eval(input()))
    mon6 = [6]
    mon9 = [1]
    for i in range(5):
        mon6.append(6 * mon6[i])
        mon9.append(9 * mon9[i])
    mon = mon6 + mon9
    mon.sort()
    inf = 10**9
    dp = [[inf] * (n + 1) for i in range(13)]
    for i in range(13):
        dp[i][0] = 0
    for i in range(12):
        for j in range(1, n + 1):
            if j - mon[i] >= 0:
                dp[i + 1][j] = min(dp[i + 1][j - mon[i]] + 1, dp[i][j])
            else:
                dp[i + 1][j] = dp[i][j]
    print((dp[-1][-1]))


if __name__ == "__main__":
    main()

import sys

sys.setrecursionlimit(10000000)
MOD = 10**9 + 7
INF = 10**15


def main():
    N = int(eval(input()))
    money = [1]
    for i in range(10):
        money.append(money[-1] * 6)
    money.append(9)
    for i in range(9):
        money.append(money[-1] * 9)
    dp = [INF] * (1 + N)
    dp[0] = 0
    for m in money:
        for j in range(m, N + 1):
            dp[j] = min(dp[j], dp[j - m] + 1)
    ans = dp[-1]
    print(ans)


if __name__ == "__main__":
    main()

+import sys + +sys.setrecursionlimit(10000000) +MOD = 10**9 + 7 +INF = 10**15 + + -    n = int(eval(input())) -    mon6 = [6] -    mon9 = [1] -    for i in range(5): -        mon6.append(6 * mon6[i]) -        mon9.append(9 * mon9[i]) -    mon = mon6 + mon9 -    mon.sort() -    inf = 10**9 -    dp = [[inf] * (n + 1) for i in range(13)] -    for i in range(13): -        dp[i][0] = 0 -    for i in range(12): -        for j in range(1, n + 1): -            if j - mon[i] >= 0: -                dp[i + 1][j] = min(dp[i + 1][j - mon[i]] + 1, dp[i][j]) -            else: -                dp[i + 1][j] = dp[i][j] -    print((dp[-1][-1])) +    N = int(eval(input())) +    money = [1] +    for i in range(10): +        money.append(money[-1] * 6) +    money.append(9) +    for i in range(9): +        money.append(money[-1] * 9) +    dp = [INF] * (1 + N) +    dp[0] = 0 +    for m in money: +        for j in range(m, N + 1): +            dp[j] = min(dp[j], dp[j - m] + 1) +    ans = dp[-1] +    print(ans)