#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);
}

; ModuleID = '/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_522/source.m'
source_filename = "/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_522/source.m"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [3 x i8] c"%d\00", align 1

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize willreturn memory(argmem: readwrite) uwtable
define dso_local i32 @q(ptr nocapture noundef %n, i32 noundef %m) local_unnamed_addr #0 {
entry:
  %0 = load i32, ptr %n, align 4, !tbaa !5
  %div = sdiv i32 %0, %m
  %mul = mul nsw i32 %div, %m
  %sub.recomposed = srem i32 %0, %m
  store i32 %sub.recomposed, ptr %n, align 4, !tbaa !5
  ret i32 %div
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: minsize nofree nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #2 {
entry:
  %n = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %n) #5
  %call = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %n) #6
  %0 = load i32, ptr %n, align 4, !tbaa !5
  br label %for.cond

for.cond:                                         ; preds = %for.body, %entry
  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  %result.0 = phi i32 [ 2147483647, %entry ], [ %cond, %for.body ]
  %cmp.not = icmp sgt i32 %i.0, %0
  br i1 %cmp.not, label %for.cond.cleanup, label %for.body

for.cond.cleanup:                                 ; preds = %for.cond
  %call25 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %result.0) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %n) #5
  ret i32 0

for.body:                                         ; preds = %for.cond
  %sub = sub nsw i32 %0, %i.0
  %div.i = udiv i32 %i.0, 59049
  %mul.i.neg = mul i32 %div.i, -59049
  %sub.i = add i32 %mul.i.neg, %i.0
  %div.i44 = sdiv i32 %sub.i, 6561
  %mul.i45.neg = mul nsw i32 %div.i44, -6561
  %sub.i46 = add i32 %mul.i45.neg, %sub.i
  %div.i47 = sdiv i32 %sub.i46, 729
  %mul.i48.neg = mul nsw i32 %div.i47, -729
  %sub.i49 = add i32 %mul.i48.neg, %sub.i46
  %div.i50 = sdiv i32 %sub.i49, 81
  %mul.i51.neg = mul nsw i32 %div.i50, -81
  %sub.i52 = add i32 %mul.i51.neg, %sub.i49
  %div.i53 = sdiv i32 %sub.i52, 9
  %mul.i54.neg = mul nsw i32 %div.i53, -9
  %sub.i55 = add i32 %sub.i52, %sub
  %add10 = add i32 %sub.i55, %mul.i54.neg
  %div.i56 = sdiv i32 %add10, 46656
  %mul.i57.neg = mul nsw i32 %div.i56, -46656
  %sub.i58 = add i32 %mul.i57.neg, %add10
  %div.i59 = sdiv i32 %sub.i58, 7776
  %mul.i60.neg = mul nsw i32 %div.i59, -7776
  %sub.i61 = add i32 %mul.i60.neg, %sub.i58
  %div.i62 = sdiv i32 %sub.i61, 1296
  %mul.i63.neg = mul nsw i32 %div.i62, -1296
  %sub.i64 = add i32 %mul.i63.neg, %sub.i61
  %div.i65 = sdiv i32 %sub.i64, 216
  %mul.i66.neg = mul nsw i32 %div.i65, -216
  %sub.i67 = add i32 %mul.i66.neg, %sub.i64
  %div.i68 = sdiv i32 %sub.i67, 36
  %mul.i69.neg = mul nsw i32 %div.i68, -36
  %sub.i70 = add i32 %mul.i69.neg, %sub.i67
  %div.i71 = sdiv i32 %sub.i70, 6
  %mul.i72.neg = mul nsw i32 %div.i71, -6
  %sub.i73 = add nsw i32 %div.i44, %div.i
  %add3 = add nsw i32 %sub.i73, %div.i47
  %add5 = add nsw i32 %add3, %div.i50
  %add7 = add nsw i32 %add5, %div.i53
  %add9 = add nsw i32 %add7, %div.i56
  %add12 = add nsw i32 %add9, %div.i59
  %add14 = add nsw i32 %add12, %div.i62
  %add16 = add nsw i32 %add14, %div.i65
  %add18 = add nsw i32 %add16, %div.i68
  %add20 = add i32 %add18, %sub.i70
  %add22 = add i32 %add20, %div.i71
  %add23 = add i32 %add22, %mul.i72.neg
  %cond = call i32 @llvm.smin.i32(i32 %result.0, i32 %add23)
  %inc = add nuw nsw i32 %i.0, 1
  br label %for.cond
}

; Function Attrs: minsize nofree nounwind optsize
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: minsize nofree nounwind optsize
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #4

attributes #0 = { minsize mustprogress nofree norecurse nosync nounwind optsize willreturn memory(argmem: readwrite) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { minsize nofree nounwind optsize uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { minsize nofree nounwind optsize "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind }
attributes #6 = { minsize optsize }

!llvm.linker.options = !{}
!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"Ubuntu clang version 18.0.0 (++20231007042255+8abb2ace888b-1~exp1~20231007042414.1230)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
