n=int(eval(input()))



k9=5

k6=6

n9=9**k9

smin=n

for i9 in range(n9):

    ii9=i9

    s=0

    m=0

    astr=""

    for ii in range(1,k9+2):

        r9=ii9%9

        ii9=(ii9-r9)//9    

        s=s*9+r9

        m=m+r9

        astr=astr+str(r9)

#    print("i9",i9,"astr",astr,"s",s,"m",m)

    

    nr=n-s

    nr1=nr

#    print("nr",nr,"m",m)

    

    if nr==0:

        snra=m

#        print(nr,m)

    elif nr>0:

        snra=m

        for jj in range(k6,-1,-1):

            nra=nr1//(6**jj)

            nr1=nr1%(6**jj)

            snra=snra+nra

#        print("jj",jj,"nra",nra,"nr1",nr1)

    if smin>snra:

        smin=snra

print(smin)

n=int(eval(input()))



res=n

for i in range(n+1):

    cc=0

    t=i

    while t>0:

        cc=cc+t%6

        t=t//6

    t=n-i

    while t>0:

        cc=cc+t%9

        t=t//9

    if res>cc:

        res=cc



print(res)

n = int(eval(input()))
k9 = 5
k6 = 6
n9 = 9**k9
smin = n
for i9 in range(n9):
    ii9 = i9
    s = 0
    m = 0
    astr = ""
    for ii in range(1, k9 + 2):
        r9 = ii9 % 9
        ii9 = (ii9 - r9) // 9
        s = s * 9 + r9
        m = m + r9
        astr = astr + str(r9)
    #    print("i9",i9,"astr",astr,"s",s,"m",m)
    nr = n - s
    nr1 = nr
    #    print("nr",nr,"m",m)
    if nr == 0:
        snra = m
    #        print(nr,m)
    elif nr > 0:
        snra = m
        for jj in range(k6, -1, -1):
            nra = nr1 // (6**jj)
            nr1 = nr1 % (6**jj)
            snra = snra + nra
    #        print("jj",jj,"nra",nra,"nr1",nr1)
    if smin > snra:
        smin = snra
print(smin)

n = int(eval(input()))
res = n
for i in range(n + 1):
    cc = 0
    t = i
    while t > 0:
        cc = cc + t % 6
        t = t // 6
    t = n - i
    while t > 0:
        cc = cc + t % 9
        t = t // 9
    if res > cc:
        res = cc
print(res)

-k9 = 5 -k6 = 6 -n9 = 9**k9 -smin = n -for i9 in range(n9): -    ii9 = i9 -    s = 0 -    m = 0 -    astr = "" -    for ii in range(1, k9 + 2): -        r9 = ii9 % 9 -        ii9 = (ii9 - r9) // 9 -        s = s * 9 + r9 -        m = m + r9 -        astr = astr + str(r9) -    #    print("i9",i9,"astr",astr,"s",s,"m",m) -    nr = n - s -    nr1 = nr -    #    print("nr",nr,"m",m) -    if nr == 0: -        snra = m -    #        print(nr,m) -    elif nr > 0: -        snra = m -        for jj in range(k6, -1, -1): -            nra = nr1 // (6**jj) -            nr1 = nr1 % (6**jj) -            snra = snra + nra -    #        print("jj",jj,"nra",nra,"nr1",nr1) -    if smin > snra: -        smin = snra -print(smin) +res = n +for i in range(n + 1): +    cc = 0 +    t = i +    while t > 0: +        cc = cc + t % 6 +        t = t // 6 +    t = n - i +    while t > 0: +        cc = cc + t % 9 +        t = t // 9 +    if res > cc: +        res = cc +print(res)