N=eval(input())



dp=[ float("inf") for i in range(N+1) ]

dp[0]=0



i=1



for i in range(1,8):

	for n in range(N):

		if n+ 9**i  <=N: 	dp[ n+ 9**i ]=min( dp[n]+1, dp[ n+9**i ] )

		if n+ 6**i  <=N: 	dp[ n+ 6**i ]=min( dp[n]+1, dp[ n+6**i ] )

		if n+ 1  <=N:	dp[ n+ 1 ]=min( dp[n]+1, dp[ n+1 ] )





print(dp[N])
N=eval(input())	#N=a+b



ans=float("inf")

for a in range(N+1):

	b=N-a

	cnt=0

	for i in range(8,-1,-1):

		t=6**i

		if a>=t:

			cnt+=(a/t)

			a=a%t



	for i in range(8,-1,-1):

		t=9**i

		if b>=t:

			cnt+=(b/t)

			b=b%t

	ans=min(ans,cnt)



print(ans)

		
N = eval(input())
dp = [float("inf") for i in range(N + 1)]
dp[0] = 0
i = 1
for i in range(1, 8):
    for n in range(N):
        if n + 9**i <= N:
            dp[n + 9**i] = min(dp[n] + 1, dp[n + 9**i])
        if n + 6**i <= N:
            dp[n + 6**i] = min(dp[n] + 1, dp[n + 6**i])
        if n + 1 <= N:
            dp[n + 1] = min(dp[n] + 1, dp[n + 1])
print(dp[N])

N = eval(input())  # N=a+b
ans = float("inf")
for a in range(N + 1):
    b = N - a
    cnt = 0
    for i in range(8, -1, -1):
        t = 6**i
        if a >= t:
            cnt += a / t
            a = a % t
    for i in range(8, -1, -1):
        t = 9**i
        if b >= t:
            cnt += b / t
            b = b % t
    ans = min(ans, cnt)
print(ans)

-N = eval(input()) -dp = [float("inf") for i in range(N + 1)] -dp[0] = 0 -i = 1 -for i in range(1, 8): -    for n in range(N): -        if n + 9**i <= N: -            dp[n + 9**i] = min(dp[n] + 1, dp[n + 9**i]) -        if n + 6**i <= N: -            dp[n + 6**i] = min(dp[n] + 1, dp[n + 6**i]) -        if n + 1 <= N: -            dp[n + 1] = min(dp[n] + 1, dp[n + 1]) -print(dp[N]) +N = eval(input())  # N=a+b +ans = float("inf") +for a in range(N + 1): +    b = N - a +    cnt = 0 +    for i in range(8, -1, -1): +        t = 6**i +        if a >= t: +            cnt += a / t +            a = a % t +    for i in range(8, -1, -1): +        t = 9**i +        if b >= t: +            cnt += b / t +            b = b % t +    ans = min(ans, cnt) +print(ans)