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.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String line = reader.readLine();
		int n = Integer.parseInt(line);
		int ans = 0;
		int Acount = 0;
		int Bcount = 0;
		boolean[] Aflag  = new boolean[n];
		boolean[] Bflag = new boolean[n];
		boolean final_flag = false;
		for (int i = 0; i < n; i++) {
			String[] str = reader.readLine().split("");
			if (str[str.length-1].equals("A")) {
				Aflag[i] = true;
				Acount++;
			}
			if (str[0].equals("B")) {
				Bflag[i] = true;
				Bcount++;
			}
			for (int k = 0; k < str.length; k++) {
				if (str[k].equals("A") && k < str.length-1 && str[k+1].equals("B")) {
					ans++;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if ((Aflag[i] && !Bflag[i]) || (!Aflag[i] && Bflag[i])) {
				final_flag = true;
			}
		}
		if (final_flag) {
			ans += Math.min(Acount, Bcount);
		}
		else {
			ans += Math.max(Math.min(Acount, Bcount) - 1, 0);
		}
		System.out.println(ans);
	}
}
