#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: mustprogress nofree norecurse nosync nounwind 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: nofree nounwind 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)
  %0 = load i32, ptr %n, align 4, !tbaa !5
  %cmp.not86 = icmp slt i32 %0, 0
  br i1 %cmp.not86, label %for.cond.cleanup, label %for.body.preheader

for.body.preheader:                               ; preds = %entry
  %1 = add i32 %0, 1
  %min.iters.check = icmp ult i32 %1, 4
  br i1 %min.iters.check, label %for.body.preheader89, label %vector.ph

vector.ph:                                        ; preds = %for.body.preheader
  %n.vec = and i32 %1, -4
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %0, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 2147483647, i32 2147483647, i32 2147483647, i32 2147483647>, %vector.ph ], [ %48, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %2 = sub nsw <4 x i32> %broadcast.splat, %vec.ind
  %3 = udiv <4 x i32> %vec.ind, <i32 59049, i32 59049, i32 59049, i32 59049>
  %4 = mul <4 x i32> %3, <i32 -59049, i32 -59049, i32 -59049, i32 -59049>
  %5 = add <4 x i32> %4, %vec.ind
  %6 = sdiv <4 x i32> %5, <i32 6561, i32 6561, i32 6561, i32 6561>
  %7 = mul nsw <4 x i32> %6, <i32 -6561, i32 -6561, i32 -6561, i32 -6561>
  %8 = add <4 x i32> %7, %5
  %9 = sdiv <4 x i32> %8, <i32 729, i32 729, i32 729, i32 729>
  %10 = mul nsw <4 x i32> %9, <i32 -729, i32 -729, i32 -729, i32 -729>
  %11 = add <4 x i32> %10, %8
  %12 = sdiv <4 x i32> %11, <i32 81, i32 81, i32 81, i32 81>
  %13 = mul nsw <4 x i32> %12, <i32 -81, i32 -81, i32 -81, i32 -81>
  %14 = add <4 x i32> %13, %11
  %15 = sdiv <4 x i32> %14, <i32 9, i32 9, i32 9, i32 9>
  %16 = mul nsw <4 x i32> %15, <i32 -9, i32 -9, i32 -9, i32 -9>
  %17 = add <4 x i32> %14, %2
  %18 = add <4 x i32> %17, %16
  %19 = sdiv <4 x i32> %18, <i32 46656, i32 46656, i32 46656, i32 46656>
  %20 = mul nsw <4 x i32> %19, <i32 -46656, i32 -46656, i32 -46656, i32 -46656>
  %21 = add <4 x i32> %20, %18
  %22 = sdiv <4 x i32> %21, <i32 7776, i32 7776, i32 7776, i32 7776>
  %23 = mul nsw <4 x i32> %22, <i32 -7776, i32 -7776, i32 -7776, i32 -7776>
  %24 = add <4 x i32> %23, %21
  %25 = sdiv <4 x i32> %24, <i32 1296, i32 1296, i32 1296, i32 1296>
  %26 = mul nsw <4 x i32> %25, <i32 -1296, i32 -1296, i32 -1296, i32 -1296>
  %27 = add <4 x i32> %26, %24
  %28 = sdiv <4 x i32> %27, <i32 216, i32 216, i32 216, i32 216>
  %29 = mul nsw <4 x i32> %28, <i32 -216, i32 -216, i32 -216, i32 -216>
  %30 = add <4 x i32> %29, %27
  %31 = sdiv <4 x i32> %30, <i32 36, i32 36, i32 36, i32 36>
  %32 = mul nsw <4 x i32> %31, <i32 -36, i32 -36, i32 -36, i32 -36>
  %33 = add <4 x i32> %32, %30
  %34 = sdiv <4 x i32> %33, <i32 6, i32 6, i32 6, i32 6>
  %35 = mul nsw <4 x i32> %34, <i32 -6, i32 -6, i32 -6, i32 -6>
  %36 = add nsw <4 x i32> %6, %3
  %37 = add nsw <4 x i32> %36, %9
  %38 = add nsw <4 x i32> %37, %12
  %39 = add nsw <4 x i32> %38, %15
  %40 = add nsw <4 x i32> %39, %19
  %41 = add nsw <4 x i32> %40, %22
  %42 = add nsw <4 x i32> %41, %25
  %43 = add nsw <4 x i32> %42, %28
  %44 = add nsw <4 x i32> %43, %31
  %45 = add <4 x i32> %44, %33
  %46 = add <4 x i32> %45, %34
  %47 = add <4 x i32> %46, %35
  %48 = call <4 x i32> @llvm.smin.v4i32(<4 x i32> %vec.phi, <4 x i32> %47)
  %index.next = add nuw i32 %index, 4
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %49 = icmp eq i32 %index.next, %n.vec
  br i1 %49, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %50 = call i32 @llvm.vector.reduce.smin.v4i32(<4 x i32> %48)
  %cmp.n = icmp eq i32 %1, %n.vec
  br i1 %cmp.n, label %for.cond.cleanup, label %for.body.preheader89

for.body.preheader89:                             ; preds = %for.body.preheader, %middle.block
  %result.088.ph = phi i32 [ 2147483647, %for.body.preheader ], [ %50, %middle.block ]
  %i.087.ph = phi i32 [ 0, %for.body.preheader ], [ %n.vec, %middle.block ]
  br label %for.body

for.cond.cleanup:                                 ; preds = %for.body, %middle.block, %entry
  %result.0.lcssa = phi i32 [ 2147483647, %entry ], [ %50, %middle.block ], [ %cond, %for.body ]
  %call25 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %result.0.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %n) #5
  ret i32 0

for.body:                                         ; preds = %for.body.preheader89, %for.body
  %result.088 = phi i32 [ %cond, %for.body ], [ %result.088.ph, %for.body.preheader89 ]
  %i.087 = phi i32 [ %inc, %for.body ], [ %i.087.ph, %for.body.preheader89 ]
  %sub = sub nsw i32 %0, %i.087
  %div.i = udiv i32 %i.087, 59049
  %mul.i.neg = mul i32 %div.i, -59049
  %sub.i = add i32 %mul.i.neg, %i.087
  %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.088, i32 %add23)
  %inc = add nuw i32 %i.087, 1
  %exitcond.not = icmp eq i32 %i.087, %0
  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body, !llvm.loop !12
}

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

; Function Attrs: nofree nounwind
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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smin.v4i32(<4 x i32>) #4

attributes #0 = { mustprogress nofree norecurse nosync nounwind 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 = { nofree nounwind 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 = { nofree nounwind "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 }

!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"}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !11, !10}
