integer N,N_6,N_9
integer ans,preans
read*,N

ans=huge(ans)
do i =0,N
  preans=0
  N_6=i
  do while(N_6/=0)
    preans=preans+mod(N_6,6)
    N_6=N_6/6
  end do
  N_9=N-i
  do while(N_9/=0)
    preans=preans+mod(N_9,9)
    N_9=N_9/9
  end do
  ans=min(ans,preans)
end do

print"(I0)",ans
end
; ModuleID = 'FIRModule'
source_filename = "FIRModule"
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"

@_QQcl.a65beafbb88d9e5b1201865542cb1938 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_5909/source.f\00"
@_QQcl.28493029 = linkonce constant [4 x i8] c"(I0)"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

define void @_QQmain() local_unnamed_addr {
  %1 = alloca i32, align 4
  %2 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.a65beafbb88d9e5b1201865542cb1938, i32 3)
  %3 = call i1 @_FortranAioInputInteger(ptr %2, ptr nonnull %1, i32 4)
  %4 = call i32 @_FortranAioEndIoStatement(ptr %2)
  %5 = load i32, ptr %1, align 4, !tbaa !3
  %6 = icmp ult i32 %5, 2147483647
  br i1 %6, label %.preheader.preheader, label %._crit_edge35

.preheader.preheader:                             ; preds = %0
  %7 = add nuw nsw i32 %5, 1
  %smin = call i32 @llvm.smin.i32(i32 %5, i32 0)
  %8 = sub nuw nsw i32 %7, %smin
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge30
  %.01834 = phi i32 [ %17, %._crit_edge30 ], [ 0, %.preheader.preheader ]
  %.01933 = phi i32 [ %16, %._crit_edge30 ], [ 2147483647, %.preheader.preheader ]
  %.not22 = icmp eq i32 %.01834, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.024 = phi i32 [ %10, %.lr.ph ], [ 0, %.preheader ]
  %.01723 = phi i32 [ %11, %.lr.ph ], [ %.01834, %.preheader ]
  %9 = srem i32 %.01723, 6
  %10 = add i32 %.024, %9
  %11 = sdiv i32 %.01723, 6
  %.01723.off = add i32 %.01723, 5
  %.not = icmp ult i32 %.01723.off, 11
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.0.lcssa = phi i32 [ 0, %.preheader ], [ %10, %.lr.ph ]
  %12 = sub nuw nsw i32 %5, %.01834
  %.not2125 = icmp eq i32 %12, 0
  br i1 %.not2125, label %._crit_edge30, label %.lr.ph29

.lr.ph29:                                         ; preds = %._crit_edge, %.lr.ph29
  %.127 = phi i32 [ %14, %.lr.ph29 ], [ %.0.lcssa, %._crit_edge ]
  %.01626 = phi i32 [ %15, %.lr.ph29 ], [ %12, %._crit_edge ]
  %13 = srem i32 %.01626, 9
  %14 = add i32 %.127, %13
  %15 = sdiv i32 %.01626, 9
  %.01626.off = add i32 %.01626, 8
  %.not21 = icmp ult i32 %.01626.off, 17
  br i1 %.not21, label %._crit_edge30, label %.lr.ph29

._crit_edge30:                                    ; preds = %.lr.ph29, %._crit_edge
  %.1.lcssa = phi i32 [ %.0.lcssa, %._crit_edge ], [ %14, %.lr.ph29 ]
  %16 = call i32 @llvm.smin.i32(i32 %.01933, i32 %.1.lcssa)
  %17 = add nuw i32 %.01834, 1
  %exitcond.not = icmp eq i32 %17, %8
  br i1 %exitcond.not, label %._crit_edge35, label %.preheader

._crit_edge35:                                    ; preds = %._crit_edge30, %0
  %.019.lcssa = phi i32 [ 2147483647, %0 ], [ %16, %._crit_edge30 ]
  %18 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28493029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.a65beafbb88d9e5b1201865542cb1938, i32 21)
  %19 = call i1 @_FortranAioOutputInteger32(ptr %18, i32 %.019.lcssa)
  %20 = call i32 @_FortranAioEndIoStatement(ptr %18)
  ret void
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

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

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
