import sys

from math import log, floor



n = int(sys.stdin.readline().rstrip())



def main():

    cand = set()

    for i in range(floor(log(n, 6)) + 1):

        if 6 ** i <= n:

            cand.add(6 ** i)

        if 9 ** i <= n:

            cand.add(9 ** i)

    cand = sorted(cand)



    res = set([0])

    cnt = 0

    while True:

        nex = set()

        for val in res:

            for c in cand:

                if val + c <= n:

                    nex.add(val + c)

                else:

                    break

        cnt += 1

        if n in nex:

            return cnt

        res = nex

    

if __name__ == '__main__':

    ans = main()

    print(ans)
import sys



n = int(sys.stdin.readline().rstrip())



def main():

    bit6 = [0] * 8

    bit9 = [0] * 7

    b = n

    for i in range(5, -1, -1):

        q, r = divmod(b, 9 ** i)

        bit9[i] = q

        b = r



    res = sum(bit6) + sum(bit9)



    for _ in range(n):

        i = 0

        while True:

            bit6[i] += 1

            if bit6[i] == 6:

                bit6[i] = 0

                i += 1

            else:

                break



        i = 0

        while True:

            bit9[i] -= 1

            if bit9[i] == -1:

                bit9[i] = 8

                i += 1

            else:

                break        



        res = min(res, sum(bit6) + sum(bit9))



    return res



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys
from math import log, floor

n = int(sys.stdin.readline().rstrip())


def main():
    cand = set()
    for i in range(floor(log(n, 6)) + 1):
        if 6**i <= n:
            cand.add(6**i)
        if 9**i <= n:
            cand.add(9**i)
    cand = sorted(cand)
    res = set([0])
    cnt = 0
    while True:
        nex = set()
        for val in res:
            for c in cand:
                if val + c <= n:
                    nex.add(val + c)
                else:
                    break
        cnt += 1
        if n in nex:
            return cnt
        res = nex


if __name__ == "__main__":
    ans = main()
    print(ans)

import sys

n = int(sys.stdin.readline().rstrip())


def main():
    bit6 = [0] * 8
    bit9 = [0] * 7
    b = n
    for i in range(5, -1, -1):
        q, r = divmod(b, 9**i)
        bit9[i] = q
        b = r
    res = sum(bit6) + sum(bit9)
    for _ in range(n):
        i = 0
        while True:
            bit6[i] += 1
            if bit6[i] == 6:
                bit6[i] = 0
                i += 1
            else:
                break
        i = 0
        while True:
            bit9[i] -= 1
            if bit9[i] == -1:
                bit9[i] = 8
                i += 1
            else:
                break
        res = min(res, sum(bit6) + sum(bit9))
    return res


if __name__ == "__main__":
    ans = main()
    print(ans)

-from math import log, floor -    cand = set() -    for i in range(floor(log(n, 6)) + 1): -        if 6**i <= n: -            cand.add(6**i) -        if 9**i <= n: -            cand.add(9**i) -    cand = sorted(cand) -    res = set([0]) -    cnt = 0 -    while True: -        nex = set() -        for val in res: -            for c in cand: -                if val + c <= n: -                    nex.add(val + c) -                else: -                    break -        cnt += 1 -        if n in nex: -            return cnt -        res = nex +    bit6 = [0] * 8 +    bit9 = [0] * 7 +    b = n +    for i in range(5, -1, -1): +        q, r = divmod(b, 9**i) +        bit9[i] = q +        b = r +    res = sum(bit6) + sum(bit9) +    for _ in range(n): +        i = 0 +        while True: +            bit6[i] += 1 +            if bit6[i] == 6: +                bit6[i] = 0 +                i += 1 +            else: +                break +        i = 0 +        while True: +            bit9[i] -= 1 +            if bit9[i] == -1: +                bit9[i] = 8 +                i += 1 +            else: +                break +        res = min(res, sum(bit6) + sum(bit9)) +    return res