N=int(eval(input()))

ans=N

for i in range(N+1):

  cnt=0

  t=i

  while t>0:

    cnt+=t%6

    t//=6

  j=N-i

  while j>0:

    cnt+=j%9

    j//=9

  ans=min(ans,cnt)

print(ans)

N=int(eval(input()))

dp=[0]*(100000+1)

a=1

b=6

c=9

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

  if i>=b*6:

    b*=6

  if i>=c*9:

    c*=9

  if i<6:

    dp[i]=i

  elif i<9:

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

  else:

    dp[i]=min([dp[i-a]+1,dp[i-b]+1,dp[i-c]+1])

print((dp[N]))

N = int(eval(input()))
ans = N
for i in range(N + 1):
    cnt = 0
    t = i
    while t > 0:
        cnt += t % 6
        t //= 6
    j = N - i
    while j > 0:
        cnt += j % 9
        j //= 9
    ans = min(ans, cnt)
print(ans)

N = int(eval(input()))
dp = [0] * (100000 + 1)
a = 1
b = 6
c = 9
for i in range(1, N + 1):
    if i >= b * 6:
        b *= 6
    if i >= c * 9:
        c *= 9
    if i < 6:
        dp[i] = i
    elif i < 9:
        dp[i] = min(dp[i - a] + 1, dp[i - b] + 1)
    else:
        dp[i] = min([dp[i - a] + 1, dp[i - b] + 1, dp[i - c] + 1])
print((dp[N]))

-ans = N -for i in range(N + 1): -    cnt = 0 -    t = i -    while t > 0: -        cnt += t % 6 -        t //= 6 -    j = N - i -    while j > 0: -        cnt += j % 9 -        j //= 9 -    ans = min(ans, cnt) -print(ans) +dp = [0] * (100000 + 1) +a = 1 +b = 6 +c = 9 +for i in range(1, N + 1): +    if i >= b * 6: +        b *= 6 +    if i >= c * 9: +        c *= 9 +    if i < 6: +        dp[i] = i +    elif i < 9: +        dp[i] = min(dp[i - a] + 1, dp[i - b] + 1) +    else: +        dp[i] = min([dp[i - a] + 1, dp[i - b] + 1, dp[i - c] + 1]) +print((dp[N]))