#include <bits/stdc++.h>
using namespace std;
const int N = 300300;
int lo[3], hi[3];
int p[N], c[N];
char tmp[6];
int main() {
  int n;
  scanf("%d", &n);
  lo[0] = lo[1] = lo[2] = -1;
  hi[0] = hi[1] = hi[2] = -1;
  for (int i = (0); i <= (int(n) - 1); ++i) {
    scanf("%d%s", &p[i], tmp);
    c[i] = tmp[0] == 'G' ? 0 : tmp[0] == 'R' ? 1 : 2;
    if (lo[c[i]] == -1) lo[c[i]] = i;
    hi[c[i]] = i;
  }
  if (lo[0] == -1) {
    int res = 0;
    if (lo[1] != -1) res += p[hi[1]] - p[lo[1]];
    if (lo[2] != -1) res += p[hi[2]] - p[lo[2]];
    printf("%d\n", res);
    return 0;
  }
  int res = 0;
  for (int i = (1); i <= (2); ++i) {
    if (lo[i] != -1 && lo[i] < lo[0]) res += p[lo[0]] - p[lo[i]];
    if (hi[i] != -1 && hi[i] > hi[0]) res += p[hi[i]] - p[hi[0]];
  }
  int la = lo[0];
  for (int i = lo[0] + 1; i <= hi[0]; i++) {
    if (c[i] == 0) {
      int xle = p[la], xri = p[i];
      int l[3] = {0, xle, xle}, mx[3] = {0, 0, 0}, f[3] = {0, 0, 0};
      for (int j = la + 1; j < i; j++) {
        f[c[j]] = 1;
        mx[c[j]] = max(mx[c[j]], p[j] - l[c[j]]);
        l[c[j]] = p[j];
      }
      for (int j = (1); j <= (2); ++j) mx[j] = max(mx[j], p[i] - l[j]);
      if (f[1] && f[2] && 2 * (xri - xle) < 3 * (xri - xle) - mx[1] - mx[2]) {
        res += 2 * (xri - xle);
      } else {
        res += 3 * (xri - xle) - mx[1] - mx[2];
      }
      la = i;
    }
  }
  printf("%d\n", res);
  return 0;
}

### Prompt

Your challenge is to write a Cpp solution to the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 300300;
int lo[3], hi[3];
int p[N], c[N];
char tmp[6];
int main() {
  int n;
  scanf("%d", &n);
  lo[0] = lo[1] = lo[2] = -1;
  hi[0] = hi[1] = hi[2] = -1;
  for (int i = (0); i <= (int(n) - 1); ++i) {
    scanf("%d%s", &p[i], tmp);
    c[i] = tmp[0] == 'G' ? 0 : tmp[0] == 'R' ? 1 : 2;
    if (lo[c[i]] == -1) lo[c[i]] = i;
    hi[c[i]] = i;
  }
  if (lo[0] == -1) {
    int res = 0;
    if (lo[1] != -1) res += p[hi[1]] - p[lo[1]];
    if (lo[2] != -1) res += p[hi[2]] - p[lo[2]];
    printf("%d\n", res);
    return 0;
  }
  int res = 0;
  for (int i = (1); i <= (2); ++i) {
    if (lo[i] != -1 && lo[i] < lo[0]) res += p[lo[0]] - p[lo[i]];
    if (hi[i] != -1 && hi[i] > hi[0]) res += p[hi[i]] - p[hi[0]];
  }
  int la = lo[0];
  for (int i = lo[0] + 1; i <= hi[0]; i++) {
    if (c[i] == 0) {
      int xle = p[la], xri = p[i];
      int l[3] = {0, xle, xle}, mx[3] = {0, 0, 0}, f[3] = {0, 0, 0};
      for (int j = la + 1; j < i; j++) {
        f[c[j]] = 1;
        mx[c[j]] = max(mx[c[j]], p[j] - l[c[j]]);
        l[c[j]] = p[j];
      }
      for (int j = (1); j <= (2); ++j) mx[j] = max(mx[j], p[i] - l[j]);
      if (f[1] && f[2] && 2 * (xri - xle) < 3 * (xri - xle) - mx[1] - mx[2]) {
        res += 2 * (xri - xle);
      } else {
        res += 3 * (xri - xle) - mx[1] - mx[2];
      }
      la = i;
    }
  }
  printf("%d\n", res);
  return 0;
}

```