N=int(eval(input()))

inf=100000

dp=[inf for _ in range(N+1)]

dp[0]=0

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

    num=100000

    i=0

    j=0

    while 9**(i+1)<=n:

        i+=1

    while 6**(j+1)<=n:

        j+=1

    for k in range(i+1):

        num=min(num,dp[n-9**k]+1)

    for k in range(j+1):

        num=min(num,dp[n-6**k]+1)

    dp[n]=min(dp[n],num)

print((dp[N]))
def main3():

    import sys

    sys.setrecursionlimit(100000000)



    N=int(eval(input()))

    dp=[-1 for n in range(N+1)]

    dp[0]=0

    dp[1]=1

    def rec(x,dp):

        if x==1:

            return dp[1]

        elif x==0:

            return dp[0]

        else:

            l=[]

            i=0

            j=0

            while 9**(i+1)<=x:

                i+=1

            while 6**(j+1)<=x:

                j+=1

            for k in reversed(list(range(i+1))):

                tmp=x-9**k

                if dp[tmp]!=-1:

                    l.append(dp[tmp]+1)

                else:

                    l.append(rec(tmp,dp)+1)

            for k in reversed(list(range(j+1))):

                tmp=x-6**k

                if dp[tmp]!=-1:

                    l.append(dp[tmp]+1)

                else:

                    l.append(rec(tmp,dp)+1)

            dp[x]=min(l)

            return dp[x]



    return rec(N,dp)

        

if __name__=="__main__":

    print((main3()))
N = int(eval(input()))
inf = 100000
dp = [inf for _ in range(N + 1)]
dp[0] = 0
for n in range(1, N + 1):
    num = 100000
    i = 0
    j = 0
    while 9 ** (i + 1) <= n:
        i += 1
    while 6 ** (j + 1) <= n:
        j += 1
    for k in range(i + 1):
        num = min(num, dp[n - 9**k] + 1)
    for k in range(j + 1):
        num = min(num, dp[n - 6**k] + 1)
    dp[n] = min(dp[n], num)
print((dp[N]))

def main3():
    import sys

    sys.setrecursionlimit(100000000)
    N = int(eval(input()))
    dp = [-1 for n in range(N + 1)]
    dp[0] = 0
    dp[1] = 1

    def rec(x, dp):
        if x == 1:
            return dp[1]
        elif x == 0:
            return dp[0]
        else:
            l = []
            i = 0
            j = 0
            while 9 ** (i + 1) <= x:
                i += 1
            while 6 ** (j + 1) <= x:
                j += 1
            for k in reversed(list(range(i + 1))):
                tmp = x - 9**k
                if dp[tmp] != -1:
                    l.append(dp[tmp] + 1)
                else:
                    l.append(rec(tmp, dp) + 1)
            for k in reversed(list(range(j + 1))):
                tmp = x - 6**k
                if dp[tmp] != -1:
                    l.append(dp[tmp] + 1)
                else:
                    l.append(rec(tmp, dp) + 1)
            dp[x] = min(l)
            return dp[x]

    return rec(N, dp)


if __name__ == "__main__":
    print((main3()))

-N = int(eval(input())) -inf = 100000 -dp = [inf for _ in range(N + 1)] -dp[0] = 0 -for n in range(1, N + 1): -    num = 100000 -    i = 0 -    j = 0 -    while 9 ** (i + 1) <= n: -        i += 1 -    while 6 ** (j + 1) <= n: -        j += 1 -    for k in range(i + 1): -        num = min(num, dp[n - 9**k] + 1) -    for k in range(j + 1): -        num = min(num, dp[n - 6**k] + 1) -    dp[n] = min(dp[n], num) -print((dp[N])) +def main3(): +    import sys + +    sys.setrecursionlimit(100000000) +    N = int(eval(input())) +    dp = [-1 for n in range(N + 1)] +    dp[0] = 0 +    dp[1] = 1 + +    def rec(x, dp): +        if x == 1: +            return dp[1] +        elif x == 0: +            return dp[0] +        else: +            l = [] +            i = 0 +            j = 0 +            while 9 ** (i + 1) <= x: +                i += 1 +            while 6 ** (j + 1) <= x: +                j += 1 +            for k in reversed(list(range(i + 1))): +                tmp = x - 9**k +                if dp[tmp] != -1: +                    l.append(dp[tmp] + 1) +                else: +                    l.append(rec(tmp, dp) + 1) +            for k in reversed(list(range(j + 1))): +                tmp = x - 6**k +                if dp[tmp] != -1: +                    l.append(dp[tmp] + 1) +                else: +                    l.append(rec(tmp, dp) + 1) +            dp[x] = min(l) +            return dp[x] + +    return rec(N, dp) + + +if __name__ == "__main__": +    print((main3()))