There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.

Takahashi the human wants to achieve the following objective:

* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.



In order to achieve this, he can perform the following operation:

* Swap two adjacent balls.



Find the minimum number of operations required to achieve the objective.

Constraints

* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).

Input

Input is given from Standard Input in the following format:


N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}


Output

Print the minimum number of operations required to achieve the objective.

Examples

Input

3
B 1
W 2
B 3
W 1
W 3
B 2


Output

4


Input

4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1


Output

18


Input

9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7


Output

41
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
	class BIT {
		int n;
		long[] v;

		public BIT(int n_) {
			n = n_;
			v = new long[n + 1];
		}

		void add(int k, int val) {
			if (k <= 0)
				throw new AssertionError();
			while (k <= n) {
				v[k] += val;
				k += k & -k;
			}
		}

		long sum(int k) {
			long ret = 0;
			while (k >= 1) {
				ret += v[k];
				k -= k & -k;
			}
			return ret;
		}

		long sum(int l, int r) {
			return sum(r - 1) - sum(l - 1);
		}
	}

	void run() {
		Scanner sc = new Scanner();
		int N = sc.nextInt();
		char[] c = new char[2 * N];
		int[] a = new int[2 * N];
		int[] posW = new int[N + 1];
		int[] posB = new int[N + 1];
		long ans = 0;
		for (int i = 0; i < 2 * N; ++i) {
			c[i] = sc.next().toCharArray()[0];
			a[i] = sc.nextInt();
			if (c[i] == 'W') {
				posW[a[i]] = i + 1;
			} else {
				posB[a[i]] = i + 1;
			}
		}
		long[][] smaller = new long[2 * N][2 * N];// 位置iにあって、自分以下の数の同色のものは[0,j]に幾つあるか。
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j <= i; ++j) {
				if (j > 0)
					smaller[i][j] += smaller[i][j - 1];
				if (c[i] == c[j] && a[j] <= a[i]) {
					smaller[i][j]++;
				}
			}
		}
		long[][] dp = new long[N + 1][N + 1];// wがi,bがjまでそろった時のmin
		for (int i = 0; i < dp.length; ++i) {
			for (int j = 0; j < dp[i].length; ++j) {
				dp[i][j] = Long.MAX_VALUE / 3;
			}
		}
		// dp[n][n]が答え
		dp[0][0] = 0;
		BIT bit1 = new BIT(2 * N + 1);
		for (int i = 0; i <= N; ++i) {// w
			if (i > 0)
				bit1.add(posW[i], 1);
			BIT bit2 = new BIT(2 * N + 1);
			for (int j = 0; j <= N; ++j) {
				if (j > 0) {
					bit2.add(posB[j], 1);
				}
				if (i + 1 <= N)
					dp[i + 1][j] = Math.min(dp[i + 1][j],
							dp[i][j] + (posW[i + 1] - 1) - bit1.sum(1, posW[i + 1]) - bit2.sum(1, posW[i + 1]));
				if (j + 1 <= N)
					dp[i][j + 1] = Math.min(dp[i][j + 1],
							dp[i][j] + (posB[j + 1] - 1) - bit1.sum(1, posB[j + 1]) - bit2.sum(1, posB[j + 1]));
			}
		}
		System.out.println(dp[N][N]);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		int nextInt() {
			return (int) nextLong();
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}
	}
}
