Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string.

Constraints

* 1 \leq N \leq 10^{4}
* 2 \leq |s_i| \leq 10
* s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:


N
s_1
\vdots
s_N


Output

Print the answer.

Examples

Input

3
ABCA
XBAZ
BAD


Output

2


Input

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA


Output

4


Input

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA


Output

4
import java.util.Scanner;
public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int N = Integer.parseInt(sc.next());

		int sum = 0;

		String[] s1 = new String[N];
		int A = 0;
		int B = 0;
		int BA = 0;

		for(int i=0;i<N;i++) {
            int t=0;
			String st = sc.next();
			if(st.substring(0,1).equals("B")) {
				t+=2;
			}
			int sl = st.length();
			if(st.substring(sl-1,sl).equals("A")){
				t++;
			}

			if(t==1) {
				A++;
			}else if(t==2){
				B++;
			}else if(t==3){
				BA++;
			}

			for(int j=0;j<sl-1;j++){

				String t1= st.substring(j,j+2);
				if(t1.equals("AB")) {
					sum++;
					j++;
				}
			}
		}
		if(BA==0) {
			if(B==A) {
				sum += A;
			}else {
				sum += Math.min(A,B);
			}
		}else {
			if(B==A){
				if(A==0) {
					sum += (BA-1);
				}else {
					sum = sum + BA + A;
				}
			}else{
				if(B==0||A==0) {
					sum += BA;
				}else {
					sum += BA + Math.min(A,B);
				}
			}
		}
		System.out.println(sum);
	}
}