l=[100000 for i in range(100001)]

l[0]=0

dif=[1,6,9,36,81,216,729,1296,6561,7776,46656,59049]

for i in range(1,100001):

	for j in dif:

		if j>i:

			break

		l[i]=min(l[i],l[i-j]+1)

n=int(eval(input()))

print((l[n]))
dp=[100000 for i in range(100001)]

dp[0]=0

l=[1]

i=6

while i<=100000:

    l.append(i)

    i*=6

i=9

while i<=100000:

    l.append(i)

    i*=9

for i in range(1,100001):

    for j in l:

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

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

print((dp[int(eval(input()))]))

l = [100000 for i in range(100001)]
l[0] = 0
dif = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
for i in range(1, 100001):
    for j in dif:
        if j > i:
            break
        l[i] = min(l[i], l[i - j] + 1)
n = int(eval(input()))
print((l[n]))

dp = [100000 for i in range(100001)]
dp[0] = 0
l = [1]
i = 6
while i <= 100000:
    l.append(i)
    i *= 6
i = 9
while i <= 100000:
    l.append(i)
    i *= 9
for i in range(1, 100001):
    for j in l:
        if j <= i and dp[i - j] + 1 < dp[i]:
            dp[i] = dp[i - j] + 1
print((dp[int(eval(input()))]))

-l = [100000 for i in range(100001)] -l[0] = 0 -dif = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049] +dp = [100000 for i in range(100001)] +dp[0] = 0 +l = [1] +i = 6 +while i <= 100000: +    l.append(i) +    i *= 6 +i = 9 +while i <= 100000: +    l.append(i) +    i *= 9 -    for j in dif: -        if j > i: -            break -        l[i] = min(l[i], l[i - j] + 1) -n = int(eval(input())) -print((l[n])) +    for j in l: +        if j <= i and dp[i - j] + 1 < dp[i]: +            dp[i] = dp[i - j] + 1 +print((dp[int(eval(input()))]))