#!usr/bin/env python3

from collections import defaultdict

from collections import deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

def LI(): return list(map(int, sys.stdin.readline().split()))

def I(): return int(sys.stdin.readline())

def LS():return list(map(list, sys.stdin.readline().split()))

def S(): return list(sys.stdin.readline())[:-1]

def IR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = I()

    return l

def LIR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LI()

    return l

def SR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = S()

    return l

def LSR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = SR()

    return l

mod = 1000000007



#A

def A():

    return



#B

def B():

    return



#C

def C():

    n = I()

    ans = float("inf")

    for i in range(n+1):

        m = 0

        k = i

        while k > 0:

            m += k%6

            k //= 6

        k = n-i

        while k > 0:

            m += k%9

            k //= 9

        ans = min(ans,m)

    print(ans)

#D

def D():

    return



#E

def E():

    return



#F

def F():

    return



#G

def G():

    return



#H

def H():

    return



#Solve

if __name__ == "__main__":

    C()

#!usr/bin/env python3

from collections import defaultdict

from collections import deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

def LI(): return list(map(int, sys.stdin.readline().split()))

def I(): return int(sys.stdin.readline())

def LS():return list(map(list, sys.stdin.readline().split()))

def S(): return list(sys.stdin.readline())[:-1]

def IR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = I()

    return l

def LIR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LI()

    return l

def SR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = S()

    return l

def LSR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LS()

    return l

sys.setrecursionlimit(1000000)

mod = 1000000007



#A

def A():

    n = I()

    ans = float("inf")

    for i in range(n+1):

        m = 0

        k = i

        while k > 0:

            m += k%6

            k //= 6

        k = n-i

        while k > 0:

            m += k%9

            k //= 9

        if m < ans:

            ans = m

    print(ans)

    return



#B

def B():

    return



#C

def C():

    return



#D

def D():

    return



#E

def E():

    return



#F

def F():

    return



#G

def G():

    return



#H

def H():

    return



#I

def I_():

    return



#J

def J():

    return



#Solve

if __name__ == "__main__":

    A()

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random


def LI():
    return list(map(int, sys.stdin.readline().split()))


def I():
    return int(sys.stdin.readline())


def LS():
    return list(map(list, sys.stdin.readline().split()))


def S():
    return list(sys.stdin.readline())[:-1]


def IR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = I()
    return l


def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = LI()
    return l


def SR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = S()
    return l


def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = SR()
    return l


mod = 1000000007
# A
def A():
    return


# B
def B():
    return


# C
def C():
    n = I()
    ans = float("inf")
    for i in range(n + 1):
        m = 0
        k = i
        while k > 0:
            m += k % 6
            k //= 6
        k = n - i
        while k > 0:
            m += k % 9
            k //= 9
        ans = min(ans, m)
    print(ans)


# D
def D():
    return


# E
def E():
    return


# F
def F():
    return


# G
def G():
    return


# H
def H():
    return


# Solve
if __name__ == "__main__":
    C()

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random


def LI():
    return list(map(int, sys.stdin.readline().split()))


def I():
    return int(sys.stdin.readline())


def LS():
    return list(map(list, sys.stdin.readline().split()))


def S():
    return list(sys.stdin.readline())[:-1]


def IR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = I()
    return l


def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = LI()
    return l


def SR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = S()
    return l


def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = LS()
    return l


sys.setrecursionlimit(1000000)
mod = 1000000007
# A
def A():
    n = I()
    ans = float("inf")
    for i in range(n + 1):
        m = 0
        k = i
        while k > 0:
            m += k % 6
            k //= 6
        k = n - i
        while k > 0:
            m += k % 9
            k //= 9
        if m < ans:
            ans = m
    print(ans)
    return


# B
def B():
    return


# C
def C():
    return


# D
def D():
    return


# E
def E():
    return


# F
def F():
    return


# G
def G():
    return


# H
def H():
    return


# I
def I_():
    return


# J
def J():
    return


# Solve
if __name__ == "__main__":
    A()

-        l[i] = SR() +        l[i] = LS() +sys.setrecursionlimit(1000000) -    return - - -# B -def B(): -    return - - -# C -def C(): -        ans = min(ans, m) +        if m < ans: +            ans = m +    return + + +# B +def B(): +    return + + +# C +def C(): +    return +# I +def I_(): +    return + + +# J +def J(): +    return + + -    C() +    A()