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.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int endA = 0, startB = 0, both = 0, contain = 0;
	int result = 0;

	Main(List<String> list) {
		for (int i = 0; i < list.size(); ++i) {
			String str = list.get(i);
			if (str.endsWith("A") && str.startsWith("B")) {
				++both;
			} else if (str.endsWith("A")) {
				endA++;
			} else if (str.startsWith("B")) {
				startB++;
			}
			if (str.contains("AB")) {
				for (int j = 0; j < str.length() - 1; ++j) {
					if (str.substring(j).startsWith("AB")) {
						++contain;
					}
				}
			}
		}
		result += contain;
		if (both >= 2) {
			result += both - 1;
		}
		if (both >= 1) {
			if (endA > 0) {
				result++;
				endA--;
			}
			if (startB > 0) {
				result++;
				startB--;
			}
		}
		result += Math.min(endA, startB);
	}

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		int N = Integer.parseInt(in.readLine());
		List<String> list = new ArrayList<>();
		for (int i = 0; i < N; ++i) {
			list.add(in.readLine());
		}

		in.close();
		Main main = new Main(list);
		System.out.println(main.result);
	}

}