import bisect

N = int(eval(input()))



ans = N

sixs = [1]

i = 6

while i <= N:

    sixs.append(i)

    i *= 6

nines = [1]

i = 9

while i <= N:

    nines.append(i)

    i *= 9



for i in range(N+1):

    #iの分だけ6**xで構成

    cnt = 0

    a = i

    b = N-i

    while a > 0:

        idx = bisect.bisect_right(sixs, a)

        cnt += a // sixs[idx-1]

        a -= (a // sixs[idx-1]) * sixs[idx-1]



    while b > 0:

        idx = bisect.bisect_right(nines, b)

        cnt += b // nines[idx-1]

        b -= (b // nines[idx-1]) * nines[idx-1]



    ans = min(ans, cnt)



print(ans)
from bisect import bisect_right



def main():

    N = int(eval(input()))





    sixs = [1]

    i = 6

    while i <= N:

        sixs.append(i)

        i *= 6



    nines = [1]

    i = 9

    while i <= N:

        nines.append(i)

        i *= 9



    """

    i円分を6**xxx で、　残りを９**yyyと１*zzzで構成する。これらを全通り試す

    """



    ans = N

    for i in range(N+1):

        cnt = 0

        sum_six = i

        sum_nine = N-i

        while sum_six > 0:

            idx = bisect_right(sixs, sum_six) - 1

            tmp = sum_six // sixs[idx]

            cnt += tmp

            sum_six -= tmp * sixs[idx]



        while sum_nine > 0:

            idx = bisect_right(nines, sum_nine) - 1

            tmp = sum_nine // nines[idx]

            cnt += tmp

            sum_nine -= tmp * nines[idx]



        ans = min(ans, cnt)

    print(ans)



if __name__ == "__main__":

    main()
import bisect

N = int(eval(input()))
ans = N
sixs = [1]
i = 6
while i <= N:
    sixs.append(i)
    i *= 6
nines = [1]
i = 9
while i <= N:
    nines.append(i)
    i *= 9
for i in range(N + 1):
    # iの分だけ6**xで構成
    cnt = 0
    a = i
    b = N - i
    while a > 0:
        idx = bisect.bisect_right(sixs, a)
        cnt += a // sixs[idx - 1]
        a -= (a // sixs[idx - 1]) * sixs[idx - 1]
    while b > 0:
        idx = bisect.bisect_right(nines, b)
        cnt += b // nines[idx - 1]
        b -= (b // nines[idx - 1]) * nines[idx - 1]
    ans = min(ans, cnt)
print(ans)

from bisect import bisect_right


def main():
    N = int(eval(input()))
    sixs = [1]
    i = 6
    while i <= N:
        sixs.append(i)
        i *= 6
    nines = [1]
    i = 9
    while i <= N:
        nines.append(i)
        i *= 9
    """
    i円分を6**xxx で、　残りを９**yyyと１*zzzで構成する。これらを全通り試す
    """
    ans = N
    for i in range(N + 1):
        cnt = 0
        sum_six = i
        sum_nine = N - i
        while sum_six > 0:
            idx = bisect_right(sixs, sum_six) - 1
            tmp = sum_six // sixs[idx]
            cnt += tmp
            sum_six -= tmp * sixs[idx]
        while sum_nine > 0:
            idx = bisect_right(nines, sum_nine) - 1
            tmp = sum_nine // nines[idx]
            cnt += tmp
            sum_nine -= tmp * nines[idx]
        ans = min(ans, cnt)
    print(ans)


if __name__ == "__main__":
    main()

-import bisect +from bisect import bisect_right -N = int(eval(input())) -ans = N -sixs = [1] -i = 6 -while i <= N: -    sixs.append(i) -    i *= 6 -nines = [1] -i = 9 -while i <= N: -    nines.append(i) -    i *= 9 -for i in range(N + 1): -    # iの分だけ6**xで構成 -    cnt = 0 -    a = i -    b = N - i -    while a > 0: -        idx = bisect.bisect_right(sixs, a) -        cnt += a // sixs[idx - 1] -        a -= (a // sixs[idx - 1]) * sixs[idx - 1] -    while b > 0: -        idx = bisect.bisect_right(nines, b) -        cnt += b // nines[idx - 1] -        b -= (b // nines[idx - 1]) * nines[idx - 1] -    ans = min(ans, cnt) -print(ans) + +def main(): +    N = int(eval(input())) +    sixs = [1] +    i = 6 +    while i <= N: +        sixs.append(i) +        i *= 6 +    nines = [1] +    i = 9 +    while i <= N: +        nines.append(i) +        i *= 9 +    """ +    i円分を6**xxx で、　残りを９**yyyと１*zzzで構成する。これらを全通り試す +    """ +    ans = N +    for i in range(N + 1): +        cnt = 0 +        sum_six = i +        sum_nine = N - i +        while sum_six > 0: +            idx = bisect_right(sixs, sum_six) - 1 +            tmp = sum_six // sixs[idx] +            cnt += tmp +            sum_six -= tmp * sixs[idx] +        while sum_nine > 0: +            idx = bisect_right(nines, sum_nine) - 1 +            tmp = sum_nine // nines[idx] +            cnt += tmp +            sum_nine -= tmp * nines[idx] +        ans = min(ans, cnt) +    print(ans) + + +if __name__ == "__main__": +    main()