#include<bits/stdc++.h>

#define ll long long

using namespace std;
//memset(dp, 0, sizeof(dp));

void work(int T) {
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		if(s == "AND") {
			a[i] = 1;
		} else {
			a[i] = 0;
		}
	}
	vector<vector<ll>> dp(n + 1, vector<ll>(2));
	dp[0][0] = dp[0][1] = 1;
	for(int i = 1; i <= n; i++) {
		if(a[i - 1]) {
			dp[i][0] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][0];
			dp[i][1] = dp[i - 1][1];
		} else {
			dp[i][0] = dp[i - 1][0];
			dp[i][1] = dp[i - 1][1] + dp[i - 1][0] + dp[i - 1][1];
		}
	}
	cout << dp[n][1] << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
	// int t;
	// for(cin >> t; t > 0; t--) {
		// work(t);
	// }
	work(0);
	
	return 0;
};
#include<bits/stdc++.h>
#define rep(a,b,c) for(register int a=(b);a<=(c);++a)
#define dow(a,b,c) for(register int a=(b);a>=(c);--a)
using namespace std;
const int MaxN=1000000+5;
int lef[MaxN],rig[MaxN];
bool usd[MaxN];
int main()
{
	register int n;
	scanf("%d",&n);
	register int pos,typ;
	rep(i,1,n)
	{
		scanf("%d%d",&pos,&typ);
		if(!usd[typ])
		{
			usd[typ]=true;
			lef[typ]=rig[typ]=pos;
			continue;
		}
		lef[typ]=min(lef[typ],pos);
		rig[typ]=max(rig[typ],pos);
	}
	usd[n+1]=true;
	register long long dpB=0,dpE=0,_dpB=0,_dpE=0,Answer;
	register int Last=0;
	rep(i,1,n+1)
	{
		if(!usd[typ=i])
			continue;
		_dpB=dpB,_dpE=dpE;
		dpB=(rig[typ]-lef[typ])+min(_dpB+abs(rig[typ]-lef[Last]),_dpE+abs(rig[typ]-rig[Last]));
		dpE=(rig[typ]-lef[typ])+min(_dpB+abs(lef[typ]-lef[Last]),_dpE+abs(lef[typ]-rig[Last]));
		Last=typ;
	}
	printf("%lld\n",Answer=min(dpB,dpE));
	return 0;
}