text: #include <bits/stdc++.h>
  using namespace std;
  
  #ifdef LOCAL
  #include "/debug.h"
  #else
  #define db(...)
  #endif
  
  #define all(v) v.begin(), v.end()
  #define pb push_back
  using ll = long long;
  const int NAX = 2e5 + 5, MOD = 1000000007;
  
  #define int int64_t
  
  int64_t solveCase(int N, const vector<int64_t> &a, const vector<char> &c)
  {
      // TODO: edit here
      // db(a);
      // db(c);
      map<char, vector<int64_t>> mp;
      mp['R'].clear();
      mp['G'].clear();
      mp['B'].clear();
      for (size_t i = 0; i < 2 * N; i++)
          mp[c[i]].pb(a[i]);
      int cnt = 0;
      vector<char> oddwale;
      for (auto &x : mp)
      {
          // db(x);
          cnt += x.second.size() & 1;
          if (x.second.size() & 1)
              oddwale.pb(x.first);
          sort(all(x.second));
      }
      db(cnt);
      db(mp);
      if (cnt == 0)
          return 0;
      assert(cnt == 2);
      char R = oddwale[0];
      char G = oddwale[1];
      char B = 'R' ^ 'G' ^ 'B' ^ R ^ G;
      db(R, G, B);
      int64_t res = LLONG_MAX;
      auto d = [&](char a, char b) -> int64_t
      {
          vector<pair<ll, int>> vecc;
          for (auto &x : mp[a])
              vecc.pb({x, 0});
          for (auto &x : mp[b])
              vecc.pb({x, 1});
          sort(all(vecc));
          ll ret = LLONG_MAX;
          db(a, b, vecc);
          for (size_t i = 0; i + 1 < vecc.size(); i++)
              if (vecc[i + 1].second != vecc[i].second)
                  ret = min(ret, vecc[i + 1].first - vecc[i].first);
          return ret;
      };
      res = min(res, d(R, G));
      res = min(res, d(R, B) + d(B, G));
      return res;
  }
  
  // generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
  signed main()
  {
  #ifndef LOCAL
      std::ios::sync_with_stdio(false);
      std::cin.tie(nullptr);
  #endif
      constexpr char endl = '\n';
      int N;
      cin >> N;
      vector<int64_t> a(2 * N);
      vector<char> c(2 * N);
      for (int i = 0; i < 2 * N; ++i)
      {
          cin >> a[i] >> c[i];
      }
      auto ans = solveCase(N, a, c);
      cout << ans << endl;
      return 0;
  }