Problem: One day, Hongcow goes to the store and sees a brand new deck of n special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.

This game takes some number of turns to complete. On a turn, Hongcow may do one of two things: 

  * Collect tokens. Hongcow collects 1 red token and 1 blue token by choosing this option (thus, 2 tokens in total per one operation). 
  * Buy a card. Hongcow chooses some card and spends tokens to purchase it as specified below. 



The i-th card requires ri red resources and bi blue resources. Suppose Hongcow currently has A red cards and B blue cards. Then, the i-th card will require Hongcow to spend max(ri - A, 0) red tokens, and max(bi - B, 0) blue tokens. Note, only tokens disappear, but the cards stay with Hongcow forever. Each card can be bought only once.

Given a description of the cards and their costs determine the minimum number of turns Hongcow needs to purchase all cards.

Input

The first line of input will contain a single integer n (1 ≤ n ≤ 16).

The next n lines of input will contain three tokens ci, ri and bi. ci will be 'R' or 'B', denoting the color of the card as red or blue. ri will be an integer denoting the amount of red resources required to obtain the card, and bi will be an integer denoting the amount of blue resources required to obtain the card (0 ≤ ri, bi ≤ 107).

Output

Output a single integer, denoting the minimum number of turns needed to acquire all the cards.

Examples

Input

3
R 0 1
B 1 0
R 1 1


Output

4


Input

3
R 3 0
R 2 0
R 1 0


Output

6

Note

For the first sample, Hongcow's four moves are as follows: 

  1. Collect tokens 
  2. Buy card 1
  3. Buy card 2
  4. Buy card 3

Note, at the fourth step, Hongcow is able to buy card 3 because Hongcow already has one red and one blue card, so we don't need to collect tokens.

For the second sample, one optimal strategy is as follows: 

  1. Collect tokens 
  2. Collect tokens 
  3. Buy card 2
  4. Collect tokens 
  5. Buy card 3
  6. Buy card 1

At the fifth step, even though Hongcow has a red token, Hongcow doesn't actually need to spend it, since Hongcow has a red card already.

Solution (in JAVA):

Solution:
// -*- coding: utf-8 -*-
//import java.awt.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream;
    if (args.length > 0 && args[0].equals("devTesting")) {
      try {
        inputStream = new FileInputStream(args[1]);
      } catch(FileNotFoundException e) {
        throw new RuntimeException(e);
      }
    } else {
      inputStream = System.in;
    }
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }
  
  static class TaskE {    
    int n;
    int[] color, r, b, cr, cb;
    int[][][] state;
    
    void solve(int testNumber, InputReader in, PrintWriter out) {
      n = in.nextInt();
      color = new int[n];
      r = new int[n];
      b = new int[n];
      int base = 0; // count of obligatory token-collecting turns
      int needred = 0; // count of obligatory red tokens to collect
      int needblue = 0; // count of obligatory blue tokens to collect
      for (int i = 0; i < n; ++i) {
        color[i] = in.next().charAt(0) == 'R' ? 0 : 1;
        r[i] = in.nextInt();
        b[i] = in.nextInt();
        needred += Math.max(0, r[i] - n);
        needblue += Math.max(0, b[i] - n);
        r[i] = Math.min(n, r[i]);
        b[i] = Math.min(n, b[i]);
      }
      base = Math.max(needred, needblue);
      cr = new int[1 << n]; // count of red cards after completing a given set S of cards
      cb = new int[1 << n]; // count of blue cards after completing a given set S of cards;
      for (int i = 1; i < 1 << n; ++i) {
        cr[i] = cr[i ^ (i & -i)];
        cb[i] = cb[i ^ (i & -i)];
        cr[i] += color[Integer.numberOfTrailingZeros(i & -i)] ^ 1;
        cb[i] += color[Integer.numberOfTrailingZeros(i & -i)];
      }
      state = new int[2][n * n + 1][1 << n];
      for (int[][] i : state) {
        for (int[] j : i) {
          Arrays.fill(j, -1);
        }
      }
      int ans = base + (needred < needblue ? go(0, 0, Math.min(n * n, needblue - needred)) : go(0, 1, Math.min(n * n, needred - needblue))) + n;
      out.println(ans);
    }
    
    int go(int mask, int redempty, int other) {
      if (mask == (1 << n) - 1) {
        return 0;
      }
      if (state[redempty][other][mask] != -1) {
        return state[redempty][other][mask];
      }
      int ret = Integer.MAX_VALUE / 2;
      for (int i = 0; i < n; ++i) {
        if ((mask & 1 << i) != 0) {
          continue;
        }
        int nextmask = mask | 1 << i;
        int remtokenred = (redempty ^ 1) * other;
        int remtokenblue = redempty * other;
        int cardred = cr[mask];
        int cardblue = cb[mask];
        int nred = Math.max(0, r[i] - (cardred + remtokenred)); // attempt to buy new red tokens saving as much new turns as possible
        int nblue = Math.max(0, b[i] - (cardblue + remtokenblue)); // attempt to buy new blue tokens saving as much new turns as possible
        int needmoves = Math.max(nred, nblue); // turns to do after saves
        int nextred = Math.min(n * n, remtokenred + needmoves - Math.max(0, r[i] - cardred)); // red tokens remaining, with clamping
        int nextblue = Math.min(n * n, remtokenblue + needmoves - Math.max(0, b[i] - cardblue)); // red tokens remaining, with clamping
        if (nextred == 0) {
          ret = Math.min(ret, go(nextmask, 1, nextblue) + needmoves);
        }
        if (nextblue == 0) {
          ret = Math.min(ret, go(nextmask, 0, nextred) + needmoves);
        }
      }
      state[redempty][other][mask] = ret;
      return ret;
    }
    
    int i(long x) {
      return (int) x;
    }
    
    <T> void heapsort(T[] a, int s, int e) {
      Queue<T> queue = new PriorityQueue<>();
      for (int i = s; i <= e; ++i)
        queue.add(a[i]);
      for (int i = s; i <= e; ++i)
        a[i] = queue.poll();
    }    
    
  }
      
  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }
    
    public String nextLine() {
      try {
        return reader.readLine();
      } catch(IOException e) {
        throw new RuntimeException(e);
      }
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public long nextLong() {
      return Long.parseLong(next());
    }
    
    public double nextDouble() {
      return Double.parseDouble(next());
    }

    public boolean hasInput() {
      try {
        if (tokenizer != null && tokenizer.hasMoreTokens()) {
          return true;
        }
        reader.mark(1);
        int ch = reader.read();
        if (ch != -1) {
          reader.reset();
          return true;
        }
        return false;
      } catch(IOException e) {
        throw new RuntimeException(e);
      }
    }
    
  }
}