import math

n = int(eval(input()))

#n = 127

inf = float('inf')



dp = [inf]*(n+100000)

dp[0]=0

#print(dp)

#print(len(dp))



def motome(target):

    tmp = []

    for i in range(10):

        if 9**i <= n:

            tmp.append(9**i)

        if 6**i <= n:

            tmp.append(6**i)

    #print(tmp) 

    #[1, 6, 36, 1, 9, 81]

    #print(dp)

    for k in range(target+1):

        for t in sorted(list(set(tmp))):

            if k-t>=0:

                dp[k] = min(dp[k],1 + dp[k-t])

                #dp[k] = min([dp[k-t] for t in tmp])

                '''

                try:

                    dp[k] = min(dp[k],1 + min([dp[k-t] for t in tmp]))

                except:

                    print(len(dp),dp[k-t],dp[k])

                    pass

                '''

        #print(k)

        #print([t for t in tmp])

        #print([dp[k-t] for t in tmp])



motome(n)

#print(dp[n-1])

print((dp[n]))

#print(dp)

#print(dp[n+2])

#print(dp[n-1])

#print(dp[n])
n = int(eval(input()))

inf = float('inf')

dp = [inf]*(n+1)

dp[0] = 0

a = 1

b = 6

c = 9

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

    if i>=b*6:

        b*=6

    if i>=c*9:

        c*=9

    if i<6:

        dp[i] = i

    elif i<9:

        dp[i] = min(dp[i],dp[i-b]+1)

    else:

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

print((dp[n]))
import math

n = int(eval(input()))
# n = 127
inf = float("inf")
dp = [inf] * (n + 100000)
dp[0] = 0
# print(dp)
# print(len(dp))
def motome(target):
    tmp = []
    for i in range(10):
        if 9**i <= n:
            tmp.append(9**i)
        if 6**i <= n:
            tmp.append(6**i)
    # print(tmp)
    # [1, 6, 36, 1, 9, 81]
    # print(dp)
    for k in range(target + 1):
        for t in sorted(list(set(tmp))):
            if k - t >= 0:
                dp[k] = min(dp[k], 1 + dp[k - t])
                # dp[k] = min([dp[k-t] for t in tmp])
                """
                try:
                    dp[k] = min(dp[k],1 + min([dp[k-t] for t in tmp]))
                except:
                    print(len(dp),dp[k-t],dp[k])
                    pass
                """
        # print(k)
        # print([t for t in tmp])
        # print([dp[k-t] for t in tmp])


motome(n)
# print(dp[n-1])
print((dp[n]))
# print(dp)
# print(dp[n+2])
# print(dp[n-1])
# print(dp[n])

n = int(eval(input()))
inf = float("inf")
dp = [inf] * (n + 1)
dp[0] = 0
a = 1
b = 6
c = 9
for i in range(1, n + 1):
    if i >= b * 6:
        b *= 6
    if i >= c * 9:
        c *= 9
    if i < 6:
        dp[i] = i
    elif i < 9:
        dp[i] = min(dp[i], dp[i - b] + 1)
    else:
        dp[i] = min(dp[i], dp[i - b] + 1, dp[i - c] + 1)
print((dp[n]))

-import math - -# n = 127 -dp = [inf] * (n + 100000) +dp = [inf] * (n + 1) -# print(dp) -# print(len(dp)) -def motome(target): -    tmp = [] -    for i in range(10): -        if 9**i <= n: -            tmp.append(9**i) -        if 6**i <= n: -            tmp.append(6**i) -    # print(tmp) -    # [1, 6, 36, 1, 9, 81] -    # print(dp) -    for k in range(target + 1): -        for t in sorted(list(set(tmp))): -            if k - t >= 0: -                dp[k] = min(dp[k], 1 + dp[k - t]) -                # dp[k] = min([dp[k-t] for t in tmp]) -                """ -                try: -                    dp[k] = min(dp[k],1 + min([dp[k-t] for t in tmp])) -                except: -                    print(len(dp),dp[k-t],dp[k]) -                    pass -                """ -        # print(k) -        # print([t for t in tmp]) -        # print([dp[k-t] for t in tmp]) - - -motome(n) -# print(dp[n-1]) +a = 1 +b = 6 +c = 9 +for i in range(1, n + 1): +    if i >= b * 6: +        b *= 6 +    if i >= c * 9: +        c *= 9 +    if i < 6: +        dp[i] = i +    elif i < 9: +        dp[i] = min(dp[i], dp[i - b] + 1) +    else: +        dp[i] = min(dp[i], dp[i - b] + 1, dp[i - c] + 1) -# print(dp) -# print(dp[n+2]) -# print(dp[n-1]) -# print(dp[n])