#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::min;
using std::sort;
using std::vector;

struct block {
    int x, y;
} blocks[400000];

int up_most[200001];

vector<vector<int>> sum;

void set(int place) {
    if (!sum[0][place]) {
        for (int bit = 0; ; ++bit) {
            ++sum[bit][place];
            place >>= 1;
            if (sum[bit].size() == 1) {
                break;
            }
        }
    }
}

int get(int left, int right) {
    int bit = 0, ret = 0;
    while (left < right) {
        if (left & 1) {
            ret += sum[bit][left];
            left = left / 2 + 1;
        } else {
            left >>= 1;
        }
        if (right & 1) {
            right >>= 1;
        } else {
            ret += sum[bit][right];
            right = right / 2 - 1;
        }
        ++bit;
    }
    return left == right ? ret + sum[bit][left] : ret;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int h, w, m;
    long long ans = 0;
    cin >> h >> w >> m;
    for (int i = w; i; --i) {
        up_most[i] = h + 1;
    }
    for (int i = 0; i < m; ++i) {
        cin >> blocks[i].x >> blocks[i].y;
        up_most[blocks[i].y] = min(up_most[blocks[i].y], blocks[i].x);
    }
    for (int i = w + 2; ; i = i + 1 >> 1) {
        sum.push_back(vector<int>(i, 0));
        if (i == 1) {
            break;
        }
    }
    for (int i = 1; i <= w; ++i) {
        if (up_most[i] == 1) {
            while (i < w) {
                set(++i);
            }
            break;
        } else {
            ans += up_most[i] - 1;
        }
    }
    for (int i = 0; i < h; ++i) {
        blocks[m + i] = {i + 1, w + 1};
    }
    sort(blocks, blocks + m + h, [](block a, block b) {
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    });
    for (int x = 2, i = 0; x <= h; ++x) {
        while (blocks[i].x < x) {
            set(blocks[i++].y);
        }
        if (blocks[i].y == 1) {
            break;
        } else {
            ans += get(1, blocks[i].y - 1);
        }
    }
    cout << ans;
    return 0;
}
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int>a[3];
int n,mp[200],x,y,ok,ans=LLONG_MAX,m1=LLONG_MAX,m2=LLONG_MAX;
signed main()
{
	ios::sync_with_stdio(0);
	mp['R']=0,mp['G']=1,mp['B']=2;
	cin>>n;
	n<<=1;
	for(int i=1;i<=n;i++)
	{
		int x;
		char t;
		cin>>x>>t;
		a[mp[t]].push_back(x);
	}
	ok=1;
	for(int i=0;i<3;i++)
	{
		sort(a[i].begin(),a[i].end());
		if(a[i].size()&1)
			ok=0;
	}
	if(ok)
	{
		cout<<0<<endl;
		return 0;
	}
	for(int i=0;i<3;i++)
		if(a[i].size()&1)
			x=i;
	for(int i=2;i>=0;i--)
		if(a[i].size()&1)
			y=i;
	int z=3-x-y;
	for(int i=0;i<a[x].size();i++)
	{
		int v=a[x][i];
		int p=lower_bound(a[y].begin(),a[y].end(),v)-a[y].begin();
		if(p!=a[y].size()) ans=min(ans,abs(v-a[y][p]));
		if(p) ans=min(ans,abs(v-a[y][p-1]));
	}
	for(int i=0;i<a[x].size();i++)
	{
		int v=a[x][i];
		int p=lower_bound(a[z].begin(),a[z].end(),v)-a[z].begin();
		if(p!=a[z].size()) m1=min(m1,abs(v-a[z][p]));
		if(p) m1=min(m1,abs(v-a[z][p-1]));
	}
	for(int i=0;i<a[y].size();i++)
	{
		int v=a[y][i];
		int p=lower_bound(a[z].begin(),a[z].end(),v)-a[z].begin();
		if(p!=a[z].size()) m2=min(m2,abs(v-a[z][p]));
		if(p) m2=min(m2,abs(v-a[z][p-1]));
	}
	if(m1<1e18)
		ans=min(ans,m1+m2);
	cout<<ans<<endl;
	return 0;
}