def ds(a, b):

	return a if a<b else ds(a//b,b)+a%b

ans = float('inf')

n = int(eval(input()))

for i in range(n+1):

	ans = min(ans,ds(i,6)+ds(n-i,9))

print(ans)
def ds(a, b):

	return a if a<b else ds(a//b,b)+a%b

n = int(eval(input()))

print((min([ds(i,6)+ds(n-i,9) for i in range(n+1)])))
def ds(a, b):
    return a if a < b else ds(a // b, b) + a % b


ans = float("inf")
n = int(eval(input()))
for i in range(n + 1):
    ans = min(ans, ds(i, 6) + ds(n - i, 9))
print(ans)

def ds(a, b):
    return a if a < b else ds(a // b, b) + a % b


n = int(eval(input()))
print((min([ds(i, 6) + ds(n - i, 9) for i in range(n + 1)])))

-ans = float("inf") -for i in range(n + 1): -    ans = min(ans, ds(i, 6) + ds(n - i, 9)) -print(ans) +print((min([ds(i, 6) + ds(n - i, 9) for i in range(n + 1)])))