Under construction

Still being built. Things may change

Liars and Truth Tellers

Interview

Time limit1sMemory limit128 MB

Summary
Each statement says whether one cow calls another honest or a liar; find the largest prefix of statements that admits a consistent truth/lie labeling of all cows.
Level

Medium7 of 10

Topics
Graph, Union-find, Binary search, Implementation
Solved
No attempts yet

Problem

A farmer has spent so much time with his cows that he has begun to understand their language. He notices that among his NN cows (2≤N≤10002 \le N \le 1000), some always tell the truth and the rest always lie.

The farmer writes down MM statements (1≤M≤10 0001 \le M \le 10\,000) made by the cows. Each statement has one of two forms:

  • x y T — cow xx claims that cow yy always tells the truth.
  • x y L — cow xx claims that cow yy always lies.

Every statement is about a pair of two different cows, and the same pair of cows may appear in several statements.

The farmer suspects that he copied some entries down incorrectly, so there may be no way to label every cow as a truth teller or a liar that is consistent with all MM statements. To salvage as much of the list as possible, find the largest value of AA for which some labeling of the NN cows as truth tellers or liars is consistent with the first AA statements.

Input

  • Line 1: two space-separated integers NN and MM.
  • Lines 2 to M+1M+1: each line has the form x y T or x y L, describing one statement made by cow xx about cow yy (1≤x,y≤N1 \le x, y \le N, x≠yx \ne y).

Output

  • Line 1: the largest value of AA such that some assignment of "truth teller" or "liar" to each of the NN cows is consistent with the first AA statements.

Hint

In the sample, statements 1 and 3 cannot both hold, but statements 1 and 2 can: let cows 1, 2, and 3 tell the truth and let cow 4 be a liar.

Examples3

  1. Example 1

    Input
    4 3
    1 4 L
    2 3 T
    4 1 T
    
    Expected output
    2
    
  2. Example 2

    Input
    2 1
    1 2 T
    
    Expected output
    1
    
  3. Example 3

    Input
    2 1
    1 2 L
    
    Expected output
    1