n = int(eval(input()))

a = 6

b = [1]

b.append(a)

while a <= 100000:

	a *= 6

	b.append(a)

c = 9

b.append(c)

while c <= 100000:

	c *= 9

	b.append(c)

b.sort()

INF = 10**7

dp = [INF]*100001

dp[0] = 0

for i in range(100001):

	for j in b:

		if i + j > 100000:

			continue

		dp[i+j] = min(dp[i]+1, dp[i+j])

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

ary = [1]

x = 1

for i in range(6):

	x *= 6

	ary.append(x)

x = 1

for i in range(5):

	x *= 9

	ary.append(x)

ary.sort()



dp = [i for i in range(n+1)]



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

	for j in ary:

		if j > i:

			break

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

print((dp[n]))
n = int(eval(input()))
a = 6
b = [1]
b.append(a)
while a <= 100000:
    a *= 6
    b.append(a)
c = 9
b.append(c)
while c <= 100000:
    c *= 9
    b.append(c)
b.sort()
INF = 10**7
dp = [INF] * 100001
dp[0] = 0
for i in range(100001):
    for j in b:
        if i + j > 100000:
            continue
        dp[i + j] = min(dp[i] + 1, dp[i + j])
print((dp[n]))

n = int(eval(input()))
ary = [1]
x = 1
for i in range(6):
    x *= 6
    ary.append(x)
x = 1
for i in range(5):
    x *= 9
    ary.append(x)
ary.sort()
dp = [i for i in range(n + 1)]
for i in range(1, n + 1):
    for j in ary:
        if j > i:
            break
        dp[i] = min(dp[i], dp[i - j] + 1)
print((dp[n]))

-a = 6 -b = [1] -b.append(a) -while a <= 100000: -    a *= 6 -    b.append(a) -c = 9 -b.append(c) -while c <= 100000: -    c *= 9 -    b.append(c) -b.sort() -INF = 10**7 -dp = [INF] * 100001 -dp[0] = 0 -for i in range(100001): -    for j in b: -        if i + j > 100000: -            continue -        dp[i + j] = min(dp[i] + 1, dp[i + j]) +ary = [1] +x = 1 +for i in range(6): +    x *= 6 +    ary.append(x) +x = 1 +for i in range(5): +    x *= 9 +    ary.append(x) +ary.sort() +dp = [i for i in range(n + 1)] +for i in range(1, n + 1): +    for j in ary: +        if j > i: +            break +        dp[i] = min(dp[i], dp[i - j] + 1)