#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iomanip>
#include <vector>
#include <cstdio>
#include <float.h>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <stack>
#include <functional>
 
using namespace std;
 
#define endl '\n'
#define MOD 1000000007
#define INF 1ll<<30
#define MAX 100010
#define eps 1e-11
#define bit_max 1ll<<32
#define _USE_MATH_DEFINES




int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  //  freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    string a[n];
    vector<int> ab,bb,ba,aa,zz;
    for(int i=0;i<n;i++)
    {
        string temp;
        cin>>temp;
        a[i] = temp;
        if(temp[0] == 'A' && temp[temp.size()-1]=='B')
            ab.push_back(i);
        else if(temp[0] == 'B' && temp[temp.size()-1]!='A')
            bb.push_back(i);
        else if(temp[0] == 'B' && temp[temp.size()-1]=='A')
            ba.push_back(i);
        else if(temp[0] != 'B' && temp[temp.size()-1]=='A')
            aa.push_back(i);
        else
            zz.push_back(i);


    }
    vector<int> order;
    int q,w,e,r,t;
    q =0;
    w =0;
    e =0;
    r =0;
    t=0;
    int last = -1;
    for(int i=0;i<n;i++)
    {
        if(order.size() == 0)
        {
            if(r<aa.size())
            {
                order.push_back(aa[r]);
                r++;
                last = 0;
                // cout<<"AA"<<endl;
            }
            else if(e<ba.size())
            {
                order.push_back(ba[e]);
                e++;
                last = 0;
                // cout<<"BA"<<endl;
            }
            else
            {
                if(q<ab.size())
                {
                    order.push_back(ab[q]);
                    q++;
                    // cout<<"AB"<<endl;
                }
                else if(w<bb.size())
                {
                    order.push_back(bb[w]);
                    w++;
                    // cout<<"BB"<<endl;
                }
                else
                {
                    order.push_back(zz[t]);
                    t++;
                    // cout<<"ZZ"<<endl;
                }
                last = 1;
            }
        }
        else
        {
            if(last == 0)
            {
                if(e<ba.size())
                {
                    order.push_back(ba[e]);
                    e++;
                    last = 0;
                    // cout<<"BA"<<endl;
                }
                else if(w<bb.size())
                {
                    order.push_back(bb[w]);
                    w++;
                    last = 1;
                    // cout<<"BB"<<endl;
                }
                else if(r<aa.size())
                {
                    order.push_back(aa[r]);
                    r++;
                    last = 0;
                    // cout<<"AA"<<endl;
                }
                else if(q<ab.size())
                {
                    order.push_back(ab[q]);
                    q++;
                    last = 1;
                    // cout<<"AB"<<endl;
                }
                else
                {
                    order.push_back(zz[t]);
                    t++;
                    last = 1;
                    // cout<<"ZZ"<<endl;
                }
            }
            else
            {
                if(r<aa.size())
                {
                    order.push_back(aa[r]);
                    r++;
                    last = 0;
                    // cout<<"AA"<<endl;
                }
                else if(e<ba.size())
                {
                    order.push_back(ba[e]);
                    e++;
                    last = 0;
                    // cout<<"AA"<<endl;
                }
                else if(w<bb.size())
                {
                    order.push_back(bb[w]);
                    w++;
                    last = 1;
                    // cout<<"AA"<<endl;
                }
                else if(q<ab.size())
                {
                    order.push_back(ab[q]);
                    q++;
                    last = 1;
                    // cout<<"AA"<<endl;
                }
                else
                {
                    order.push_back(zz[t]);
                    t++;
                    // cout<<"AA"<<endl;
                }
            }
        }
    }
    string str = "";
    for(int i=0;i<order.size();i++)
    {
        str+=a[order[i]];
    }
    int ctr = 0;
    // cout<<str<<endl;
    for(int i=0;i<str.size()-1;i++)
    {
        if(str[i]=='A'&&str[i+1]=='B')
            ctr++;
    }
    cout<<ctr;

}

Measured memory_bytes: 1024