n=int(eval(input()))

dp=list(range(n+1))

C=[]

i=1

while n>=6**i:

    C.append(6**i)

    i+=1

i=1

while n>=9**i:

    C.append(9**i)

    i+=1

C.sort()





for c in C:

    for i in range(n+1):

        if i-c<0:continue

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

print((dp[-1]))

n=int(eval(input()))

dp=list(range(n+1))

C=[]

i=1

while n>=6**i:

    C.append(6**i)

    C.append(9**i)

    i+=1

i=1

while n>=9**i:

    C.append(9**i)

    i+=1

C.sort()





for c in C:

    for i in range(n+1):

        if i-c<0:continue

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

print((dp[-1]))

n = int(eval(input()))
dp = list(range(n + 1))
C = []
i = 1
while n >= 6**i:
    C.append(6**i)
    i += 1
i = 1
while n >= 9**i:
    C.append(9**i)
    i += 1
C.sort()
for c in C:
    for i in range(n + 1):
        if i - c < 0:
            continue
        dp[i] = min(dp[i], dp[i - c] + 1)
print((dp[-1]))

n = int(eval(input()))
dp = list(range(n + 1))
C = []
i = 1
while n >= 6**i:
    C.append(6**i)
    C.append(9**i)
    i += 1
i = 1
while n >= 9**i:
    C.append(9**i)
    i += 1
C.sort()
for c in C:
    for i in range(n + 1):
        if i - c < 0:
            continue
        dp[i] = min(dp[i], dp[i - c] + 1)
print((dp[-1]))

+    C.append(9**i)