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
#include <bits/stdc++.h>
using namespace std;
char a[1000];
int main()
{
	long long n,a1=0,a2=0,a3=0,a4=0;
	cin>>n;
	while(n--)
	{
		memset(a,'\0',sizeof(a));
		cin>>a;
			if(a[0]=='B')
			{
				a1++;
			}
			if(a[strlen(a)-1]=='A')
			{
				a2++;
			}
			if(a[strlen(a)-1]=='A'&&a[0]=='B')
			{
				a4++;
			}
		for(int i=0;i<strlen(a)-1;i++)
		{
			if(a[i]=='A'&&a[i+1]=='B')
			{
				a3++;
			}
		}
	}
	long long aa=min(a1,a2);
	if(a1==a4&&a2==a4&&a4!=0)
	{
		cout<<aa+a3-1<<endl;
	}
	else
	cout<<aa+a3<<endl;
}