def main():

    N = int(eval(input()))



    dp = [float('inf')] * (N + 10)

    dp[0] = 0

    

    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()
def main():

    N = int(eval(input()))

    cand = [1]

    dp = [float('inf')] * (N + 5)

    tmp = 6

    cnt = 1

    while tmp**cnt <= N:

        cand.append(tmp**cnt)

        cnt += 1

    tmp = 9

    cnt = 1

    while tmp**cnt <= N:

        cand.append(tmp**cnt)

        cnt += 1

    cand.sort()

    dp[0] = 0

    for i in range(N+1):

        for c in cand:

            if i + c <= N:

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

    print((dp[N]))



if __name__ == "__main__":

    main()
def main():
    N = int(eval(input()))
    dp = [float("inf")] * (N + 10)
    dp[0] = 0
    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()

def main():
    N = int(eval(input()))
    cand = [1]
    dp = [float("inf")] * (N + 5)
    tmp = 6
    cnt = 1
    while tmp**cnt <= N:
        cand.append(tmp**cnt)
        cnt += 1
    tmp = 9
    cnt = 1
    while tmp**cnt <= N:
        cand.append(tmp**cnt)
        cnt += 1
    cand.sort()
    dp[0] = 0
    for i in range(N + 1):
        for c in cand:
            if i + c <= N:
                dp[i + c] = min(dp[i + c], dp[i] + 1)
    print((dp[N]))


if __name__ == "__main__":
    main()

-    dp = [float("inf")] * (N + 10) +    cand = [1] +    dp = [float("inf")] * (N + 5) +    tmp = 6 +    cnt = 1 +    while tmp**cnt <= N: +        cand.append(tmp**cnt) +        cnt += 1 +    tmp = 9 +    cnt = 1 +    while tmp**cnt <= N: +        cand.append(tmp**cnt) +        cnt += 1 +    cand.sort() -    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 +    for i in range(N + 1): +        for c in cand: +            if i + c <= N: +                dp[i + c] = min(dp[i + c], dp[i] + 1) -main() +if __name__ == "__main__": +    main()