program AGC009A
    implicit none
    integer(8)::i,N,ans
    integer(8),allocatable,dimension(:)::A,B
    read(5,*)N
    allocate(A(N))
    allocate(B(N))
    !末尾から挿入
    read(5,*)(A(N-i),B(N-i),i=0,N-1)
    ans=0

    do i=1,N
        if(i==1)then
            ans=mod(B(i)-mod(A(i),B(i)),B(i))
        else
            A(i)=A(i)+ans
            ans=ans+mod(B(i)-mod(A(i),B(i)),B(i))
        end if
    end do

    print'(i0)',ans
end program AGC009A
; 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"

@_QFEa.0 = internal unnamed_addr global ptr null
@_QFEb.0 = internal unnamed_addr global ptr null
@_QQcl.f10efc98cfc885eab3653ab8eba581cc = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_7544/source.f\00"
@_QQcl.28693029 = linkonce constant [4 x i8] c"(i0)"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

define void @_QQmain() local_unnamed_addr {
  %1 = alloca i64, align 8
  %2 = tail call ptr @_FortranAioBeginExternalListInput(i32 5, ptr nonnull @_QQcl.f10efc98cfc885eab3653ab8eba581cc, i32 5)
  %3 = call i1 @_FortranAioInputInteger(ptr %2, ptr nonnull %1, i32 8)
  %4 = call i32 @_FortranAioEndIoStatement(ptr %2)
  %5 = load i64, ptr %1, align 8, !tbaa !3
  %6 = call i64 @llvm.smax.i64(i64 %5, i64 0)
  %7 = shl i64 %6, 3
  %8 = call ptr @malloc(i64 %7)
  store ptr %8, ptr @_QFEa.0, align 8, !tbaa !7
  %9 = call ptr @malloc(i64 %7)
  store ptr %9, ptr @_QFEb.0, align 8, !tbaa !7
  %10 = call ptr @_FortranAioBeginExternalListInput(i32 5, ptr nonnull @_QQcl.f10efc98cfc885eab3653ab8eba581cc, i32 9)
  %11 = load i64, ptr %1, align 8, !tbaa !3
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.neg304 = phi i64 [ %.neg, %.lr.ph ], [ 0, %0 ]
  %13 = phi i64 [ %22, %.lr.ph ], [ 0, %0 ]
  %.unpack278 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %14 = load i64, ptr %1, align 8, !tbaa !3
  %.neg302 = add i64 %.neg304, -1
  %15 = add i64 %.neg302, %14
  %16 = getelementptr i64, ptr %.unpack278, i64 %15
  %17 = call i1 @_FortranAioInputInteger(ptr %10, ptr %16, i32 8)
  %.unpack290 = load ptr, ptr @_QFEb.0, align 8, !tbaa !7
  %18 = load i64, ptr %1, align 8, !tbaa !3
  %.neg303 = add i64 %.neg304, -1
  %19 = add i64 %.neg303, %18
  %20 = getelementptr i64, ptr %.unpack290, i64 %19
  %21 = call i1 @_FortranAioInputInteger(ptr %10, ptr %20, i32 8)
  %22 = add nuw nsw i64 %13, 1
  %.neg = xor i64 %13, -1
  %exitcond.not = icmp eq i64 %22, %11
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  %23 = call i32 @_FortranAioEndIoStatement(ptr %10)
  %24 = load i64, ptr %1, align 8, !tbaa !3
  %25 = icmp sgt i64 %24, 0
  br i1 %25, label %26, label %._crit_edge308

26:                                               ; preds = %._crit_edge
  %.unpack = load ptr, ptr @_QFEa.0, align 8
  %.unpack230 = load ptr, ptr @_QFEb.0, align 8
  %27 = load i64, ptr %.unpack230, align 8, !tbaa !3
  %28 = load i64, ptr %.unpack, align 8, !tbaa !3
  %29 = srem i64 %28, %27
  %30 = sub i64 %27, %29
  %31 = srem i64 %30, %27
  %exitcond309.peel.not = icmp eq i64 %24, 1
  br i1 %exitcond309.peel.not, label %._crit_edge308, label %.peel.next

.peel.next:                                       ; preds = %26, %.peel.next
  %32 = phi i64 [ %44, %.peel.next ], [ 2, %26 ]
  %.0305 = phi i64 [ %43, %.peel.next ], [ %31, %26 ]
  %33 = add i64 %32, -1
  %34 = getelementptr i64, ptr %.unpack, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !3
  %36 = add i64 %35, %.0305
  store i64 %36, ptr %34, align 8, !tbaa !3
  %37 = add i64 %32, -1
  %38 = getelementptr i64, ptr %.unpack230, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !3
  %40 = srem i64 %36, %39
  %41 = sub i64 %39, %40
  %42 = srem i64 %41, %39
  %43 = add i64 %42, %.0305
  %44 = add nuw i64 %32, 1
  %exitcond309.not = icmp eq i64 %32, %24
  br i1 %exitcond309.not, label %._crit_edge308, label %.peel.next, !llvm.loop !9

._crit_edge308:                                   ; preds = %.peel.next, %26, %._crit_edge
  %.0.lcssa = phi i64 [ 0, %._crit_edge ], [ %31, %26 ], [ %43, %.peel.next ]
  %45 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.f10efc98cfc885eab3653ab8eba581cc, i32 21)
  %46 = call i1 @_FortranAioOutputInteger64(ptr %45, i64 %.0.lcssa)
  %47 = call i32 @_FortranAioEndIoStatement(ptr %45)
  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 @_FortranAioOutputInteger64(ptr, i64) local_unnamed_addr

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

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
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"}
!7 = !{!8, !8, i64 0}
!8 = !{!"descriptor member", !5, i64 0}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.peeled.count", i32 1}
