stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int a[3];

	memset(a,0,sizeof(a));

	int cnt = 0;

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') a[0] ++;

		if(s[i][l-1] == 'A') a[1]++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') a[2] ++;

	}

	if(a[0] < a[1])  cnt += a[0];

	else cnt += a[1];

	if(a[0] == a[1] && a[1] == a[2] && a[2] != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.349112371880518x):
#include<iostream>

#include<cstdio>

#include<map>

#include<algorithm>

#define LL long long



using namespace std;

const int N = 1e4+7;

int main()

{

	ios :: sync_with_stdio(false);

	cin.tie(0);cout.tie(0);

	

	int n;

	cin>>n;

	string s;

	int ans=0,kBA=0,Bk=0,kA=0;

	for(int i=0;i<n;i++){

		cin>>s;

		int len=s.size() ;

		for(int i=1;i<len;i++) {

			if(s[i]=='B'&&s[i-1]=='A') ans++;

		}

		if(s[0]=='B'&&s[len-1]=='A') kBA++;

		else if(s[0]=='B') Bk++;

		else if(s[len-1]=='A') kA++;

	}

	if(kBA>0) {

		ans+=kBA-1;

		if(Bk) ans++,Bk--;

		if(kA) ans++,kA--;

	}

	ans+=min(Bk,kA);

	cout<<ans<<endl;

	return 0;

 }
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int a[3];

	memset(a,0,sizeof(a));

	int cnt = 0;

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') a[0] ++;

		if(s[i][l-1] == 'A') a[1]++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') a[2] ++;

	}

	if(a[0] < a[1])  cnt += a[0];

	else cnt += a[1];

	if(a[0] == a[1] && a[1] == a[2] && a[2] != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.27459337550408x):
#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<string>

#define ios ios::sync_with_stdio(false)

using namespace std;

typedef long long ll;

const int maxn=1e4+100;

string a[maxn];

int main()

{

	ios;

	int n,A=0,B=0,AB=0; cin>>n;

	ll sum=0;

	for(int i=0;i<n;i++){

		cin>>a[i];

		int len=a[i].size()-1;

		for(int j=1;j<=len;j++){

			if(a[i][j]=='B'&&a[i][j-1]=='A') sum++;

		}

		if(a[i][0]=='B'&&a[i][len]=='A'){

			AB++; continue;

		}

		if(a[i][0]=='B'){

			B++; continue;

		}

		if(a[i][len]=='A'){

			A++; continue;

		}

	}

	ll ans=0;

	if(AB>0){

		if(A>0||B>0){

			A-=1; B-=1;

			ans+=2;AB-=1;			

		}

		else AB-=1;

	}

	ans+=min(A,B)+AB+sum;

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

typedef long long ll;

const int N= 100100;

string s[N];

//map<int,map<char,char>>mp;

int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	ll n;

	cin>>n;

	int t;

	ll res;

	map<char,ll>m;

	map<char,ll>p;

	int flag=0;

	for(ll i=0;i<n;i++) 

	{

		cin>>s[i];

		m[s[i][0]]++;

		p[s[i][s[i].size()-1]]++;

		if(s[i][0]=='B'&&s[i][s[i].size()-1]=='A')

			flag++;

	}

	ll a=m['B'],b=p['A'];

	ll c=a-flag,d=b-flag;

	ll ans=0;

	for(ll i=0;i<n;i++)

	{

		t=s[i].size();

		for(ll j=0;j<t;j++)

		{

			if(s[i][j+1]=='B'&&s[i][j]=='A')

				ans++;

		}

	}

	res=min(b,a);

	ans+=res;

//	cout<<flag<<endl;

//	cout<<ans<<endl;

	if(c==0&&d==0&&flag!=0) ans--; 

	cout<<ans<<endl;

	return 0;

 } 

  

// Faster C++ solution for the same problem (measured speedup: 3.2727258682314666x):
#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<string>

#define ios ios::sync_with_stdio(false)

using namespace std;

typedef long long ll;

const int maxn=1e4+100;

string a[maxn];

int main()

{

	ios;

	int n,A=0,B=0,AB=0; cin>>n;

	ll sum=0;

	for(int i=0;i<n;i++){

		cin>>a[i];

		int len=a[i].size()-1;

		for(int j=1;j<=len;j++){

			if(a[i][j]=='B'&&a[i][j-1]=='A') sum++;

		}

		if(a[i][0]=='B'&&a[i][len]=='A'){

			AB++; continue;

		}

		if(a[i][0]=='B'){

			B++; continue;

		}

		if(a[i][len]=='A'){

			A++; continue;

		}

	}

	ll ans=0;

	if(AB>0){

		if(A>0||B>0){

			A-=1; B-=1;

			ans+=2;AB-=1;			

		}

		else AB-=1;

	}

	ans+=min(A,B)+AB+sum;

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:


#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<stack>

#include<cstring>

using namespace std;

typedef long long ll;

ll a[1111111];

int main()

{

	ios::sync_with_stdio(false);

	ll n;	cin>>n;

	ll xx=0;

	ll yy=0;

	ll zz=0;

	ll sum=0;

	string ss[111111];

	

	for(ll i=0;i<n;i++)

	{

		string s;	cin>>s;

		if(s[0]=='B'&&s[s.size()-1]=='A')

		{

//			ss[zz][0]='B';

//			ss[zz][1]='A';

			zz++;

		}

		if(s[0]=='B'&&s[s.size()-1]!='A')

			xx++;	

		if(s[0]!='B'&&s[s.size()-1]=='A')

			yy++;

		for(ll j=0;j<s.size()-1;j++)

		{

			if(s[j]=='A'&&s[j+1]=='B')

				sum++;

		}

		

	}

	//cout<<sum;

	int xx1=0;

	int yy1=0;

	if(zz)

	{

		sum=sum+zz-1;

		if(xx)

			yy1=1;

		if(yy)

			xx1=1;	

	}

	xx=xx+xx1;

	yy=yy+yy1;

	sum=sum+min(xx,yy);

	cout<<sum<<endl;

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 3.4545889737504596x):
#include<iostream>



using namespace std;



int main(){

	int N;

	cin>>N;

	string s[12345];

	int a[4]={0};

	int ans=0;

	for(int i=0;i<N;i++){

		cin>>s[i];

		int l=s[i].size();

		for(int j=1;j<l;j++){

			if(s[i][j-1]=='A'&&s[i][j]=='B'){

				ans++;

			}	

		}

		if(s[i][0]=='B'){

				a[0]++;

			}

			if(s[i][l-1]=='A'){

				a[1]++;

			}

			if(s[i][l-1]=='A'&&s[i][0]=='B'){

				a[2]++;

			}

	}

//	cout<<ans<<"  "<<a[0]<<"  "<<a[1]<<"  "<<a[2]<<endl;

	if(a[0]<a[1]){

		ans+=a[0];

	}

	else ans+=a[1];

	if(a[0]==a[1]&&a[0]==a[2]&&a[0]!=0) ans--;

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:


#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<stack>

#include<cstring>

using namespace std;

typedef long long ll;

ll a[1111111];

int main()

{

	ios::sync_with_stdio(false);

	ll n;	cin>>n;

	ll xx=0;

	ll yy=0;

	ll zz=0;

	ll sum=0;

	string ss[111111];

	

	for(ll i=0;i<n;i++)

	{

		string s;	cin>>s;

		if(s[0]=='B'&&s[s.size()-1]=='A')

		{

//			ss[zz][0]='B';

//			ss[zz][1]='A';

			zz++;

		}

		if(s[0]=='B'&&s[s.size()-1]!='A')

			xx++;	

		if(s[0]!='B'&&s[s.size()-1]=='A')

			yy++;

		for(ll j=0;j<s.size()-1;j++)

		{

			if(s[j]=='A'&&s[j+1]=='B')

				sum++;

		}

		

	}

	//cout<<sum;

	int xx1=0;

	int yy1=0;

	if(zz)

	{

		sum=sum+zz-1;

		if(xx)

			yy1=1;

		if(yy)

			xx1=1;	

	}

	xx=xx+xx1;

	yy=yy+yy1;

	sum=sum+min(xx,yy);

	cout<<sum<<endl;

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 3.5363152578973285x):
#include <bits/stdc++.h>

using namespace std;



const int maxn = 11000;



string str[maxn];

string s;

char tmp[maxn];

bool vis[maxn];



int main() {

	ios::sync_with_stdio(0);

	cin.tie(0); cout.tie(0);

	int n; cin >> n;

	int ans = 0;

	for(int i = 0; i < n; ++ i) {

		cin >> s;

		int l = 0;

		for(int i = s.length() - 1; i >= 0; -- i) {

			if(i && s[i] == 'B' && s[i - 1] == 'A') {

				++ ans;

				-- i;

			}

			else tmp[l ++] = s[i];

		}

		tmp[l] = 0;

		reverse(tmp, tmp + l);

		str[i] = tmp;

	}

	int ac = 0, bc = 0, cnt = 0;

	for(int i = 0; i < n; ++ i) {

		if(str[i].length()) {

			bool flag = true;

			if(str[i][0] == 'B') ++ bc;

			else flag = false;

			if(str[i][str[i].length() - 1] == 'A') ++ ac;

			else flag = false;

			if(flag) ++ cnt;

		}

	}

	if(ac != bc) ans += min(ac, bc);

	else if(ac){

		if(cnt == ac) ans += ac - 1;

		else ans += ac;

	}

	cout << ans;

	return 0;

}

/*

4

A

BABA

B

AB

*/
// Slower C++ solution:


#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<stack>

#include<cstring>

using namespace std;

typedef long long ll;

ll a[1111111];

int main()

{

	ios::sync_with_stdio(false);

	ll n;	cin>>n;

	ll xx=0;

	ll yy=0;

	ll zz=0;

	ll sum=0;

	string ss[111111];

	

	for(ll i=0;i<n;i++)

	{

		string s;	cin>>s;

		if(s[0]=='B'&&s[s.size()-1]=='A')

		{

//			ss[zz][0]='B';

//			ss[zz][1]='A';

			zz++;

		}

		if(s[0]=='B'&&s[s.size()-1]!='A')

			xx++;	

		if(s[0]!='B'&&s[s.size()-1]=='A')

			yy++;

		for(ll j=0;j<s.size()-1;j++)

		{

			if(s[j]=='A'&&s[j+1]=='B')

				sum++;

		}

		

	}

	//cout<<sum;

	int xx1=0;

	int yy1=0;

	if(zz)

	{

		sum=sum+zz-1;

		if(xx)

			yy1=1;

		if(yy)

			xx1=1;	

	}

	xx=xx+xx1;

	yy=yy+yy1;

	sum=sum+min(xx,yy);

	cout<<sum<<endl;

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.1082613760923248x):
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int cnt = 0;

	int x = 0,y = 0, z = 0; 

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') x ++;

		if(s[i][l-1] == 'A') y++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') z ++;

	}

	if(x < y)  cnt += x;

	else cnt += y;

	if(x == y && y == z && z != 0) cnt --;

	cout << cnt << endl;

	return 0;

}
// Slower C++ solution:


#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<stack>

#include<cstring>

using namespace std;

typedef long long ll;

ll a[1111111];

int main()

{

	ios::sync_with_stdio(false);

	ll n;	cin>>n;

	ll xx=0;

	ll yy=0;

	ll zz=0;

	ll sum=0;

	string ss[111111];

	

	for(ll i=0;i<n;i++)

	{

		string s;	cin>>s;

		if(s[0]=='B'&&s[s.size()-1]=='A')

		{

//			ss[zz][0]='B';

//			ss[zz][1]='A';

			zz++;

		}

		if(s[0]=='B'&&s[s.size()-1]!='A')

			xx++;	

		if(s[0]!='B'&&s[s.size()-1]=='A')

			yy++;

		for(ll j=0;j<s.size()-1;j++)

		{

			if(s[j]=='A'&&s[j+1]=='B')

				sum++;

		}

		

	}

	//cout<<sum;

	int xx1=0;

	int yy1=0;

	if(zz)

	{

		sum=sum+zz-1;

		if(xx)

			yy1=1;

		if(yy)

			xx1=1;	

	}

	xx=xx+xx1;

	yy=yy+yy1;

	sum=sum+min(xx,yy);

	cout<<sum<<endl;

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 4.877215695165262x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 1e7+5;



int main(){

	int n,sum=0,a=0,b=0,c=0;

	cin>>n;

	for(int i=1;i<=n;i++){

		string s;

		cin>>s;

		int l=s.size();

		if(s[0]=='B') b++;

		if(s[l-1]=='A') a++;

		if(s[0]=='B'&&s[l-1]=='A') c++;

		for(int j=1;j<l;j++){

			if(s[j-1]=='A'&&s[j]=='B') sum++;

			//cout<<sum<<" "<<b<<" "<<a<<endl;

		}

	}

	if(b==c&&a==c&&c!=0) sum--;

	cout<<sum+min(a,b)<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>



using namespace std;



int main(){

	int N;

	cin>>N;

	string s[12345];

	int a[4]={0};

	int ans=0;

	for(int i=0;i<N;i++){

		cin>>s[i];

		int l=s[i].size();

		for(int j=1;j<l;j++){

			if(s[i][j-1]=='A'&&s[i][j]=='B'){

				ans++;

			}	

		}

		if(s[i][0]=='B'){

				a[0]++;

			}

			if(s[i][l-1]=='A'){

				a[1]++;

			}

			if(s[i][l-1]=='A'&&s[i][0]=='B'){

				a[2]++;

			}

	}

//	cout<<ans<<"  "<<a[0]<<"  "<<a[1]<<"  "<<a[2]<<endl;

	if(a[0]<a[1]){

		ans+=a[0];

	}

	else ans+=a[1];

	if(a[0]==a[1]&&a[0]==a[2]&&a[0]!=0) ans--;

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4118078104876057x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 1e7+5;



int main(){

	int n,sum=0,a=0,b=0,c=0;

	cin>>n;

	for(int i=1;i<=n;i++){

		string s;

		cin>>s;

		int l=s.size();

		if(s[0]=='B') b++;

		if(s[l-1]=='A') a++;

		if(s[0]=='B'&&s[l-1]=='A') c++;

		for(int j=1;j<l;j++){

			if(s[j-1]=='A'&&s[j]=='B') sum++;

			//cout<<sum<<" "<<b<<" "<<a<<endl;

		}

	}

	if(b==c&&a==c&&c!=0) sum--;

	cout<<sum+min(a,b)<<endl;

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int maxn = 11000;



string str[maxn];

string s;

char tmp[maxn];

bool vis[maxn];



int main() {

	ios::sync_with_stdio(0);

	cin.tie(0); cout.tie(0);

	int n; cin >> n;

	int ans = 0;

	for(int i = 0; i < n; ++ i) {

		cin >> s;

		int l = 0;

		for(int i = s.length() - 1; i >= 0; -- i) {

			if(i && s[i] == 'B' && s[i - 1] == 'A') {

				++ ans;

				-- i;

			}

			else tmp[l ++] = s[i];

		}

		tmp[l] = 0;

		reverse(tmp, tmp + l);

		str[i] = tmp;

	}

	int ac = 0, bc = 0, cnt = 0;

	for(int i = 0; i < n; ++ i) {

		if(str[i].length()) {

			bool flag = true;

			if(str[i][0] == 'B') ++ bc;

			else flag = false;

			if(str[i][str[i].length() - 1] == 'A') ++ ac;

			else flag = false;

			if(flag) ++ cnt;

		}

	}

	if(ac != bc) ans += min(ac, bc);

	else if(ac){

		if(cnt == ac) ans += ac - 1;

		else ans += ac;

	}

	cout << ans;

	return 0;

}

/*

4

A

BABA

B

AB

*/


// Faster C++ solution for the same problem (measured speedup: 1.3791801181394738x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 1e7+5;



int main(){

	int n,sum=0,a=0,b=0,c=0;

	cin>>n;

	for(int i=1;i<=n;i++){

		string s;

		cin>>s;

		int l=s.size();

		if(s[0]=='B') b++;

		if(s[l-1]=='A') a++;

		if(s[0]=='B'&&s[l-1]=='A') c++;

		for(int j=1;j<l;j++){

			if(s[j-1]=='A'&&s[j]=='B') sum++;

			//cout<<sum<<" "<<b<<" "<<a<<endl;

		}

	}

	if(b==c&&a==c&&c!=0) sum--;

	cout<<sum+min(a,b)<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int cnt = 0;

	int x = 0,y = 0, z = 0; 

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') x ++;

		if(s[i][l-1] == 'A') y++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') z ++;

	}

	if(x < y)  cnt += x;

	else cnt += y;

	if(x == y && y == z && z != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.400781079606045x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 1e7+5;



int main(){

	int n,sum=0,a=0,b=0,c=0;

	cin>>n;

	for(int i=1;i<=n;i++){

		string s;

		cin>>s;

		int l=s.size();

		if(s[0]=='B') b++;

		if(s[l-1]=='A') a++;

		if(s[0]=='B'&&s[l-1]=='A') c++;

		for(int j=1;j<l;j++){

			if(s[j-1]=='A'&&s[j]=='B') sum++;

			//cout<<sum<<" "<<b<<" "<<a<<endl;

		}

	}

	if(b==c&&a==c&&c!=0) sum--;

	cout<<sum+min(a,b)<<endl;

	return 0;

}
// Slower C++ solution:


#include<iostream>

#include<stdio.h>

#include<algorithm>

#include<stack>

#include<cstring>

using namespace std;

typedef long long ll;

ll a[1111111];

int main()

{

	ios::sync_with_stdio(false);

	ll n;	cin>>n;

	ll xx=0;

	ll yy=0;

	ll zz=0;

	ll sum=0;

	string ss[111111];

	

	for(ll i=0;i<n;i++)

	{

		string s;	cin>>s;

		if(s[0]=='B'&&s[s.size()-1]=='A')

		{

//			ss[zz][0]='B';

//			ss[zz][1]='A';

			zz++;

		}

		if(s[0]=='B'&&s[s.size()-1]!='A')

			xx++;	

		if(s[0]!='B'&&s[s.size()-1]=='A')

			yy++;

		for(ll j=0;j<s.size()-1;j++)

		{

			if(s[j]=='A'&&s[j+1]=='B')

				sum++;

		}

		

	}

	//cout<<sum;

	int xx1=0;

	int yy1=0;

	if(zz)

	{

		sum=sum+zz-1;

		if(xx)

			yy1=1;

		if(yy)

			xx1=1;	

	}

	xx=xx+xx1;

	yy=yy+yy1;

	sum=sum+min(xx,yy);

	cout<<sum<<endl;

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 4.874307515375552x):
#include<iostream>

#include<cstring> 

using namespace std;

int main()

{

	char a[20];

	int n,A=0,B=0,C=0,ans=0;

	cin>>n;

	while(n--)

	{

		cin>>a;

		int len = strlen(a);

		if(a[0]=='B')

		B++;

		if(a[len-1]=='A')

		A++;

		if(a[0]=='B'&&a[len-1]=='A')

		C++;

		for(int i=0;i<len;i++)

		{

			if(a[i]=='A'&&a[i+1]=='B')

			ans++;

		}

	}

	int t;

	if(A>B) t = B;

	else

	t = A;

	if(A == C&&B == C&&C!=0)

		ans--;

	cout<<ans+t<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>



using namespace std;



int main(){

	int N;

	cin>>N;

	string s[12345];

	int a[4]={0};

	int ans=0;

	for(int i=0;i<N;i++){

		cin>>s[i];

		int l=s[i].size();

		for(int j=1;j<l;j++){

			if(s[i][j-1]=='A'&&s[i][j]=='B'){

				ans++;

			}	

		}

		if(s[i][0]=='B'){

				a[0]++;

			}

			if(s[i][l-1]=='A'){

				a[1]++;

			}

			if(s[i][l-1]=='A'&&s[i][0]=='B'){

				a[2]++;

			}

	}

//	cout<<ans<<"  "<<a[0]<<"  "<<a[1]<<"  "<<a[2]<<endl;

	if(a[0]<a[1]){

		ans+=a[0];

	}

	else ans+=a[1];

	if(a[0]==a[1]&&a[0]==a[2]&&a[0]!=0) ans--;

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4109659795746354x):
#include<iostream>

#include<cstring> 

using namespace std;

int main()

{

	char a[20];

	int n,A=0,B=0,C=0,ans=0;

	cin>>n;

	while(n--)

	{

		cin>>a;

		int len = strlen(a);

		if(a[0]=='B')

		B++;

		if(a[len-1]=='A')

		A++;

		if(a[0]=='B'&&a[len-1]=='A')

		C++;

		for(int i=0;i<len;i++)

		{

			if(a[i]=='A'&&a[i+1]=='B')

			ans++;

		}

	}

	int t;

	if(A>B) t = B;

	else

	t = A;

	if(A == C&&B == C&&C!=0)

		ans--;

	cout<<ans+t<<endl;

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int maxn = 11000;



string str[maxn];

string s;

char tmp[maxn];

bool vis[maxn];



int main() {

	ios::sync_with_stdio(0);

	cin.tie(0); cout.tie(0);

	int n; cin >> n;

	int ans = 0;

	for(int i = 0; i < n; ++ i) {

		cin >> s;

		int l = 0;

		for(int i = s.length() - 1; i >= 0; -- i) {

			if(i && s[i] == 'B' && s[i - 1] == 'A') {

				++ ans;

				-- i;

			}

			else tmp[l ++] = s[i];

		}

		tmp[l] = 0;

		reverse(tmp, tmp + l);

		str[i] = tmp;

	}

	int ac = 0, bc = 0, cnt = 0;

	for(int i = 0; i < n; ++ i) {

		if(str[i].length()) {

			bool flag = true;

			if(str[i][0] == 'B') ++ bc;

			else flag = false;

			if(str[i][str[i].length() - 1] == 'A') ++ ac;

			else flag = false;

			if(flag) ++ cnt;

		}

	}

	if(ac != bc) ans += min(ac, bc);

	else if(ac){

		if(cnt == ac) ans += ac - 1;

		else ans += ac;

	}

	cout << ans;

	return 0;

}

/*

4

A

BABA

B

AB

*/


// Faster C++ solution for the same problem (measured speedup: 1.3783577424241824x):
#include<iostream>

#include<cstring> 

using namespace std;

int main()

{

	char a[20];

	int n,A=0,B=0,C=0,ans=0;

	cin>>n;

	while(n--)

	{

		cin>>a;

		int len = strlen(a);

		if(a[0]=='B')

		B++;

		if(a[len-1]=='A')

		A++;

		if(a[0]=='B'&&a[len-1]=='A')

		C++;

		for(int i=0;i<len;i++)

		{

			if(a[i]=='A'&&a[i+1]=='B')

			ans++;

		}

	}

	int t;

	if(A>B) t = B;

	else

	t = A;

	if(A == C&&B == C&&C!=0)

		ans--;

	cout<<ans+t<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

const int maxn = 1e5+10;

int main()

{

	int n;

	cin >> n;

	string s[maxn];

	int cnt = 0;

	int x = 0,y = 0, z = 0; 

	for(int i = 0;i < n;i ++){

		cin >> s[i];

		int l =s[i].size();

		for(int j = 1;j < l;j ++){

			if(s[i][j - 1] =='A' && s[i][j] == 'B'){

				cnt ++;

			}

		}

		if(s[i][0] == 'B') x ++;

		if(s[i][l-1] == 'A') y++;

		if(s[i][l-1] == 'A' && s[i][0] =='B') z ++;

	}

	if(x < y)  cnt += x;

	else cnt += y;

	if(x == y && y == z && z != 0) cnt --;

	cout << cnt << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.3981569876251765x):
#include<iostream>

#include<cstring> 

using namespace std;

int main()

{

	char a[20];

	int n,A=0,B=0,C=0,ans=0;

	cin>>n;

	while(n--)

	{

		cin>>a;

		int len = strlen(a);

		if(a[0]=='B')

		B++;

		if(a[len-1]=='A')

		A++;

		if(a[0]=='B'&&a[len-1]=='A')

		C++;

		for(int i=0;i<len;i++)

		{

			if(a[i]=='A'&&a[i+1]=='B')

			ans++;

		}

	}

	int t;

	if(A>B) t = B;

	else

	t = A;

	if(A == C&&B == C&&C!=0)

		ans--;

	cout<<ans+t<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll(x) (x << 1)

#define rr(x) (x << 1 | 1)

using namespace std;

const int N = 2e5+5;

const int inf = 0x3f3f3f3f;



int n, m, ans = 0;



vector <int> a[N];



struct SegmentTree{ int l, r, mx, tag; }t[N*4];



void up(int x){ t[x].mx = max(t[ll(x)].mx, t[rr(x)].mx); }



void build(int l, int r, int x = 1){

	t[x].l = l, t[x].r = r; int mid = (l+r>>1);

	if(l == r){ t[x].mx = -m+r-1; return; }

	build(l, mid, ll(x)), build(mid+1, r, rr(x)), up(x);

}



void pushdown(int x){

	if(!t[x].tag) return;

	t[ll(x)].tag += t[x].tag, t[ll(x)].mx += t[x].tag;

	t[rr(x)].tag += t[x].tag, t[rr(x)].mx += t[x].tag;

	t[x].tag = 0;

}



void update(int l, int r, int val, int x = 1){

	if(l <= t[x].l && t[x].r <= r){

		t[x].tag += val, t[x].mx += val; return;

	}

	int mid = (t[x].l+t[x].r>>1); pushdown(x);

	if(l <= mid) update(l, r, val, ll(x));

	if(mid < r) update(l, r, val, rr(x)); up(x);

}



int query(int l, int r, int x = 1){

	if(l <= t[x].l && t[x].r <= r) return t[x].mx;

	if(r < t[x].l || t[x].r < l) return -inf;

	pushdown(x);

	return max(query(l, r, ll(x)), query(l, r, rr(x)));

}



int main(){

	ios::sync_with_stdio(false);

	int x, y; cin >> n >> m, ans = max(ans, n-m);

	for(int i = 1; i <= n; i++) cin >> x >> y, a[x].push_back(y);

	build(0, m+1);

	for(int i = 0; i <= m-1; i++){

		for(int j = 0; j < a[i].size(); j++) update(1, a[i][j], 1);

		ans = max(ans, query(i+2, m+1)-i);

	}

	cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 6.420283714968733x):
#include <bits/stdc++.h>

using namespace std;

#define int long long

int n = 0, m = 0, p = 0, q = 0, tot = 0;

pair<int, int> peo[200500], mid[200500];

bool used[200500];

signed main() {

	ios::sync_with_stdio(false);

	cin.tie(0), cout.tie(0);

	cin >> n >> m;

	for(int i = 1; i <= n; i++) {

		cin >> peo[i].first >> peo[i].second;

		peo[i].second = m + 1 - peo[i].second;

	}

	sort(peo + 1, peo + n + 1);

	for(int i = 1; i <= n; i++)

		if(peo[i].first > p) p++, used[i] = true;

	for(int i = 1; i <= n; i++)

		if(not used[i]) tot++, mid[tot] = peo[i], swap(mid[tot].first, mid[tot].second);

	sort(mid + 1, mid + tot + 1);

	for(int i = 1; i <= tot; i++)

		if(mid[i].first > q) q++;

	cout << n - min(m, p + q) << endl;

	return 0;

}