#![allow(unused_imports)]
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufWriter, Read, Write};
use std::ops::{Add, Div, Mul, Sub};
use std::str::{self, FromStr};
use std::{cmp, fmt};

fn main() {
    let n: u64 = {
        let mut sc = InputScanOnce::new(io::stdin(), 1024);
        sc.next()
    };
    let mut m = n;
    'a: for d6 in 0..n / 6 + 1 {
        for d9 in 0..n / 9 + 1 {
            let d = d6 * 6 + d9 * 9;
            if n < d {
                continue 'a;
            }
            let c1 = n - d;
            let c6 = f6(d6);
            let c9 = f9(d9);
            m = cmp::min(m, c1 + c6 + c9);
        }
    }
    println!("{}", m);
}

fn f6(mut d: u64) -> u64 {
    let a5 = d / 6u64.pow(5);
    d -= a5 * 6u64.pow(5);
    let a4 = d / 6u64.pow(4);
    d -= a4 * 6u64.pow(4);
    let a3 = d / 6u64.pow(3);
    d -= a3 * 6u64.pow(3);
    let a2 = d / 6u64.pow(2);
    d -= a2 * 6u64.pow(2);
    let a1 = d / 6;
    d -= a1 * 6;
    d + a1 + a2 + a3 + a4 + a5
}

fn f9(mut d: u64) -> u64 {
    let a5 = d / 9u64.pow(5);
    d -= a5 * 9u64.pow(5);
    let a4 = d / 9u64.pow(4);
    d -= a4 * 9u64.pow(4);
    let a3 = d / 9u64.pow(3);
    d -= a3 * 9u64.pow(3);
    let a2 = d / 9u64.pow(2);
    d -= a2 * 9u64.pow(2);
    let a1 = d / 9;
    d -= a1 * 9;
    d + a1 + a2 + a3 + a4 + a5
}

struct InputScanOnce {
    buf: Vec<u8>,
    pos: usize,
}

#[allow(dead_code)]
impl InputScanOnce {
    fn new<R: Read>(mut reader: R, estimated: usize) -> Self {
        let mut buf = Vec::with_capacity(estimated);
        let _ = io::copy(&mut reader, &mut buf).unwrap();
        InputScanOnce { buf: buf, pos: 0 }
    }

    #[inline]
    fn next<T: FromStr>(&mut self) -> T
    where
        T::Err: fmt::Debug,
    {
        let mut start = None;
        loop {
            match (self.buf[self.pos], start.is_some()) {
                (b' ', true) | (b'\n', true) => break,
                (_, true) | (b' ', false) | (b'\n', false) => self.pos += 1,
                (_, false) => start = Some(self.pos),
            }
        }
        let target = &self.buf[start.unwrap()..self.pos];
        unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where
        T::Err: fmt::Debug,
    {
        (0..n).map(|_| self.next()).collect()
    }

    fn pairs<T1: FromStr, T2: FromStr>(&mut self, n: usize) -> Vec<(T1, T2)>
    where
        T1::Err: fmt::Debug,
        T2::Err: fmt::Debug,
    {
        (0..n).map(|_| (self.next(), self.next())).collect()
    }

    fn trios<T1: FromStr, T2: FromStr, T3: FromStr>(&mut self, n: usize) -> Vec<(T1, T2, T3)>
    where
        T1::Err: fmt::Debug,
        T2::Err: fmt::Debug,
        T3::Err: fmt::Debug,
    {
        (0..n)
            .map(|_| (self.next(), self.next(), self.next()))
            .collect()
    }

    fn mat<T: FromStr>(&mut self, m: usize, n: usize) -> Vec<Vec<T>>
    where
        T::Err: fmt::Debug,
    {
        (0..m).map(|_| self.vec(n)).collect()
    }

    fn strings_as_mat<T, F: FnMut(u8) -> T>(&mut self, h: usize, mut f: F) -> Vec<Vec<T>> {
        (0..h)
            .map(|_| {
                let l = self.next::<String>();
                l.as_bytes().iter().cloned().map(&mut f).collect()
            })
            .collect()
    }
}


Measured memory_bytes: 4352