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.io.* ;
import java.util.* ;
import java.math.* ;
import static java.lang.Math.min ;
import static java.lang.Math.max ;
public class Codeshefcode{
	public static void main(String[] args) throws IOException{
		// Solver Machine = new Solver() ;
		// Machine.Solve() ;
		// Machine.Finish() ;
		new Thread(null,new Runnable(){
			public void run(){
				Solver Machine = new Solver() ;
				try{
					Machine.Solve() ;
					Machine.Finish() ;
				}catch(Exception e){
					e.printStackTrace() ;
					System.out.flush() ;
					System.exit(-1) ;
				}catch(Error e){
					e.printStackTrace() ;
					System.out.flush() ;
					System.exit(-1) ;
				}
			}
		},"Solver",1l<<27).start() ;
	}
}
class Mod{
	static long mod=1000000007 ;
	static long d(long a,long b){ return (a*inv(b))%mod ; }
	static long m(long a,long b){ return (a*b)%mod ; }
	static private long inv(long a){ return pow(a,mod-2) ; }
	static long pow(long a,long b){
		if(b<0) return pow(inv(a),-b) ;
		long val=a ; long ans=1 ;
		while(b!=0){
			if((b&1)==1) ans = (ans*val)%mod ; 
			val = (val*val)%mod ; 
			b/=2 ;
		}
		return ans ;
	}	
}
class pair implements Comparable<pair>{
	int x ; long y ; 
	pair(int x,long y){ this.x=x ; this.y=y ;} 
	public int compareTo(pair p){
		return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ;
	}
}
class Solver{
	Reader ip = new Reader(System.in) ;	  
	PrintWriter op = new PrintWriter(System.out) ;
	public void Solve() throws IOException{
		int n = ip.i() ;
		char clr[] = new char[n] ;
		long pos[] = new long[n] ;
		for(int i=0 ; i<n ; i++){
			pos[i] = ip.i() ;
			clr[i] = ip.s().charAt(0) ; 
		}
		mylist rd = new mylist() ;
		mylist bl = new mylist() ;
		long cost=0 ;
		long pR=-1,pB=-1,pG=-1 ;
		for(int i=0 ; i<n ; i++){
			long p = pos[i] ;
			if(clr[i]=='G'){
				if(pR!=-1) rd.add(p-pR) ;
				if(pB!=-1) bl.add(p-pB) ;
				if(pG!=-1){
					if(rd.size()!=0 && bl.size()!=0){
						long alt1 = (p-pG)*2 ;
						Collections.sort(rd) ;
						Collections.sort(bl) ;
						long alt2 = 3*(p-pG)-(rd.get(rd.size()-1)+bl.get(bl.size()-1)) ;
						cost+=min(alt1,alt2) ;
					}else{
						long alt = (p-pG) ;
						if(rd.size()!=0){
							Collections.sort(rd) ;
							alt+=((p-pG)-rd.get(rd.size()-1)) ;
						}
						if(bl.size()!=0){ 
							Collections.sort(bl) ;
							alt+=((p-pG)-bl.get(bl.size()-1)) ;
						}
						cost+=alt ;
					}
				}else
					cost+=(sm(rd)+sm(bl)) ;
				pG = p ;
				pR = -1 ; pB = -1 ;
				rd = new mylist() ;
				bl = new mylist() ;
			}else if(clr[i]=='R'){
				if(pR!=-1) rd.add(p-pR) ;
				else if(pG!=-1) rd.add(p-pG) ;
				pR=p ;
			}else{ 
				if(pB!=-1) bl.add(p-pB) ;
				else if(pG!=-1) bl.add(p-pG) ;
				pB=p ;
			}
		}
		if(rd.size()!=0)
			cost+=sm(rd) ;
		if(bl.size()!=0)
			cost+=sm(bl) ;
		pln(cost) ;
	}
	long sm(mylist ls){
		long res=0 ;
		for(long elem : ls) res+=elem ;
		return res ;
	}
	void Finish(){
		op.flush();
		op.close();
	}
	void p(Object o){
		op.print(o) ;
	}
	void pln(Object o){
		op.println(o) ;
	}		
}
class mylist extends ArrayList<Long>{}
class myset extends TreeSet<Integer>{}
class mystack extends Stack<Integer>{}
class mymap extends TreeMap<Long,Integer>{}
class Reader{
	BufferedReader reader;
	StringTokenizer tokenizer;
	Reader(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input)) ;
		tokenizer = new StringTokenizer("") ;
	}
	String s() throws IOException {
		while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()) ;
		return tokenizer.nextToken();
	}
	int i() throws IOException { return Integer.parseInt(s()) ;}
	long l() throws IOException { return Long.parseLong(s()) ;}
	double d() throws IOException {	return Double.parseDouble(s()) ;}
}