Under construction

Still being built. Things may change

Lefties vs. Righties

Interview

Time limit1sMemory limit1024 MB

Summary
Given T topics and N experts labeled rightie or leftie, pick at least one expert per topic with equal numbers of each label, minimizing the total interviewed.
Level

Medium5 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

Election season has begun. The news network you work for wants to present expert opinions on a variety of important topics. To give the appearance of being unbiased, the news chief insists that the experts interviewed cover a broad range of the political spectrum.

The political spectrum is so varied that this is hard to do. You decide to use a tried and true practice instead: each person is labeled either a rightie or a leftie. You want the job done as quickly as possible, so you want to conduct the fewest interviews possible.

More specifically, there are TT topics to be covered and NN experts. Each expert is experienced with exactly one of the topics you should cover, and each expert is either a rightie or a leftie. Interview the fewest experts possible such that the following conditions hold.

  • For each topic, you interview at least one expert in that topic.
  • You interview each expert at most once, since the audience would get bored otherwise.
  • The number of rightie experts you interview is the same as the number of leftie experts you interview.

Input

The first line contains two integers TT (1≤T≤1001 \leq T \leq 100) and NN (1≤N≤2001 \leq N \leq 200), the number of topics and the number of experts.

The next NN lines each contain an integer tit_i (1≤ti≤T1 \leq t_i \leq T) and a character cic_i (ci∈{R,L}c_i \in \{R, L\}). Here tit_i is the topic of the ii-th expert, and cic_i indicates whether that expert is a rightie (R) or a leftie (L).

Output

Output a single integer xx on a line by itself, the fewest interviews that can be conducted to satisfy these constraints. If it is not possible to satisfy all constraints, output −1-1.

Examples3

  1. Example 1

    Input
    3 6
    1 L
    2 R
    3 R
    2 R
    3 L
    2 R
    
    Expected output
    4
    
  2. Example 2

    Input
    3 3
    1 L
    2 L
    3 R
    
    Expected output
    -1
    
  3. Example 3

    Input
    4 6
    1 L
    2 L
    3 L
    1 R
    2 R
    3 R
    
    Expected output
    -1