def main():

    n = int(eval(input()))

    m = [10 ** 5 for _ in range(n + 1)]

    m[0] = 0

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

        j = 0

        while i - 6 ** j >= 0:

            m[i] = min(1 + m[i - 6 ** j], m[i])

            j += 1

        j = 0

        while i - 9 ** j >= 0:

            m[i] = min(1 + m[i - 9 ** j], m[i])

            j += 1



    print((m[-1]))





if __name__ == '__main__':

    main()

def main():

    import bisect

    n = int(eval(input()))

    m = [10 ** 5 for _ in range(n + 1)]

    m[0] = 0

    a = [6 ** i for i in range(7)]

    b = [9 ** i for i in range(6)]

    c = sorted(a + b)

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

        j = bisect.bisect_right(c, i)

        k = [1 + m[i - x] for l, x in enumerate(c) if l < j]

        m[i] = min(k)



    print((m[-1]))





if __name__ == '__main__':

    main()

def main():
    n = int(eval(input()))
    m = [10**5 for _ in range(n + 1)]
    m[0] = 0
    for i in range(1, n + 1):
        j = 0
        while i - 6**j >= 0:
            m[i] = min(1 + m[i - 6**j], m[i])
            j += 1
        j = 0
        while i - 9**j >= 0:
            m[i] = min(1 + m[i - 9**j], m[i])
            j += 1
    print((m[-1]))


if __name__ == "__main__":
    main()

def main():
    import bisect

    n = int(eval(input()))
    m = [10**5 for _ in range(n + 1)]
    m[0] = 0
    a = [6**i for i in range(7)]
    b = [9**i for i in range(6)]
    c = sorted(a + b)
    for i in range(1, n + 1):
        j = bisect.bisect_right(c, i)
        k = [1 + m[i - x] for l, x in enumerate(c) if l < j]
        m[i] = min(k)
    print((m[-1]))


if __name__ == "__main__":
    main()

+    import bisect + +    a = [6**i for i in range(7)] +    b = [9**i for i in range(6)] +    c = sorted(a + b) -        j = 0 -        while i - 6**j >= 0: -            m[i] = min(1 + m[i - 6**j], m[i]) -            j += 1 -        j = 0 -        while i - 9**j >= 0: -            m[i] = min(1 + m[i - 9**j], m[i]) -            j += 1 +        j = bisect.bisect_right(c, i) +        k = [1 + m[i - x] for l, x in enumerate(c) if l < j] +        m[i] = min(k)