Lefties vs. Righties

시간 제한1초메모리 제한1024 MB

요약
모든 주제에 전문가를 최소 한 명씩 포함하면서 오른쪽 성향과 왼쪽 성향의 인터뷰 인원 수가 같아지도록 인터뷰할 전문가를 최소 인원으로 고른다.
난이도

보통10점 중 6점

유형
동적 계획법, 비트 연산
정답자
아직 제출이 없습니다

문제

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 the experts that are interviewed cover a broad range of the political spectrum.

This seems difficult to do since the political spectrum is so varied, so you decide to go with the tried, tested, and true practice of calling each person either just a rightie or a leftie. Finally, you want to get this job done as quickly as possible meaning 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 only one of the topics you should cover, and each expert is also either a rightie or a leftie. Your job is to interview the fewest experts possible such that the following conditions hold.

  • For each topic, you interviewed at least one expert in that topic.

  • You interviewed each expert at most once (the audience would get bored otherwise).

  • The number of rightie experts you interviewed is the same as the number of leftie experts you interviewed.

입력

The first line of input contains two integers TT (1≤T≤1001 \leq T \leq 100) and NN (1≤N≤2001 \leq N \leq 200) denoting the number of topics and experts, respectively. Then NN lines follow, each containing an integer t_it\_ i (1≤t_i≤T1 \leq t\_ i \leq T), indicating the topic that the ii’th expert is experienced with, and a single character c_ic\_ i (c\_ i \in \\{ R, L\\} ) indicating if the ii’th expert is a rightie or a leftie.

출력

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

예제3

  1. 예제 1

    입력
    3 6
    1 L
    2 R
    3 R
    2 R
    3 L
    2 R
    
    예상 출력
    4
    
  2. 예제 2

    입력
    3 3
    1 L
    2 L
    3 R
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    4 6
    1 L
    2 L
    3 L
    1 R
    2 R
    3 R
    
    예상 출력
    -1