Roy and Biv have a set of n points on the infinite number line.

Each point has one of 3 colors: red, green, or blue.

Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.

They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).

However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.

Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).

Help them compute the minimum cost way to choose edges to satisfy the above constraints.

Input

The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points.

The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109, ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.

Output

Print a single integer, the minimum cost way to solve the problem.

Examples

Input

4
1 G
5 R
10 B
15 G


Output

23


Input

4
1 G
2 R
3 B
10 G


Output

12

Note

In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.
#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;
}
