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.*;
public class Main {

  public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int head_b_cnt = 0;
      int tail_a_cnt = 0;
      int h_t_b_a_cnt = 0;
      long cnt = 0;
      for(int i = 0; i < n; i++) {
        String s = sc.next();
        int len = s.length();
        if(s.charAt(0) == 'B' && s.charAt(len - 1) == 'A')
          h_t_b_a_cnt++;
        else if(s.charAt(0) == 'B')
          head_b_cnt++;
        else if(s.charAt(len-1) == 'A')
          tail_a_cnt++;
        cnt += cntAB(s);
      }

      if(h_t_b_a_cnt > 0) {
        cnt += h_t_b_a_cnt - 1;
        if(head_b_cnt > 0) {
            cnt++; head_b_cnt--;
        }
        if(tail_a_cnt > 0) {
            cnt++; tail_a_cnt--;
        }
      }
      int minCnt = Math.min(head_b_cnt, tail_a_cnt);
      cnt += minCnt;
      System.out.println(cnt);
  }

  private static int cntAB(String s) {
    int cnt = 0;
    for(int i = 0; i < s.length() - 1; i++) {
      if(s.charAt(i) == 'A' && s.charAt(i+1) == 'B') {
        cnt++;
        i++;
      }
    }
    return cnt;
  }
}