N=int(eval(input()))

ans=N

for i in range(N+1): 

    cnt=0

    t=i

    while t>0:

        cnt+=t%6

        t//=6

    s=N-i

    while s>0:

        cnt+=s%9

        s//=9

    ans=min(ans,cnt)

print(ans)

#!/usr/bin/env python3

import sys

from itertools import product



def solve(N: int):

    nine = [9**i for i in range(5,0,-1)]

    six = [6**i for i in range(6,0,-1,)]

    answer = 10**6



    for six_bit in product([0,1,2,3,4,5],repeat = 6):

        tmp = 0

        for i in range(6):

            tmp += six[i]*six_bit[i]



        if tmp > N:

            continue

        

        count = sum(six_bit)

        cur = N-tmp

        for i in range(5):

            syou = cur//nine[i]

            cur%=nine[i]

            count += syou

        else:

            answer = min(answer,count+cur)

    print(answer)

    return





# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    solve(N)



if __name__ == '__main__':

    main()

N = int(eval(input()))
ans = N
for i in range(N + 1):
    cnt = 0
    t = i
    while t > 0:
        cnt += t % 6
        t //= 6
    s = N - i
    while s > 0:
        cnt += s % 9
        s //= 9
    ans = min(ans, cnt)
print(ans)

#!/usr/bin/env python3
import sys
from itertools import product


def solve(N: int):
    nine = [9**i for i in range(5, 0, -1)]
    six = [
        6**i
        for i in range(
            6,
            0,
            -1,
        )
    ]
    answer = 10**6
    for six_bit in product([0, 1, 2, 3, 4, 5], repeat=6):
        tmp = 0
        for i in range(6):
            tmp += six[i] * six_bit[i]
        if tmp > N:
            continue
        count = sum(six_bit)
        cur = N - tmp
        for i in range(5):
            syou = cur // nine[i]
            cur %= nine[i]
            count += syou
        else:
            answer = min(answer, count + cur)
    print(answer)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)


if __name__ == "__main__":
    main()

-N = int(eval(input())) -ans = N -for i in range(N + 1): -    cnt = 0 -    t = i -    while t > 0: -        cnt += t % 6 -        t //= 6 -    s = N - i -    while s > 0: -        cnt += s % 9 -        s //= 9 -    ans = min(ans, cnt) -print(ans) +#!/usr/bin/env python3 +import sys +from itertools import product + + +def solve(N: int): +    nine = [9**i for i in range(5, 0, -1)] +    six = [ +        6**i +        for i in range( +            6, +            0, +            -1, +        ) +    ] +    answer = 10**6 +    for six_bit in product([0, 1, 2, 3, 4, 5], repeat=6): +        tmp = 0 +        for i in range(6): +            tmp += six[i] * six_bit[i] +        if tmp > N: +            continue +        count = sum(six_bit) +        cur = N - tmp +        for i in range(5): +            syou = cur // nine[i] +            cur %= nine[i] +            count += syou +        else: +            answer = min(answer, count + cur) +    print(answer) +    return + + +# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template) +def main(): +    def iterate_tokens(): +        for line in sys.stdin: +            for word in line.split(): +                yield word + +    tokens = iterate_tokens() +    N = int(next(tokens))  # type: int +    solve(N) + + +if __name__ == "__main__": +    main()