PROGRAM ATCODER

implicit none
integer(16) :: n,i,j,count=0,count_a=0,count_b=0,count_ab=0
character(len=10),allocatable :: s(:)

read*, n
allocate(s(n))
read*, s(1:n)

do i = 1, n
  do j = 1, len_trim(s(i))-1
    if ( s(i)(j:j+1) == 'AB' ) then
      count = count + 1
    end if
  end do
  if ( s(i)(1:1) == 'B' ) then
    count_b = count_b + 1
  end if   
  if ( s(i)(len_trim(s(i)):len_trim(s(i))) == 'A' ) then
    count_a = count_a + 1
  end if
  if ( s(i)(1:1) == 'B' .and. s(i)(len_trim(s(i)):len_trim(s(i))) == 'A' ) then
    count_ab = count_ab + 1
  end if
end do

count_a = count_a - count_ab
count_b = count_b - count_ab

if ( count_a == 0 .and. count_b == 0 .and. count_ab == 0 ) then
  print'(i0)', count
  stop
end if

if ( count_a == 0 .and. count_b == 0 ) then
  print'(i0)',  count + count_ab - 1
else
  print'(i0)',  count + min(count_a,count_b) + count_ab
end if

END PROGRAM ATCODER

; 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"

@_QFEcount = internal unnamed_addr global i128 0
@_QFEcount_a = internal unnamed_addr global i128 0
@_QFEcount_ab = internal unnamed_addr global i128 0
@_QFEcount_b = internal unnamed_addr global i128 0
@_QFEs.0 = internal unnamed_addr global ptr null
@_QQcl.0c2b23b9cdb72dfc72e8ca75446d8f8f = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_4325/source.f\00"
@_QQcl.4142 = linkonce constant [2 x i8] c"AB"
@_QQcl.42 = linkonce constant [1 x i8] c"B"
@_QQcl.41 = linkonce constant [1 x i8] c"A"
@_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 { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca i128, align 8
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.0c2b23b9cdb72dfc72e8ca75446d8f8f, i32 7)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 16)
  %5 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %6 = load i128, ptr %2, align 8, !tbaa !3
  %7 = trunc i128 %6 to i64
  %8 = call i64 @llvm.smax.i64(i64 %7, i64 0)
  %9 = mul i64 %8, 10
  %10 = call ptr @malloc(i64 %9)
  store ptr %10, ptr @_QFEs.0, align 8, !tbaa !7
  %11 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.0c2b23b9cdb72dfc72e8ca75446d8f8f, i32 9)
  %.unpack = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %12 = load i128, ptr %2, align 8, !tbaa !3
  %13 = trunc i128 %12 to i64
  %14 = call i64 @llvm.smax.i64(i64 %13, i64 0)
  store ptr %.unpack, ptr %1, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 1
  store i64 10, ptr %.fca.1.gep, align 8, !tbaa !7
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 3
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 4
  store i8 40, ptr %.fca.4.gep, align 1, !tbaa !7
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 1
  store i64 %14, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 2
  store i64 10, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %15 = call i1 @_FortranAioInputDescriptor(ptr %11, ptr nonnull %1)
  %16 = call i32 @_FortranAioEndIoStatement(ptr %11)
  %17 = load i128, ptr %2, align 8, !tbaa !3
  %18 = trunc i128 %17 to i64
  %19 = icmp sgt i64 %18, 0
  br i1 %19, label %.lr.ph424, label %._crit_edge

.lr.ph424:                                        ; preds = %0, %125
  %20 = phi i64 [ %126, %125 ], [ 1, %0 ]
  %.unpack314 = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %21 = add i64 %20, -1
  %22 = getelementptr [10 x i8], ptr %.unpack314, i64 %21
  br label %23

23:                                               ; preds = %.lr.ph424, %23
  %24 = phi i64 [ 9, %.lr.ph424 ], [ %28, %23 ]
  %25 = getelementptr [10 x [1 x i8]], ptr %22, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !3
  %27 = icmp eq i8 %26, 32
  %28 = add nsw i64 %24, -1
  %29 = icmp ne i64 %24, 0
  %30 = and i1 %27, %29
  br i1 %30, label %23, label %31

31:                                               ; preds = %23
  %sext = shl i64 %24, 32
  %32 = ashr exact i64 %sext, 32
  %spec.select = select i1 %27, i64 -1, i64 %32
  %33 = icmp sgt i64 %spec.select, 0
  br i1 %33, label %.lr.ph.preheader, label %select.unfold._crit_edge

.lr.ph.preheader:                                 ; preds = %31
  %34 = add i64 %20, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %select.unfold
  %35 = phi i64 [ %37, %select.unfold ], [ 1, %.lr.ph.preheader ]
  %.unpack402 = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %36 = getelementptr [10 x i8], ptr %.unpack402, i64 %34
  %37 = add nuw nsw i64 %35, 1
  %38 = add nsw i64 %35, -1
  %39 = getelementptr [10 x [1 x i8]], ptr %36, i64 0, i64 %38
  %40 = call i32 @_FortranACharacterCompareScalar1(ptr %39, ptr nonnull @_QQcl.4142, i64 2, i64 2)
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %select.unfold

42:                                               ; preds = %.lr.ph
  %43 = load i128, ptr @_QFEcount, align 8, !tbaa !3
  %44 = add i128 %43, 1
  store i128 %44, ptr @_QFEcount, align 8, !tbaa !3
  br label %select.unfold

select.unfold:                                    ; preds = %42, %.lr.ph
  %exitcond.not = icmp eq i64 %35, %spec.select
  br i1 %exitcond.not, label %select.unfold._crit_edge.loopexit, label %.lr.ph

select.unfold._crit_edge.loopexit:                ; preds = %select.unfold
  %.unpack326.pre = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %.pre = add i64 %20, -1
  br label %select.unfold._crit_edge

select.unfold._crit_edge:                         ; preds = %select.unfold._crit_edge.loopexit, %31
  %.pre-phi = phi i64 [ %.pre, %select.unfold._crit_edge.loopexit ], [ %21, %31 ]
  %.unpack326 = phi ptr [ %.unpack326.pre, %select.unfold._crit_edge.loopexit ], [ %.unpack314, %31 ]
  %45 = getelementptr [10 x i8], ptr %.unpack326, i64 %.pre-phi
  %46 = call i32 @_FortranACharacterCompareScalar1(ptr %45, ptr nonnull @_QQcl.42, i64 1, i64 1)
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %select.unfold._crit_edge
  %49 = load i128, ptr @_QFEcount_b, align 8, !tbaa !3
  %50 = add i128 %49, 1
  store i128 %50, ptr @_QFEcount_b, align 8, !tbaa !3
  br label %51

51:                                               ; preds = %48, %select.unfold._crit_edge
  %.unpack338 = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %52 = add i64 %20, -1
  %53 = getelementptr [10 x i8], ptr %.unpack338, i64 %52
  br label %54

54:                                               ; preds = %51, %54
  %55 = phi i64 [ 9, %51 ], [ %59, %54 ]
  %56 = getelementptr [10 x [1 x i8]], ptr %53, i64 0, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !3
  %58 = icmp eq i8 %57, 32
  %59 = add nsw i64 %55, -1
  %60 = icmp ne i64 %55, 0
  %61 = and i1 %58, %60
  br i1 %61, label %54, label %.preheader414

.preheader414:                                    ; preds = %54, %.preheader414
  %62 = phi i64 [ %66, %.preheader414 ], [ 9, %54 ]
  %63 = getelementptr [10 x [1 x i8]], ptr %53, i64 0, i64 %62
  %64 = load i8, ptr %63, align 1, !tbaa !3
  %65 = icmp eq i8 %64, 32
  %66 = add nsw i64 %62, -1
  %67 = icmp ne i64 %62, 0
  %68 = and i1 %65, %67
  br i1 %68, label %.preheader414, label %69

69:                                               ; preds = %.preheader414
  %70 = shl i64 %55, 32
  %sext362 = add i64 %70, 4294967296
  %71 = ashr exact i64 %sext362, 32
  %72 = select i1 %58, i64 0, i64 %71
  %73 = shl i64 %62, 32
  %sext363 = add i64 %73, 4294967296
  %74 = ashr exact i64 %sext363, 32
  %75 = select i1 %65, i64 0, i64 %74
  %76 = add nsw i64 %72, -1
  %77 = getelementptr [10 x [1 x i8]], ptr %53, i64 0, i64 %76
  %78 = sub nsw i64 %75, %72
  %79 = call i64 @llvm.smax.i64(i64 %78, i64 -1)
  %80 = add nsw i64 %79, 1
  %81 = call i32 @_FortranACharacterCompareScalar1(ptr %77, ptr nonnull @_QQcl.41, i64 %80, i64 1)
  %82 = icmp eq i32 %81, 0
  br i1 %82, label %83, label %86

83:                                               ; preds = %69
  %84 = load i128, ptr @_QFEcount_a, align 8, !tbaa !3
  %85 = add i128 %84, 1
  store i128 %85, ptr @_QFEcount_a, align 8, !tbaa !3
  br label %86

86:                                               ; preds = %83, %69
  %.unpack364 = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %87 = add i64 %20, -1
  %88 = getelementptr [10 x i8], ptr %.unpack364, i64 %87
  %89 = call i32 @_FortranACharacterCompareScalar1(ptr %88, ptr nonnull @_QQcl.42, i64 1, i64 1)
  %.unpack376 = load ptr, ptr @_QFEs.0, align 8, !tbaa !7
  %90 = add i64 %20, -1
  %91 = getelementptr [10 x i8], ptr %.unpack376, i64 %90
  br label %92

92:                                               ; preds = %86, %92
  %93 = phi i64 [ 9, %86 ], [ %97, %92 ]
  %94 = getelementptr [10 x [1 x i8]], ptr %91, i64 0, i64 %93
  %95 = load i8, ptr %94, align 1, !tbaa !3
  %96 = icmp eq i8 %95, 32
  %97 = add nsw i64 %93, -1
  %98 = icmp ne i64 %93, 0
  %99 = and i1 %96, %98
  br i1 %99, label %92, label %.preheader

.preheader:                                       ; preds = %92, %.preheader
  %100 = phi i64 [ %104, %.preheader ], [ 9, %92 ]
  %101 = getelementptr [10 x [1 x i8]], ptr %91, i64 0, i64 %100
  %102 = load i8, ptr %101, align 1, !tbaa !3
  %103 = icmp eq i8 %102, 32
  %104 = add nsw i64 %100, -1
  %105 = icmp ne i64 %100, 0
  %106 = and i1 %103, %105
  br i1 %106, label %.preheader, label %107

107:                                              ; preds = %.preheader
  %108 = shl i64 %93, 32
  %sext400 = add i64 %108, 4294967296
  %109 = ashr exact i64 %sext400, 32
  %110 = select i1 %96, i64 0, i64 %109
  %111 = shl i64 %100, 32
  %sext401 = add i64 %111, 4294967296
  %112 = ashr exact i64 %sext401, 32
  %113 = select i1 %103, i64 0, i64 %112
  %114 = add nsw i64 %110, -1
  %115 = getelementptr [10 x [1 x i8]], ptr %91, i64 0, i64 %114
  %116 = sub nsw i64 %113, %110
  %117 = call i64 @llvm.smax.i64(i64 %116, i64 -1)
  %118 = add nsw i64 %117, 1
  %119 = call i32 @_FortranACharacterCompareScalar1(ptr %115, ptr nonnull @_QQcl.41, i64 %118, i64 1)
  %120 = or i32 %119, %89
  %121 = icmp eq i32 %120, 0
  br i1 %121, label %122, label %125

122:                                              ; preds = %107
  %123 = load i128, ptr @_QFEcount_ab, align 8, !tbaa !3
  %124 = add i128 %123, 1
  store i128 %124, ptr @_QFEcount_ab, align 8, !tbaa !3
  br label %125

125:                                              ; preds = %122, %107
  %126 = add nuw i64 %20, 1
  %exitcond434.not = icmp eq i64 %20, %18
  br i1 %exitcond434.not, label %._crit_edge, label %.lr.ph424

._crit_edge:                                      ; preds = %125, %0
  %127 = load i128, ptr @_QFEcount_a, align 8, !tbaa !3
  %128 = load i128, ptr @_QFEcount_ab, align 8, !tbaa !3
  %129 = sub i128 %127, %128
  store i128 %129, ptr @_QFEcount_a, align 8, !tbaa !3
  %130 = load i128, ptr @_QFEcount_b, align 8, !tbaa !3
  %131 = sub i128 %130, %128
  store i128 %131, ptr @_QFEcount_b, align 8, !tbaa !3
  %132 = icmp eq i128 %127, %128
  %133 = icmp eq i128 %130, %128
  %134 = and i1 %132, %133
  %135 = icmp eq i128 %128, 0
  %136 = and i1 %135, %134
  br i1 %136, label %137, label %143

137:                                              ; preds = %._crit_edge
  %138 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.0c2b23b9cdb72dfc72e8ca75446d8f8f, i32 32)
  %139 = load i128, ptr @_QFEcount, align 8, !tbaa !3
  %140 = call i1 @_FortranAioOutputInteger128(ptr %138, i128 %139)
  %141 = call i32 @_FortranAioEndIoStatement(ptr %138)
  %142 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable

143:                                              ; preds = %._crit_edge
  br i1 %134, label %144, label %151

144:                                              ; preds = %143
  %145 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.0c2b23b9cdb72dfc72e8ca75446d8f8f, i32 37)
  %146 = load i128, ptr @_QFEcount, align 8, !tbaa !3
  %147 = load i128, ptr @_QFEcount_ab, align 8, !tbaa !3
  %148 = add i128 %146, -1
  %149 = add i128 %148, %147
  %150 = call i1 @_FortranAioOutputInteger128(ptr %145, i128 %149)
  br label %161

151:                                              ; preds = %143
  %152 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.0c2b23b9cdb72dfc72e8ca75446d8f8f, i32 39)
  %153 = load i128, ptr @_QFEcount, align 8, !tbaa !3
  %154 = load i128, ptr @_QFEcount_a, align 8, !tbaa !3
  %155 = load i128, ptr @_QFEcount_b, align 8, !tbaa !3
  %156 = call i128 @llvm.smin.i128(i128 %154, i128 %155)
  %157 = add i128 %156, %153
  %158 = load i128, ptr @_QFEcount_ab, align 8, !tbaa !3
  %159 = add i128 %157, %158
  %160 = call i1 @_FortranAioOutputInteger128(ptr %152, i128 %159)
  br label %161

161:                                              ; preds = %144, %151
  %.sink = phi ptr [ %145, %144 ], [ %152, %151 ]
  %162 = call i32 @_FortranAioEndIoStatement(ptr %.sink)
  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 i1 @_FortranAioInputDescriptor(ptr, ptr) local_unnamed_addr

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

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

declare i1 @_FortranAioOutputInteger128(ptr, i128) local_unnamed_addr

declare {} @_FortranAStopStatement(i32, i1, i1) local_unnamed_addr

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i128 @llvm.smin.i128(i128, i128) #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}
