#include <bits/stdc++.h>
using namespace std;
inline void writeln2() { cout << "\n"; }
inline void writeln() { cout << "\n"; }
inline void readln() {}
template <typename T>
inline void read(T&);
template <typename T>
inline void priws(T);
template <typename T>
inline void print(T);
void err(vector<string>::iterator it) { ++it; }
template <typename Head, typename... Tail>
inline void readln(Head& head, Tail&... tail) {
  read(head);
  readln(tail...);
}
template <typename Head, typename... Tail>
inline void writeln2(Head head, Tail... tail) {
  print(head);
  writeln2(tail...);
}
template <typename Head, typename... Tail>
inline void writeln(Head head, Tail... tail) {
  priws(head);
  writeln2(tail...);
}
template <typename T>
inline void writeln_range(T f, T s) {
  if (f != s)
    for (auto i = f; i != s; ++i) writeln(*i);
}
template <typename Head, typename... Tail>
inline void err(vector<string>::iterator it, Head head, Tail... tail) {
  writeln((*it).substr((*it)[0] == ' '), "=", head);
  err(++it, tail...);
}
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.push_back(x);
  return v;
}
void run() {
  long long ans = 0;
  int n;
  readln(n);
  vector<int> pg(n, -1), ng(n, -1), mx(n, 0);
  vector<pair<int, char>> points(n);
  readln(points);
  int prev__ = -1;
  int next__ = -1;
  auto dist = [&](int i, int j) {
    if (i < j) swap(i, j);
    if (j == -1) return 0;
    return points[i].first - points[j].first;
  };
  for (int i = 0; i < (int)(n); ++i) {
    pg[i] = prev__;
    if (points[i].second == 'G') {
      if (prev__ == -1) pg[i] = i;
      prev__ = i;
    }
  }
  for (int i = n - 1; i >= (int)(0); --i) {
    ng[i] = next__;
    if (points[i].second == 'G') {
      if (next__ == -1) ng[i] = i;
      next__ = i;
    }
  }
  bool hasG = false;
  for (int e = 0; e < (int)(n); ++e) hasG |= points[e].second == 'G';
  auto run = [&](char c) {
    prev__ = -1;
    int i;
    for (i = 0; i < n && pg[i] == -1; ++i)
      if (points[i].second == c) ans += dist(i, prev__), prev__ = i;
    if (i < n && pg[i] != -1) ans += dist(i, prev__);
    if (hasG) {
      next__ = -1;
      int j;
      for (j = n - 1; j >= 0 && ng[j] == -1; --j)
        if (points[j].second == c) ans += dist(next__, j), next__ = j;
      if (j >= 0 && ng[j] != -1) ans += dist(next__, j);
    }
    while (i < n && ng[i] != -1 && ng[i] != i) {
      int mn = 0;
      prev__ = i;
      for (int q = i + 1; q < n && q < ng[i]; ++q)
        if (points[q].second == c) mn = max(mn, dist(q, prev__)), prev__ = q;
      mn = max(mn, dist(prev__, ng[i]));
      if (c == 'B')
        mx[i] = mn;
      else {
        long long d = dist(i, ng[i]);
        ans += min(2ll * d, 3ll * d - mn - mx[i]);
      }
      i = ng[i];
    }
  };
  run('B');
  run('R');
  writeln(ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  run();
  return 0;
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& _a);
template <typename T1, typename T2>
inline istream& operator>>(istream& is, pair<T1, T2>& _a) {
  return is >> _a.first >> _a.second;
}
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, pair<T1, T2>& _a) {
  return os << _a.first << " " << _a.second;
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& _a) {
  if (_a.size())
    os << _a[0];
  else
    os << "\n";
  for (int i = 1; i < _a.size(); ++i)
    os << "\n "[is_fundamental<T>::value] << _a[i];
  return os;
}
template <typename T>
inline istream& operator>>(istream& is, vector<T>& _a) {
  for (int i = 0; i < _a.size(); ++i) is >> _a[i];
  return is;
}
template <typename T>
inline ostream& operator<<(ostream& os, valarray<T>& _a) {
  if (_a.size())
    os << _a[0];
  else
    os << "\n";
  for (int i = 1; i < _a.size(); ++i)
    os << "\n "[is_fundamental<T>::value] << _a[i];
  return os;
}
template <typename T>
inline istream& operator>>(istream& is, valarray<T>& _a) {
  for (int i = 0; i < _a.size(); ++i) is >> _a[i];
  return is;
}
template <typename T>
inline void print(T _a) {
  cout << " " << _a;
}
template <typename T>
inline void priws(T _a) {
  cout << _a;
}
template <typename T>
inline void read(T& _a) {
  cin >> _a;
}
