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.
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
public class NewYearandRoads {
    
    
    
    /************************ SOLUTION STARTS HERE ************************/
    
    static int maxGap(ArrayList<Integer> arl) {
        int max = 0;
        for(int i = 0; i < arl.size() - 1; i++)
            max = Math.max(max , arl.get(i + 1) - arl.get(i));
        
        return max;
    }
    
    private static void solve() {
        
        ArrayList<ArrayList<Integer>> reds = new ArrayList<>();
        ArrayList<ArrayList<Integer>> blues = new ArrayList<>();
        
        ArrayList<Integer> greens = new ArrayList<>();
        
        int minG = Integer.MAX_VALUE;
        int maxG = Integer.MIN_VALUE;
        
        int n = nextInt();
        long cost = 0;
        ArrayList<Integer> tempB = new ArrayList<>();
        ArrayList<Integer> tempR = new ArrayList<>();
        
        while(n-->0) {
            int p = nextInt();
            switch (nextChar()) {
            case 'G':
                minG = Math.min(minG , p);
                maxG = Math.max(maxG , p);
                blues.add(tempB);
                reds.add(tempR);
                greens.add(p);
                tempB = new ArrayList<>();
                tempR = new ArrayList<>();
                break;
            case 'B':
                tempB.add(p);
                break;
            case 'R':
                tempR.add(p);
                break;
            }
        }
        
        blues.add(tempB);
        reds.add(tempR);
        
        // println("greens " + greens + " blues " + blues + " reds " + reds);
        
        if(greens.isEmpty()) { 
            /*
            List<int[]> combined = blues.get(0).stream().map(p -> new int[]{p , 1}).collect(Collectors.toList());
            combined.addAll(reds.get(0).stream().map(p -> new int[]{p , 0}).collect(Collectors.toList()));
            Collections.sort(combined , (p1 , p2) -> p1[0] - p2[0]);
             */
            ArrayList<Integer> b = blues.get(0);
            ArrayList<Integer> r = reds.get(0);
            cost += b.isEmpty() ? 0 : b.get(b.size() - 1) - b.get(0);
            cost += r.isEmpty() ? 0 : r.get(r.size() - 1) - r.get(0);
            /*
            int minDist = Integer.MAX_VALUE;
            
            for(int i = 0; i < combined.size() - 1; i++)
                if((combined.get(i)[1] ^ combined.get(i + 1)[1]) == 1)
                    minDist = Math.min(minDist , combined.get(i + 1)[0] - combined.get(i)[0]);
            cost += minDist == Integer.MAX_VALUE ? 0 : minDist;
             */
        
        } else {
            
            // segment --1--- G -- ... --- G ---n---
            // first segment 
            cost += blues.get(0).isEmpty() ? 0 : greens.get(0) - blues.get(0).get(0);
            cost += reds.get(0).isEmpty() ? 0 : greens.get(0) - reds.get(0).get(0);
            
            // last segment
            ArrayList<Integer> lastB = blues.get(blues.size() - 1);
            ArrayList<Integer> lastR = reds.get(reds.size() - 1);
            cost += lastR.isEmpty() ? 0 : lastR.get(lastR.size() - 1) - greens.get(greens.size() - 1);
            cost += lastB.isEmpty() ? 0 : lastB.get(lastB.size() - 1) - greens.get(greens.size() - 1);
            
            // middle segments
            for(int i = 1; i < greens.size(); i++) {
                int prev = greens.get(i - 1);
                int next = greens.get(i);
                reds.get(i).add(0, prev);
                reds.get(i).add(next);
                blues.get(i).add(0, prev);
                blues.get(i).add(next);
                long segLen = next - prev;
                long option1 = 2 * segLen;  // 2 parallel paths 1 using reds and other using blues
                long option2 = segLen + (segLen - maxGap(reds.get(i))) + (segLen - maxGap(blues.get(i)));
                cost += Math.min(option1 , option2);
            }
        }
        
        println(cost);
    }
    
    
    
    /************************ SOLUTION ENDS HERE ************************/
    
    
    
    
    
    /************************ TEMPLATE STARTS HERE **********************/
    
    public static void main(String[] args) throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
        st     = null;
        solve();
        reader.close();
        writer.close();
    }
    
    static BufferedReader reader;
    static PrintWriter    writer;
    static StringTokenizer st;
    
    static String next()
    {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}            
    st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
    static String nextLine()  {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}             
    static int    nextInt()   {return Integer.parseInt(next());}
    static long   nextLong()  {return Long.parseLong(next());}     
    static double nextDouble(){return Double.parseDouble(next());}
    static char   nextChar()  {return next().charAt(0);}
    static int[]  nextIntArray(int n)         {int[] a= new int[n];   int i=0;while(i<n){a[i++]=nextInt();}  return a;}
    static long[] nextLongArray(int n)        {long[]a= new long[n];  int i=0;while(i<n){a[i++]=nextLong();} return a;}    
    static int[]  nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}            
    static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}            
    static void   print(Object o)  { writer.print(o);  }
    static void   println(Object o){ writer.println(o);}
    
    /************************ TEMPLATE ENDS HERE ************************/
    
}