N=int(eval(input()))



M=[0]*(N+1)

M[1]=1



for i in range(2,N+1):

  L=[M[i-1]+1]

  j=0

  while i-6**j>=0:

    L.append(M[i-6**j]+1)

    j+=1

  j=0

  while i-9**j>=0:

    L.append(M[i-9**j]+1)

    j+=1

  M[i]=min(L)

#print(M)



print((M[N]))
N = int(eval(input()))



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



INF = 10**6

dp = [INF] * (N+1)

dp[0] = 0



for i in range(N+1):

  for p in pattern:

    if i + p <= N:

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



print((dp[N]))
N = int(eval(input()))
M = [0] * (N + 1)
M[1] = 1
for i in range(2, N + 1):
    L = [M[i - 1] + 1]
    j = 0
    while i - 6**j >= 0:
        L.append(M[i - 6**j] + 1)
        j += 1
    j = 0
    while i - 9**j >= 0:
        L.append(M[i - 9**j] + 1)
        j += 1
    M[i] = min(L)
# print(M)
print((M[N]))

N = int(eval(input()))
pattern = [1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049]
INF = 10**6
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    for p in pattern:
        if i + p <= N:
            dp[i + p] = min(dp[i + p], dp[i] + 1)
print((dp[N]))

-M = [0] * (N + 1) -M[1] = 1 -for i in range(2, N + 1): -    L = [M[i - 1] + 1] -    j = 0 -    while i - 6**j >= 0: -        L.append(M[i - 6**j] + 1) -        j += 1 -    j = 0 -    while i - 9**j >= 0: -        L.append(M[i - 9**j] + 1) -        j += 1 -    M[i] = min(L) -# print(M) -print((M[N])) +pattern = [1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049] +INF = 10**6 +dp = [INF] * (N + 1) +dp[0] = 0 +for i in range(N + 1): +    for p in pattern: +        if i + p <= N: +            dp[i + p] = min(dp[i + p], dp[i] + 1) +print((dp[N]))