// Arup Guha
// 3/26/2015
// Solution to 2014 USACO Open Bronze Problem: Fair Photography

import java.util.*;
import java.io.*;

public class fairphoto {

	final public static int NEUTRAL = 0;
	final public static int MOREG = 1;
	final public static int MOREH = -1;

	public static void main(String[] args) throws Exception {

		BufferedReader stdin = new BufferedReader(new FileReader("fairphoto.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int n = Integer.parseInt(tok.nextToken());

		// Store cows.
		cow[] myCows = new cow[n];
		for (int i=0; i<n; i++) {
			tok = new StringTokenizer(stdin.readLine());
			int pos = Integer.parseInt(tok.nextToken());
			String type = tok.nextToken();
			myCows[i] = new cow(pos, type);
		}
		stdin.close();
		Arrays.sort(myCows);

		// Do all three possibilities and take best.
		int allG = maxRun(myCows, n, MOREG);
		int allH = maxRun(myCows, n, MOREH);
		int mix = balance(myCows, n);
		int res = Math.max(allG, Math.max(allH, mix));

		System.out.println(res);

		// Write out the result.
		FileWriter fout = new FileWriter(new File("fairphoto.out"));
		fout.write(res+"\n");
		fout.close();
	}

	public static int maxRun(cow[] myCows, int n, int value) {

		int start = -1, best = 0;
		for (int i=0; i<n; i++) {

			// Streak ends.
			if (myCows[i].type != value) {
				start = -1;
				continue;
			}

			// Streak starts.
			if (start == -1)
				start = i;

			// Streak continues
			else
				best = Math.max(best, myCows[i].pos-myCows[start].pos);
		}

		// Here is our result.
		return best;
	}

	public static int balance(cow[] myCows, int n) {

		// Store cumulative frequency list of "cow balance", plus n to avoid array out of bounds.
		int[] cumfreq = new int[n+1];
		cumfreq[0] = n;
		for (int i=1; i<=n; i++)
			cumfreq[i] = cumfreq[i-1] + myCows[i-1].type;

		int[] min = new int[2*n+1];
		int[] max = new int[2*n+1];
		Arrays.fill(min, -1);
		Arrays.fill(max, -1);

		// Fill in minimum index where cumfreq is equal to that value.
		for (int i=0; i<=n; i++)
			if (min[cumfreq[i]] == -1)
				min[cumfreq[i]] = i;

		// Do same for max, by filling from the end.
		for (int i=n; i>=0; i--)
			if (max[cumfreq[i]] == -1)
				max[cumfreq[i]] = i;

		// Any valid mixed picture of cows will start at distinct points in the cumulative
		// frequency array that have equal value.
		int best = 0;
		for (int i=0; i<=2*n; i++)
			if (max[i] != -1 && min[i] < max[i])
				best = Math.max(best, myCows[max[i]-1].pos-myCows[min[i]].pos);

		return best;
	}
}

class cow implements Comparable<cow> {

	public int pos;
	public int type;

	public cow(int location, String breed) {
		pos = location;
		if (breed.equals("G"))
			type = fairphoto.MOREG;
		else
			type = fairphoto.MOREH;
	}

	public int compareTo(cow other) {
		return this.pos - other.pos;
	}
}