program double_factorization
    implicit none
    integer(kind=8) :: n
    integer(kind=8) :: ans

    read(*, *) n
    ans = min(g2(n, 2), g2(n, 5))
    print *, ans


contains
    recursive integer(kind=8) function g1(n, p) result(res)
        implicit none
        integer(kind=8), intent(in) :: n
        integer(kind=4), intent(in) :: p
        
        if(n == 0) then
            res = 0
        else
            res = n/p + g1(n/p, p)
        endif
   
    end function g1

    recursive integer(kind=8) function g2(n, p) result(res)
        implicit none
        integer(kind=8), intent(in) :: n
        integer(kind=4), intent(in) :: p

        res = 0

        if(mod(n, 2) == 1) then
            res = g1(n, p) - g2(n-1, p)
        else
            res = g1(n/2, p)

            if(p == 2) then
                res = res + n/2
            endif
        endif

    end function g2

end program double_factorization
; 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.165c75a6f272ba29c29f80193152226f = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_2836/source.f\00"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

define void @_QQmain() local_unnamed_addr {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.165c75a6f272ba29c29f80193152226f, i32 6)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %3, i32 8)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %4)
  store i32 2, ptr %2, align 4, !tbaa !3
  %7 = call i64 @_QFPg2(ptr nonnull %3, ptr nonnull %2)
  store i32 5, ptr %1, align 4, !tbaa !3
  %8 = call i64 @_QFPg2(ptr nonnull %3, ptr nonnull %1)
  %9 = call i64 @llvm.smin.i64(i64 %7, i64 %8)
  %10 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.165c75a6f272ba29c29f80193152226f, i32 8)
  %11 = call i1 @_FortranAioOutputInteger64(ptr %10, i64 %9)
  %12 = call i32 @_FortranAioEndIoStatement(ptr %10)
  ret void
}

; Function Attrs: nofree nosync nounwind memory(argmem: read)
define i64 @_QFPg1(ptr nocapture readonly %0, ptr nocapture readonly %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = load i64, ptr %0, align 8, !tbaa !3
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %12, label %6

6:                                                ; preds = %2
  %7 = load i32, ptr %1, align 4, !tbaa !3
  %8 = sext i32 %7 to i64
  %9 = sdiv i64 %4, %8
  store i64 %9, ptr %3, align 8, !tbaa !3
  %10 = call i64 @_QFPg1(ptr nonnull %3, ptr nonnull %1)
  %11 = add i64 %10, %9
  br label %12

12:                                               ; preds = %2, %6
  %.0 = phi i64 [ %11, %6 ], [ 0, %2 ]
  ret i64 %.0
}

; Function Attrs: nofree nosync nounwind memory(argmem: read)
define i64 @_QFPg2(ptr nocapture readonly %0, ptr nocapture readonly %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = load i64, ptr %0, align 8, !tbaa !3
  %6 = and i64 %5, -9223372036854775807
  %7 = icmp eq i64 %6, 1
  br i1 %7, label %8, label %13

8:                                                ; preds = %2
  %9 = tail call i64 @_QFPg1(ptr nonnull %0, ptr %1)
  %10 = add nsw i64 %5, -1
  store i64 %10, ptr %4, align 8, !tbaa !3
  %11 = call i64 @_QFPg2(ptr nonnull %4, ptr %1)
  %12 = sub i64 %9, %11
  br label %19

13:                                               ; preds = %2
  %14 = sdiv i64 %5, 2
  store i64 %14, ptr %3, align 8, !tbaa !3
  %15 = call i64 @_QFPg1(ptr nonnull %3, ptr %1)
  %16 = load i32, ptr %1, align 4, !tbaa !3
  %17 = icmp eq i32 %16, 2
  %18 = select i1 %17, i64 %14, i64 0
  %spec.select = add i64 %18, %15
  br label %19

19:                                               ; preds = %13, %8
  %.0 = phi i64 [ %12, %8 ], [ %spec.select, %13 ]
  ret i64 %.0
}

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 @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger64(ptr, i64) local_unnamed_addr

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

attributes #0 = { nofree nosync nounwind memory(argmem: read) }
attributes #1 = { 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"}
