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>
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long maxn = 3e5 + 5;
pair<long long, long long> a[maxn];
vector<long long> r, g, b;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  char ch;
  long long mode = 0;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i].first >> ch;
    if (ch == 'R') {
      a[i].second = 1;
      r.push_back(a[i].first);
    } else if (ch == 'B') {
      a[i].second = 2;
      b.push_back(a[i].first);
    } else {
      a[i].second = 3;
      g.push_back(a[i].first);
    }
    if (ch == 'G') mode = 1;
  }
  long long ans = 0;
  if (mode == 0) {
    long long pre = -1;
    for (long long i = 1; i <= n; ++i) {
      if (a[i].second != 1) continue;
      if (pre != -1) ans += a[i].first - pre;
      pre = a[i].first;
    }
    pre = -1;
    for (long long i = 1; i <= n; ++i) {
      if (a[i].second != 2) continue;
      if (pre != -1) ans += a[i].first - pre;
      pre = a[i].first;
    }
  } else {
    for (long long i = 0; i < g.size(); ++i) {
      if (i == 0) {
        long long cas = lower_bound(b.begin(), b.end(), g[i]) - b.begin();
        if (cas != 0) ans += g[i] - b[0];
        cas = lower_bound(r.begin(), r.end(), g[i]) - r.begin();
        if (cas != 0) ans += g[i] - r[0];
      }
      if (i == g.size() - 1) {
        long long cas = lower_bound(b.begin(), b.end(), g[i]) - b.begin();
        if (cas != b.size()) ans += b[b.size() - 1] - g[i];
        cas = lower_bound(r.begin(), r.end(), g[i]) - r.begin();
        if (cas != r.size()) ans += r[r.size() - 1] - g[i];
      }
      if (i != g.size() - 1) {
        long long maxa = 0x3f3f3f3f, maxb = 0x3f3f3f3f;
        long long st = lower_bound(b.begin(), b.end(), g[i]) - b.begin();
        long long ed =
            lower_bound(b.begin(), b.end(), g[i + 1]) - b.begin() - 1;
        if (st < b.size() && b[st] < g[i + 1]) {
          long long temp = b[st] - g[i];
          temp = max(temp, g[i + 1] - b[ed]);
          for (long long j = st + 1; j <= ed; ++j)
            temp = max(temp, b[j] - b[j - 1]);
          maxa = temp;
        }
        st = lower_bound(r.begin(), r.end(), g[i]) - r.begin();
        ed = lower_bound(r.begin(), r.end(), g[i + 1]) - r.begin() - 1;
        if (st < r.size() && r[st] < g[i + 1]) {
          long long temp = r[st] - g[i];
          temp = max(temp, g[i + 1] - r[ed]);
          for (long long j = st + 1; j <= ed; ++j)
            temp = max(temp, r[j] - r[j - 1]);
          maxb = temp;
        }
        long long sum = g[i + 1] - g[i];
        if (maxa == 0x3f3f3f3f && maxb == 0x3f3f3f3f)
          ans += sum;
        else if (maxa == 0x3f3f3f3f)
          ans += sum + sum - maxb;
        else if (maxb == 0x3f3f3f3f)
          ans += sum + sum - maxa;
        else
          ans += min(sum * 2LL, sum * 3LL - maxa - maxb);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
