lists=[]

emptylists=[]

numbers=int(eval(input()))



for nums in range(numbers+3):

    

    if nums<6:

        lists.append(int(nums))

         

    elif nums==6 or nums==36 or nums==216 or nums==1296 or nums==7776 or nums==46656:

        lists.append(1)

        

    elif nums==9 or nums==81 or nums==729 or nums==6561 or nums==59049:

        lists.append(1)

        

    else:

        power6=0

        power9=0

        for k in range(7):

            if 6**(k)< nums <6**(k+1):

                power6=k

                break   

                

        for j in range(6):

            if 9**(j) <nums< 9**(j+1):

                power9=j

                break

        add=min(min(lists[nums-6**(k)]+1 for k in range(0,power6+1)),min(lists[nums-9**(j)]+1 for j in range(0,power9+1)))

        lists.append(add)

print((lists[numbers]))

#strange bank

n=int(eval(input()))

dp=[10**10 for i in range(200000)]

dp[0]=0

for i in range(1,100001):

    for j in range(9):

        num=3**j

        if i>=6**j and dp[i-6**j]+1<dp[i]:

            dp[i]=dp[i-6**j]+1

        if i>=9**j and dp[i-9**j]+1<dp[i]:

            dp[i]=dp[i-9**j]+1

print((dp[n]))
lists = []
emptylists = []
numbers = int(eval(input()))
for nums in range(numbers + 3):
    if nums < 6:
        lists.append(int(nums))
    elif (
        nums == 6
        or nums == 36
        or nums == 216
        or nums == 1296
        or nums == 7776
        or nums == 46656
    ):
        lists.append(1)
    elif nums == 9 or nums == 81 or nums == 729 or nums == 6561 or nums == 59049:
        lists.append(1)
    else:
        power6 = 0
        power9 = 0
        for k in range(7):
            if 6 ** (k) < nums < 6 ** (k + 1):
                power6 = k
                break
        for j in range(6):
            if 9 ** (j) < nums < 9 ** (j + 1):
                power9 = j
                break
        add = min(
            min(lists[nums - 6 ** (k)] + 1 for k in range(0, power6 + 1)),
            min(lists[nums - 9 ** (j)] + 1 for j in range(0, power9 + 1)),
        )
        lists.append(add)
print((lists[numbers]))

# strange bank
n = int(eval(input()))
dp = [10**10 for i in range(200000)]
dp[0] = 0
for i in range(1, 100001):
    for j in range(9):
        num = 3**j
        if i >= 6 ** j and dp[i - 6**j] + 1 < dp[i]:
            dp[i] = dp[i - 6**j] + 1
        if i >= 9 ** j and dp[i - 9**j] + 1 < dp[i]:
            dp[i] = dp[i - 9**j] + 1
print((dp[n]))

-lists = [] -emptylists = [] -numbers = int(eval(input())) -for nums in range(numbers + 3): -    if nums < 6: -        lists.append(int(nums)) -    elif ( -        nums == 6 -        or nums == 36 -        or nums == 216 -        or nums == 1296 -        or nums == 7776 -        or nums == 46656 -    ): -        lists.append(1) -    elif nums == 9 or nums == 81 or nums == 729 or nums == 6561 or nums == 59049: -        lists.append(1) -    else: -        power6 = 0 -        power9 = 0 -        for k in range(7): -            if 6 ** (k) < nums < 6 ** (k + 1): -                power6 = k -                break -        for j in range(6): -            if 9 ** (j) < nums < 9 ** (j + 1): -                power9 = j -                break -        add = min( -            min(lists[nums - 6 ** (k)] + 1 for k in range(0, power6 + 1)), -            min(lists[nums - 9 ** (j)] + 1 for j in range(0, power9 + 1)), -        ) -        lists.append(add) -print((lists[numbers])) +# strange bank +n = int(eval(input())) +dp = [10**10 for i in range(200000)] +dp[0] = 0 +for i in range(1, 100001): +    for j in range(9): +        num = 3**j +        if i >= 6 ** j and dp[i - 6**j] + 1 < dp[i]: +            dp[i] = dp[i - 6**j] + 1 +        if i >= 9 ** j and dp[i - 9**j] + 1 < dp[i]: +            dp[i] = dp[i - 9**j] + 1 +print((dp[n]))