#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5 + 10;

int main() {
  int n; scanf("%d", &n);
  vector<long long> v[3];
  auto ID = [&](char c) {
    if(c == 'R') return 0;
    else if(c == 'G') return 1;
    return 2;
  };
  for(int i = 0; i < 2 * n; i++) {
    long long x; char c[2];
    scanf("%lld%s", &x, c);
    v[ID(c[0])].push_back(x);
  }

  for(int i = 0; i < 3; i++) sort(v[i].begin(), v[i].end());

  bool flag = 1;
  for(int i = 0; i < 3; i++) flag &= (int(v[i].size()) % 2 == 0);
  if(flag) return puts("0"), 0;

  long long ans = 1e17;
  for(int i = 0; i < 3; i++) if(int(v[i].size()) % 2 == 0) {
    int j1 = (i + 1) % 3, j2 = (i + 2) % 3;
    for(int k = 0, r = 0; k < int(v[j1].size()); k++) {
      while(r < int(v[j2].size()) && v[j2][r] < v[j1][k]) r++;
      if(r < int(v[j2].size())) ans = min(ans, v[j2][r] - v[j1][k]);
    }
    for(int k = 0, r = 0; k < int(v[j2].size()); k++) {
      while(r < int(v[j1].size()) && v[j1][r] < v[j2][k]) r++;
      if(r < int(v[j1].size())) ans = min(ans, v[j1][r] - v[j2][k]);
    }

    long long mn1 = 1e17;
    for(auto x : v[j1]) {
      auto it = lower_bound(v[i].begin(), v[i].end(), x);
      if(it != v[i].end()) mn1 = min(mn1, *it - x);
      if(it != v[i].begin()) mn1 = min(mn1, x - *prev(it));
    }
    long long mn2 = 1e17;
    for(auto x : v[j2]) {
      auto it = lower_bound(v[i].begin(), v[i].end(), x);
      if(it != v[i].end()) mn2 = min(mn2, *it - x);
      if(it != v[i].begin()) mn2 = min(mn2, x - *prev(it));
    }

    ans = min(ans, mn1 + mn2);
    printf("%lld\n", ans);
    return 0;
  }

  return 0;
}


#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> T[20];

int query(int h, int k, int i, int j)
{
	int m = (k << h) + (1 << (h - 1));
	if(i == (k << h) && j == ((k + 1) << h) - 1) return T[h][k];
	if(j < m) return query(h - 1, k << 1, i, j);
	if(i >= m) return query(h - 1, k << 1 | 1, i, j);
	return query(h - 1, k << 1, i, m - 1) ^ query(h - 1, k << 1 | 1, m, j);
}

int main()
{
	int n, q;
	scanf("%d%d", &n, &q);
	int d = ceil(log2(n));
	for(int i = 0; i <= d; i++)
	{
		T[i].clear();
		T[i].resize(1 << (d - i), 0);
	}
	for(int i = 0; i < n; i++)
		scanf("%I64d", &T[0][i]);
	for(int i = 1; i <= d; i++)
		for(int j = 0; j < (int)T[i].size(); j++)
			T[i][j] = T[i - 1][j << 1] ^ T[i - 1][j << 1 | 1];
	for (int i = 0; i < q; i++) {
		int t, x, y;
		scanf("%d%d%d", &t, &x, &y);
		if (t == 2) {
			printf("%d\n", query(d, 0, x - 1, y - 1));
		} else {
			int p = x - 1;
			T[0][p] = T[0][p] ^ y;
			p >>= 1;
			for (int j = 1; j <= d; j++) {
				T[j][p] = T[j - 1][p << 1] ^ T[j - 1][p << 1 | 1];
				p >>= 1;
			}
		}
	}
	return 0;
}
