Please subscribe to the official Codeforces channel in Telegram via the link: https://t.me/codeforces_official.
×

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

F. New Year and Rainbow Roads

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputRoy 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 *p*_{i} and *c*_{i} (*p*_{i} is an integer, 1 ≤ *p*_{i} ≤ 10^{9}, *c*_{i} 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.

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Dec/13/2018 17:40:27 (f2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|