Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from a_{i} to b_{i}. Of course, Famil Door wants to have as many friends celebrating together with him as possible.

Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.

Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends.

Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers a_{i} and b_{i} (1 ≤ a_{i} ≤ b_{i} ≤ 366), providing that the i-th friend can come to the party from day a_{i} to day b_{i} inclusive.


-----Output-----

Print the maximum number of people that may come to Famil Door's party.


-----Examples-----
Input
4
M 151 307
F 343 352
F 117 145
M 24 128

Output
2

Input
6
M 128 130
F 128 131
F 131 140
F 131 141
M 131 200
M 140 200

Output
4



-----Note-----

In the first sample, friends 3 and 4 can come on any day in range [117, 128].

In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
d = 367
F = [0 for i in range(d)]
M = [0 for i in range(d)]
for i in range(n):
    sex, l, r = input().split()
    l, r = int(l), int(r)
    if sex == 'F':
        for j in range(l, r+1): F[j] += 1
    else:
        for j in range(l, r+1): M[j] += 1
print(2*max(min(F[i], M[i]) for i in range(d)))
            


n = int(input())
m = [0 for i in range(400)]
f = [0 for i in range(400)]
ppl = []
for i in range(n):
    tmp = input().split()
    tmp[1] = int(tmp[1])
    tmp[2] = int(tmp[2])
    ppl.append(tmp)

ans = 0
for day in range(1, 367):
    for p in ppl:
        gender, a, b = p
        if a <= day <= b:
            if gender == 'M':
                m[day] += 1
            else:
                f[day] += 1
    ans = max(ans, min(m[day], f[day]))
print(2*ans)


v = [[0] * 367 for i in range(2)]
for i in range(int(input())):
    c = input().split()
    s = int(c[0] == 'M')
    for j in range(int(c[1]), int(c[2]) + 1):
        v[s][j] += 1
print(2 * max(min(x, y) for x, y in zip(v[0], v[1])))

m = [0 for _ in range(400)]
f = [0 for _ in range(400)]

n = int(input())

for i in range(n):
    g, a, b = input().split()
    a = int(a)
    b = int(b)

    if g == "M":
        for j in range(a, b+1):
            m[j] += 1
    else:
        for j in range(a, b+1):
            f[j] += 1

ret = 0

for i in range(1, 400):
    ret = max(ret, min(m[i], f[i]) * 2)

print(ret)


n = int(input())
p = []
for i in range(n):
    s, t, j = input().split()
    t = int(t)
    j = int(j)
    p.append([t, 'a', s])
    p.append([j, 'z', s])
p.sort()
e1 = 0
e2 = 0
maxi = 0
for i in range(2 * n):
    if p[i][1] == 'a' and p[i][2] == 'M':
        e1 += 1
    elif p[i][1] == 'a' and p[i][2] == 'F':
        e2 += 1
    elif p[i][1] == 'z' and p[i][2] == 'M':
        e1 -= 1
    else:
        e2 -= 1
    maxi = max(maxi, min(e1, e2))
print(maxi * 2)
        


n = int(input())
ar = [(0, 0)] * 367
for i in range(n):
    p, a, b = [i for i in input().split()]
    a = int(a)
    b = int(b)
    if p == 'M':
        for j in range(a, b + 1):
            ar[j] = (ar[j][0] + 1, ar[j][1])
    else:
        for j in range(a, b + 1):
            ar[j] = (ar[j][0], ar[j][1] + 1)
m = 0
for i in range(1, 367):
    m = max(m, min(ar[i][0], ar[i][1]) * 2)
print(m)

n = int(input())
events = []
for i in range(n):
    gender, a, b = input().split()
    a, b = int(a), int(b)
    events.append((a, -1, gender)) # -1: arrives
    events.append((b, 1, gender))  #  1: left
events.sort()
currM = 0
currF = 0
ans = 0
for ev in events:
    if ev[2] == "M":
        currM -= ev[1]
    else:
        currF -= ev[1]
    ans = max(ans, min(currM, currF) * 2)
    #print(currM, currF, ev)
print(ans)

DayM=[0 for i in range(367)]
DayF=[0 for i in range(367)]
n=int(input())
for i in range(n):
    a,b,c=input().split()
    Begin=int(b)
    End=int(c)
    for j in range(Begin,End+1):
        if a=='M':
            DayM[j]+=1
        else:
            DayF[j]+=1
M=0
for i in range(367):
    m=min(DayM[i],DayF[i])
    if m>M:
        M=m
print(M*2)


n = int(input())

ludia = []
for i in range(n):
    s = input().split()
    ludia.append([s[0], int(s[1]), int(s[2])])

maxi = 0
for den in range(1, 367):
    muzov, zien = 0, 0
    for clovek in ludia:
        if clovek[1] <= den <= clovek[2]:
            if clovek[0] == 'M':
                muzov += 1
            elif clovek[0] == 'F':
                zien += 1
            else:
                print('WTF?!')

    parov = min(muzov, zien)
    maxi = max(maxi, parov)

print(maxi * 2)


n = int(input())
boy = [0] * 1000
girl = [0] * 1000
for i in range(n):
    lst = list(input().split())
    lst[1] = int(lst[1])
    lst[2] = int(lst[2])
    for j in range(lst[1], lst[2] + 1):
        if lst[0] == 'M':
            boy[j] += 1
        else:
            girl[j] += 1
ans = -1            
for i in range(1000):
    k = 2 * min(boy[i], girl[i])
    ans = max(ans, k)
    
print(ans)


n = int(input())

males = [0]*368
females = [0]*368
for i in range(n):
    pol, a, b = input().split()
    a = int(a)
    b = int(b)
    if pol == 'M':
        for i in range(a,b+1):
            males[i]+=1
    else:
        for i in range(a,b+1):
            females[i]+=1
ans = 0
for i in range(368):
    if min(males[i],females[i])>ans:
        ans = min(males[i],females[i])
print(ans*2)

read = lambda: list(map(int, input().split()))
n = int(input())
p = [list() for i in range(367)]
for i in range(n):
    s, a, b = input().split()
    a, b = list(map(int, (a, b)))
    p[a].append((1, s))
    p[b].append((-1, s))
cntm = cntf = cur = ans = 0
for i in range(1, 367):
    for t, s in p[i]:
        if t == 1:
            if s == 'M': cntm += 1
            else: cntf += 1
    cur = min(cntf, cntm)
    #print(i, cur, cntf, cntm)
    ans = max(ans, cur)
    for t, s in p[i]:
        if t == -1:
            if s == 'M': cntm -= 1
            else: cntf -= 1
print(ans * 2)



n = int(input())
m = [0] * 366
f = [0] * 366
res = 0
for i in range(n):
    x, l, r = [i for i in input().split()]
    l = int(l)
    r = int(r)
    if x == 'M':
        for j in range(l-1, r):
            m[j] += 1
    else:
        for j in range(l-1, r):
            f[j] += 1
for i in range(366):
    res = max(res, min(m[i], f[i]))
print(res * 2)



n = int(input())
friends = [list(map(str,input().split())) for i in range(n)]
days = [[0]*366 for i in range(2)]
for i in range(n):
    if friends[i][0] == "M":
        for j in range(int(friends[i][1])-1,int(friends[i][2])):
            days[0][j] += 1
    else:
        for j in range(int(friends[i][1])-1,int(friends[i][2])):
            days[1][j] += 1
num = 0
for i in range(366):
    if min(days[0][i],days[1][i]) > num:
        num = min(days[0][i],days[1][i])
print(num*2)


day = 367
n = int(input())
male = [0] * day
female = [0] * day
for i in range(n):
    sex, l, r = input().split()
    if sex == 'M':
        for j in range(int(l), int(r) + 1):
            male[j] += 1
    else:
        for j in range(int(l), int(r) + 1):
            female[j] += 1

ans = 0
for i in range(1, day):
    ans = max(ans, min(male[i], female[i]))
print(ans * 2)

n = int(input())
grid = []
for i in range(n):
    tokens = input().split()
    grid.append([tokens[0], int(tokens[1]), int(tokens[2])])
ans = 0
for i in range(366):
    males, females = 0, 0
    for j in range(n):
        if grid[j][1] - 1 <= i <= grid[j][2] - 1:
            if grid[j][0] == 'M':
                males += 1
            else:
                females += 1
    x = 2 * min(males, females)
    if x > ans:
        ans = x
print(ans)

M = [0 for i in range(367)]
F = [0 for i in range(367)]
n = int(input())
for i in range(n):
    a,b,c = input().split()
    b = int(b)
    c = int(c)
    if a == 'M':
        for j in range(b,c+1):
            M[j] += 1
    else:
        for j in range(b,c+1):
            F[j] += 1
#print(F)
#print(M)

m = 0
for i in range(1,367):
    x = min(M[i],F[i])
    if x > m:
        m = x
print(m*2)

n = int(input())
day = [[0, 0] for i in range(368)]
for i in range(n):
	a, b, c = (input().split())
	if(a == "M"):
		day[int(b)][0] += 1
		day[int(c)+1][0] -= 1
	else:
		day[int(b)][1] += 1
		day[int(c)+1][1] -= 1

w = 0
m = 0
ans = 0
for i in day:
	w += i[0]
	m += i[1]
	ans = max(2 * min(m, w), ans)
print(ans)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time

n     = int(input())
bm    = {}
em    = {}
bf    = {}
ef    = {}
a     = []

for i in range(n):
    (s, b, e) = (i for i in input().split())
    b = int(b)
    e = int(e)+1

    a.append(b)
    a.append(e)

    if s =='M':
        if b in list(bm.keys()):
            bm[b] += 1
        else:
            bm[b] = 1

        if e in list(em.keys()):
            em[e] += 1
        else:
            em[e] = 1
    else:
        if b in list(bf.keys()):
            bf[b] += 1
        else:
            bf[b] = 1

        if e in list(ef.keys()):
            ef[e] += 1
        else:
            ef[e] = 1

start = time.time()

a = sorted(list(set(a)))

ans = 0
nf  = 0
nm  = 0

for i in a:
    if i in list(bm.keys()):
        nm += bm[i]
    if i in list(em.keys()):
        nm -= em[i]
    if i in list(bf.keys()):
        nf += bf[i]
    if i in list(ef.keys()):
        nf -= ef[i]

    t = min(nf, nm)

    if ans < 2*t:
        ans = 2*t

print(ans)
finish = time.time()
#print(finish - start)


n = int(input())

females = [0] * 366
males = [0] * 366

for _ in range(n):
    p, start, end = input().split()

    start = int(start) - 1
    end = int(end) # end == 366

    if p == "F":
        females[start] += 1
        if end < 366:
            females[end] -= 1
    elif p == "M":
        males[start] += 1
        if end < 366:
            males[end] -= 1

cur_m_count = 0
cur_f_count = 0
max_count = 0
for day in range(366):
    cur_m_count += males[day]
    cur_f_count += females[day]
    max_count = max(max_count, 2*min(cur_f_count, cur_m_count))

print(max_count)




#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 missingdays <missingdays@missingdays>
#
# Distributed under terms of the MIT license.

"""

"""

n = int(input())

f = []
m = []

for i in range(n):
    g, a, b = input().split()

    if g == "F":
        f.append([int(a), int(b)])
    else:
        m.append([int(a), int(b)])

maxi = 0

for i in range(1, 367):
    mw = 0
    mm = 0

    for j in range(len(f)):
        if f[j][0] <= i <= f[j][1]:
            mw += 1
    for j in range(len(m)):
        if m[j][0] <= i <= m[j][1]:
            mm += 1

    maxi = max(maxi, min(mw, mm))
    
print(maxi*2)



arrMa=[0]*367
arrFe=[0]*367
p=int(input())

for i in range(p):
    inf=list(map(str,input().split()))
    if inf[0]=='M':
        arrMa[int(inf[1])-1]+=1
        arrMa[int(inf[2])]-=1
    else:
        arrFe[int(inf[1])-1]+=1
        arrFe[int(inf[2])]-=1
        
maxi=-1
curM=0
curF=0
for i in range(366):
    curM+=arrMa[i]
    curF+=arrFe[i]
    if min(curM,curF)>maxi:
        maxi=min(curM,curF)
    
print(maxi*2)


__author__ = 'Utena'
n=int(input())
m=[0]*366
f=[0]*366
for i in range(n):
    g,a,b=input().split()
    a,b=int(a),int(b)
    if g=='F':
        for i in range(a-1,b):
            f[i]+=1
    elif g=='M':
        for i in range(a-1,b):
            m[i]+=1
t=0
for i in range(366):
    temp=min(f[i],m[i])
    if temp>t:t=temp
print(t*2)

n = int(input())
days = [[0, 0] for i in range(366)]
for i in range(n):
    str_input = list(input().split())
    #print(str_input[0])
    row = 0
    if str_input[0][0] == 'M':
        row = 1
        #print("true")
    #print(int(str_input[1]) - 1, int(str_input[2]))
    for j in range(int(str_input[1]) - 1, int(str_input[2])):
        days[j][row] += 1
cnt_guests = 0
for i in range(366):
    cnt_guests = max(cnt_guests, min(days[i][0], days[i][1]) * 2)
# for i in range(366):
#     print(i + 1, days[i])
print(cnt_guests)


#!/usr/bin/env python3
# 629B_relative.py - Codeforces.com/problemset/problem/629/B by Sergey 2016

import unittest
import sys

###############################################################################
# Relative Class (Main Program)
###############################################################################


class Relative:
    """ Relative representation """

    def __init__(self, test_inputs=None):
        """ Default constructor """

        it = iter(test_inputs.split("\n")) if test_inputs else None

        def uinput():
            return next(it) if it else sys.stdin.readline().rstrip()

        # Reading single elements
        [self.n] = list(map(int, uinput().split()))

        # Reading multiple number of lines of the same number of elements each
        l, s = self.n, 3
        inp = (" ".join(uinput() for i in range(l))).split()
        self.numm = [[(int(inp[i]) if j != 0 else inp[i] == 'M')
                     for i in range(j, l*s, s)] for j in range(s)]
        self.nums, self.numa, self.numb = self.numm

        self.dts = []
        for i in range(self.n):
            self.dts.append((self.numa[i], 1, self.nums[i]))
            self.dts.append((self.numb[i]+1, 0, self.nums[i]))
        self.sdts = sorted(self.dts)

        mcnt, fcnt = 0, 0
        self.ms, self.fs = {}, {}
        for i in range(len(self.sdts)):
            day = self.sdts[i][0]
            if self.sdts[i][2]:
                if self.sdts[i][1]:
                    mcnt += 1
                else:
                    mcnt -= 1
            else:
                if self.sdts[i][1]:
                    fcnt += 1
                else:
                    fcnt -= 1
            self.ms[day] = mcnt
            self.fs[day] = fcnt
        self.cr = []
        for d in self.ms:
            self.cr.append(min(self.ms[d], self.fs[d])*2)

    def calculate(self):
        """ Main calcualtion function of the class """

        result = max(self.cr)

        return str(result)

###############################################################################
# Unit Tests
###############################################################################


class unitTests(unittest.TestCase):

    def test_single_test(self):
        """ Relative class testing """

        # Constructor test
        test = "4\nM 151 307\nF 343 352\nF 117 145\nM 24 128"
        d = Relative(test)
        self.assertEqual(d.n, 4)
        self.assertEqual(d.numa, [151, 343, 117, 24])
        self.assertEqual(d.nums, [1, 0, 0, 1])

        # Sample test
        self.assertEqual(Relative(test).calculate(), "2")

        # Sample test
        test = "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"
        self.assertEqual(Relative(test).calculate(), "4")

        # Sample test
        test = ""
        # self.assertEqual(Relative(test).calculate(), "0")

        # My tests
        test = ""
        # self.assertEqual(Relative(test).calculate(), "0")

        # Time limit test
        # self.time_limit_test(5000)

    def time_limit_test(self, nmax):
        """ Timelimit testing """
        import random
        import timeit

        # Random inputs
        test = str(nmax) + " " + str(nmax) + "\n"
        numnums = [str(i) + " " + str(i+1) for i in range(nmax)]
        test += "\n".join(numnums) + "\n"
        nums = [random.randint(1, 10000) for i in range(nmax)]
        test += " ".join(map(str, nums)) + "\n"

        # Run the test
        start = timeit.default_timer()
        d = Relative(test)
        calc = timeit.default_timer()
        d.calculate()
        stop = timeit.default_timer()
        print(("\nTimelimit Test: " +
              "{0:.3f}s (init {1:.3f}s calc {2:.3f}s)".
              format(stop-start, calc-start, stop-calc)))

def __starting_point():

    # Avoiding recursion limitaions
    sys.setrecursionlimit(100000)

    if sys.argv[-1] == "-ut":
        unittest.main(argv=[" "])

    # Print the result string
    sys.stdout.write(Relative().calculate())

__starting_point()