six=[6**i for i in range(1,7)][::-1]

nine=[9**i for i in range(1,6)][::-1]

N=int(eval(input()))



ans=[]

def find(n,a):

    if n<6: ans.append(n+a)

    for s in six:

        if s<=n:

            find(n-s,a+1)

            break

    for s in nine:

        if s<=n:

            find(n-s,a+1)

            break

find(N,0)

print((min(ans)))
six=[6**i for i in range(1,7)][::-1]

nine=[9**i for i in range(1,6)][::-1]

N=int(eval(input()))



ans=[]

def find(n,a):

    if n<6:

        ans.append(n+a)

        return

    for s in six:

        if s<=n:

            find(n-s,a+1)

            break

    for s in nine:

        if s<=n:

            find(n-s,a+1)

            break

find(N,0)

print((min(ans)))
six = [6**i for i in range(1, 7)][::-1]
nine = [9**i for i in range(1, 6)][::-1]
N = int(eval(input()))
ans = []


def find(n, a):
    if n < 6:
        ans.append(n + a)
    for s in six:
        if s <= n:
            find(n - s, a + 1)
            break
    for s in nine:
        if s <= n:
            find(n - s, a + 1)
            break


find(N, 0)
print((min(ans)))

six = [6**i for i in range(1, 7)][::-1]
nine = [9**i for i in range(1, 6)][::-1]
N = int(eval(input()))
ans = []


def find(n, a):
    if n < 6:
        ans.append(n + a)
        return
    for s in six:
        if s <= n:
            find(n - s, a + 1)
            break
    for s in nine:
        if s <= n:
            find(n - s, a + 1)
            break


find(N, 0)
print((min(ans)))

+        return