#import <stdio.h>
#import <limits.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
int q(int *n, int m){
    int r = *n/m;
    *n -= m*r;
    return r;
}
int main(){
    int n;
    scanf("%d",&n);
    int result = INT_MAX;
    for(int i=0;i<=n;i++){
        int I = i;
        int N = n-i;
        int count = 0;
        count += q(&I, 59049);
        count += q(&I, 6561);
        count += q(&I, 729);
        count += q(&I, 81);
        count += q(&I, 9);
        
        N += I;

        count += q(&N, 46656);
        count += q(&N, 7776);
        count += q(&N, 1296);
        count += q(&N, 216);
        count += q(&N, 36);
        count += q(&N, 6);
        
        count += N;
        
        result = MIN(result,count);
    }
    printf("%d", result);
}
