### BUGGY CODE ###

#ABC099C
N=int(input())
#1:6 2:36 3:216 4:1296 5:7776 6:46656 7:9 8:81 9:729 10:6561 11:59049
#dv9=[59049, 6561, 729, 81, 9]
#dv6=[46656, 7776, 1926, 216, 36, 6]
#dv=[6,36,216,1296,7776,46656,9,81,729,6561,59049]
lm9=[2, 9, 9, 9, 9]
lm6=[3, 6, 6, 6, 6, 6]
#ans=[0]*11
ans=N
sums=0
for i in range(lm9[0]):
    sumsi=i*59049
    if sumsi>N:
        break
    else:
        for j in range(lm6[0]):
            sumsj=j*46656
            if sumsj+sumsi>N:
                break
            else:
                for k in range(lm6[1]):
                    sumsk=k*7776
                    if sumsk+sumsj+sumsi>N:
                        break
                    else:
                        for l in range(lm9[1]):
                            sumsl=l*6561
                            if sumsl+sumsk+sumsj+sumsi>N:
                                break
                            else:
                                for m in range(lm6[2]):
                                    sumsm=m*1296
                                    if sumsm+sumsl+sumsk+sumsj+sumsi>N:
                                        break
                                    else:
                                        for n in range(lm9[2]):
                                            sumsn=n*729
                                            if sumsn+sumsm+sumsl+sumsk+sumsj+sumsi>N:
                                                break
                                            else:
                                                for o in range(lm6[3]):
                                                    sumso=o*216
                                                    if sumso + sumsn + sumsm + sumsl + sumsk + sumsj + sumsi>N:
                                                        break
                                                    else:
                                                        for p in range(lm9[3]):
                                                            sumsp=p*81
                                                            if sumsp + sumso + sumsn + sumsm + sumsl + sumsk + sumsj + sumsi>N:
                                                                break
                                                            else:
                                                                for q in range(lm6[4]):
                                                                    sumsq=q*36
                                                                    if sumsq + sumsp + sumso + sumsn + sumsm + sumsl + sumsk + sumsj + sumsi>N:
                                                                        break
                                                                    else:
                                                                        for r in range(lm9[4]):
                                                                            sumsr=r*9
                                                                            if sumsr + sumsq + sumsp + sumso + sumsn + sumsm + sumsl + sumsk + sumsj + sumsi>N:
                                                                                break
                                                                            else:
                                                                                for s in range(lm6[5]):
                                                                                    sumss=s*6
                                                                                    sums = sumss + sumsr + sumsq + sumsp + sumso + sumsn + sumsm + sumsl + sumsk + sumsj + sumsi
                                                                                    if sums>N:
                                                                                        break
                                                                                    else:
                                                                                        ans_tmp=N-sums+i+j+k+l+m+n+o+p+q+r+s
                                                                                    if ans_tmp<ans:
                                                                                        ans=ans_tmp
print(ans)

### FIXED CODE ###

#ABC099C
N=int(input())
i=0
t=0
ans=N
for i in range(N+1):
    nine = 0
    six = 0
    t=i
    while (t>0):
        nine+=t%9
        t//=9
    t=N-i
    while (t>0):
        six+=t%6
        t//=6
    if ans>nine+six:
        ans=nine+six
print(ans)