PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000030. Buộc dây thành vòng (Tying Rope)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Buộc dây thành vòng (Tying Rope)

Tying Rope

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn ropes numbered from 11 to nn. Each rope has two distinct ends: one red (R) and one blue (B). Initially, the ropes are separate.

Exactly mm knots are tied. Each knot joins two previously unused ends. The ends may belong to different ropes or to the same rope. Each specific end is used in at most one knot across all operations. After all knots have been tied, some ropes form connected groups.

A group is a closed loop if following its rope segments and knots forms a cycle without encountering a free end. All other groups are open ropes. Count the number of closed-loop groups and the number of open-rope groups.

Input

The first line contains n,mn,m. Each of the next mm lines contains ai,bi,ci,dia_i,b_i,c_i,d_i, tying end bib_i of rope aia_i to end did_i of rope cic_i.

Output

Print two integers X,YX,Y: the number of cyclic and non-cyclic groups.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20, 0≤m≤200\le m\le20; m≤nm\le n; 1≤ai,ci≤n1\le a_i,c_i\le n; bi,di∈{R,B}b_i,d_i\in\{R,B\}; each rope end is tied at most once.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000, 0≤m≤30000\le m\le3000; m≤nm\le n; 1≤ai,ci≤n1\le a_i,c_i\le n; bi,di∈{R,B}b_i,d_i\in\{R,B\}; each rope end is tied at most once.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5, 0≤m≤2⋅1050\le m\le2\cdot10^5; m≤nm\le n; 1≤ai,ci≤n1\le a_i,c_i\le n; bi,di∈{R,B}b_i,d_i\in\{R,B\}; each rope end is tied at most once.

Examples

Input

5 3
3 R 5 B
5 R 3 B
4 R 2 B

Output

1 2

Explanation

The two knots between ropes 33 and 55 use all four ends, forming a closed loop. Ropes 22 and 44 form one open chain, while rope 11 remains a separate open chain. Thus there is one cyclic component and two non-cyclic components.