n = int(input())

li = [1,6,9,36,81,216,729,1296,6561,7776,46656,59049]
for i in range(len(li)):
  if i != 0:
    if li[i - 1] <= n < li[i]:
      a = li[:i]
coli = [0]*len(a)
for j in range(len(a) - 1,-1,-1):
  coli[j] += n // a[j]
  res = n % a[j]
  if res == 0:
      break
  for k in range(j - 1,-1,-1):
    coli[j] += res // a[k]
    res = res % a[k]
    if res == 0:
      break
print(min(coli))

### FIXED CODE ###


n = int(input())

ark = float("INF")
if n < 50000:
    for l in range(2):  # 59049
        abc = (n - (59049*l))
        if abc < 0:
            break
        for k in range(3):  # 46656
            abc = (n - (46656 * k+59049*l))
            if abc < 0:
                break
            for j in range(6):  # 7776
                abc = (n - (776 * j + 46656 * k+59049*l))
                if abc < 0:
                    break
                for h in range(4):  # 1296,6
                    abc = (n - (1296 * h+ 7776 * j + 46656 * k+59049*l))
                    if abc < 0:
                        break
                    for g in range(6):  # 729,9
                        abc = (n - (729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                        if abc < 0:
                            break
                        for f in range(4):#216,5(4)
                            abc = (n - (216 * f+ 729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                            if abc < 0:
                                break
                            for e in range(4):  # 81
                                abc = (n - (81 * e + 216 * f + 729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                                if abc < 0:
                                    break
                                for d in range(5):  # 36
                                    abc = (n - (36 * d + 81 * e + 216 * f + 729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                                    if abc < 0:
                                        break
                                    for c in range(4):  # 9
                                        abc = (n - (9 * c + 36 * d + 81 * e + 216 * f + 729 * g + 1296 * h + 7776 * j + 46656 * k+59049*l))
                                        if abc < 0:
                                            break
                                        for b in range(3):  # 6
                                            abc = (n - (6 * b + 9 * c + 36 * d + 81 * e + 216 * f + 729 * g + 1296 * h + 7776 * j + 46656 * k+59049*l))
                                            if abc < 0:
                                                break
                                            for a in range(6):  # 1
                                                abc = (n - (1*a+6*b+9*c+36*d+81*e+216*f+729*g+1296*h+7776*j+46656*k+59049*l))
                                                if abc < 0:
                                                    break
                                                if abc % 6561 == 0 and abc >= 0:
                                                    i = abc//6561
                                                    if ark > a+b+c+d+e+f+g+h+i+j+k+l:
                                                        ark = a+b+c+d+e+f+g+h+i+j+k+l
else:
    for l in range(2):  # 59049
        abc = (n - (59049*l))
        if abc < 0:
            break
        for k in range(3):  # 46656
            abc = (n - (46656 * k+59049*l))
            if abc < 0:
                break
            for j in range(6):  # 7776
                abc = (n - (776 * j + 46656 * k+59049*l))
                if abc < 0:
                    break
                for h in range(3):  # 1296,6
                    abc = (n - (1296 * h+ 7776 * j + 46656 * k+59049*l))
                    if abc < 0:
                        break
                    for g in range(4):  # 729,9
                        abc = (n - (729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                        if abc < 0:
                            break
                        for f in range(3):#216,5(4)
                            abc = (n - (216 * f+ 729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                            if abc < 0:
                                break
                            for e in range(4):  # 81
                                abc = (n - (81 * e + 216 * f + 729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                                if abc < 0:
                                    break
                                for d in range(5):  # 36
                                    abc = (n - (36 * d + 81 * e + 216 * f + 729 * g + 1296 * h+ 7776 * j + 46656 * k+59049*l))
                                    if abc < 0:
                                        break
                                    for c in range(4):  # 9
                                        abc = (n - (9 * c + 36 * d + 81 * e + 216 * f + 729 * g + 1296 * h + 7776 * j + 46656 * k+59049*l))
                                        if abc < 0:
                                            break
                                        for b in range(3):  # 6
                                            abc = (n - (6 * b + 9 * c + 36 * d + 81 * e + 216 * f + 729 * g + 1296 * h + 7776 * j + 46656 * k+59049*l))
                                            if abc < 0:
                                                break
                                            for a in range(6):  # 1
                                                abc = (n - (1*a+6*b+9*c+36*d+81*e+216*f+729*g+1296*h+7776*j+46656*k+59049*l))
                                                if abc < 0:
                                                    break
                                                if abc % 6561 == 0 and abc >= 0:
                                                    i = abc//6561
                                                    if ark > a+b+c+d+e+f+g+h+i+j+k+l:
                                                        ark = a+b+c+d+e+f+g+h+i+j+k+l
print(ark)