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()))

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:

			continue

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

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()))
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:
            continue
        dp[i] = min(dp[i], dp[i - j] + 1)
print((dp[n]))

-            break +            continue