#公式解法見て書いた

iN = int(eval(input()))



a6 = [0]*7

for i in range(7):

    a6[i] = 6 ** i

a6.reverse()

a9 = [0]*6

for i in range(6):

    a9[i] = 9 ** i

a9.reverse()



iAns = iN

for i in range(iN+1):

    iD = iN - i

    iC = 0

    for j in range(7):

        if i:

            iG = a6[j]

            if i >= iG:

                iC += (i // iG)

                i %=iG

    for j in range(6):

        if iD :

            iG = a9[j]

            if iD >= iG:

                iC += (iD // iG)

                iD %= iG

    if iAns > iC:

        iAns = iC

print(iAns)

#公式解法見て書いた

iN = int(eval(input()))



a6 = [0]*7

for i in range(7):

    a6[i] = 6 ** i

a6.reverse()

a9 = [0]*6

for i in range(6):

    a9[i] = 9 ** i

a9.reverse()



iAns = iN

for i in range(iN+1):

    iD = iN - i

    iC = 0

    for j in range(7):

        if i:

            iG = a6[j]

            if i >= iG:

                iC += (i // iG)

                i %=iG

    if iAns <= iC:

        continue

    for j in range(6):

        if iD :

            iG = a9[j]

            if iD >= iG:

                iC += (iD // iG)

                iD %= iG

    if iAns > iC:

        iAns = iC

print(iAns)

# 公式解法見て書いた
iN = int(eval(input()))
a6 = [0] * 7
for i in range(7):
    a6[i] = 6**i
a6.reverse()
a9 = [0] * 6
for i in range(6):
    a9[i] = 9**i
a9.reverse()
iAns = iN
for i in range(iN + 1):
    iD = iN - i
    iC = 0
    for j in range(7):
        if i:
            iG = a6[j]
            if i >= iG:
                iC += i // iG
                i %= iG
    for j in range(6):
        if iD:
            iG = a9[j]
            if iD >= iG:
                iC += iD // iG
                iD %= iG
    if iAns > iC:
        iAns = iC
print(iAns)

# 公式解法見て書いた
iN = int(eval(input()))
a6 = [0] * 7
for i in range(7):
    a6[i] = 6**i
a6.reverse()
a9 = [0] * 6
for i in range(6):
    a9[i] = 9**i
a9.reverse()
iAns = iN
for i in range(iN + 1):
    iD = iN - i
    iC = 0
    for j in range(7):
        if i:
            iG = a6[j]
            if i >= iG:
                iC += i // iG
                i %= iG
    if iAns <= iC:
        continue
    for j in range(6):
        if iD:
            iG = a9[j]
            if iD >= iG:
                iC += iD // iG
                iD %= iG
    if iAns > iC:
        iAns = iC
print(iAns)

+    if iAns <= iC: +        continue