ans = [0] * 100100

ans[0] = 0



n = int(eval(input()))

#n = 127

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

    cand1 = ans[i-1]

    cand2 = 100001

    cand3 = 100001

    x = 6

    while (x<=i):

        cand2 = min(ans[i-x], cand2)

        x=6*x

    y = 9

    while (y<=i):

        cand3 = min(ans[i-y], cand3)

        y=9*y

    ans[i] = min(cand1,cand2,cand3) + 1





print((ans[n]))

# -*- coding: utf-8 -*-

"""

Created on Mon Aug  6 17:34:40 2018



@author: Yuki

"""



ans = [0] * 100100

ans[0] = 0



n = int(eval(input()))

#n = 127

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

    cand1 = ans[i-1]

    cand2 = 100001

    cand3 = 100001

    x = 6

    while (x<=i):

        #cand2 = min(ans[i-x], cand2)

        cand2 = ans[i-x]

        x=6*x

    y = 9

    while (y<=i):

        #cand3 = min(ans[i-y], cand3)

        cand3 = ans[i-y]

        y=9*y

    ans[i] = min(cand1,cand2,cand3) + 1





print((ans[n]))



ans = [0] * 100100
ans[0] = 0
n = int(eval(input()))
# n = 127
for i in range(1, n + 1):
    cand1 = ans[i - 1]
    cand2 = 100001
    cand3 = 100001
    x = 6
    while x <= i:
        cand2 = min(ans[i - x], cand2)
        x = 6 * x
    y = 9
    while y <= i:
        cand3 = min(ans[i - y], cand3)
        y = 9 * y
    ans[i] = min(cand1, cand2, cand3) + 1
print((ans[n]))

# -*- coding: utf-8 -*-
"""
Created on Mon Aug  6 17:34:40 2018
@author: Yuki
"""
ans = [0] * 100100
ans[0] = 0
n = int(eval(input()))
# n = 127
for i in range(1, n + 1):
    cand1 = ans[i - 1]
    cand2 = 100001
    cand3 = 100001
    x = 6
    while x <= i:
        # cand2 = min(ans[i-x], cand2)
        cand2 = ans[i - x]
        x = 6 * x
    y = 9
    while y <= i:
        # cand3 = min(ans[i-y], cand3)
        cand3 = ans[i - y]
        y = 9 * y
    ans[i] = min(cand1, cand2, cand3) + 1
print((ans[n]))

+# -*- coding: utf-8 -*- +""" +Created on Mon Aug  6 17:34:40 2018 +@author: Yuki +""" -        cand2 = min(ans[i - x], cand2) +        # cand2 = min(ans[i-x], cand2) +        cand2 = ans[i - x] -        cand3 = min(ans[i - y], cand3) +        # cand3 = min(ans[i-y], cand3) +        cand3 = ans[i - y]