Company Organization

Time limit5sMemory limit128 MB

Summary
Given a priority-ordered list of subset/equality/inequality/disjoint/intersect constraints between sets assigned to groups, find the longest satisfiable prefix.
Level

Hard8 of 10

Topics
Union-find, Graph, Combinatorics
Solved
No attempts yet

Problem

You are organizing your company into groups, one group per project. You plan to form nn groups. Let XiX_i (for i=1,…,ni = 1, \dots, n) be the set of employees assigned to the ii-th group.

You have a list of requirements about how these groups relate to one another. Each requirement is one of the following five types, applied to a pair of distinct groups ii and jj:

  1. Xi⊆XjX_i \subseteq X_j — group ii must be a subset of group jj.
  2. Xi=XjX_i = X_j — the two groups must contain exactly the same employees.
  3. Xi≠XjX_i \neq X_j — the two groups must not be exactly the same.
  4. Xi∩Xj=∅X_i \cap X_j = \emptyset — the two groups must share no employee.
  5. Xi∩Xj≠∅X_i \cap X_j \neq \emptyset — the two groups must share at least one employee.

You may assign any employee to any group, you may hire or fire as many employees as you like, and a group may be empty. The ability of employees is irrelevant.

The requirements were collected without checking for consistency, so it may be impossible to satisfy all of them. Each requirement therefore has a priority, and they are listed from highest to lowest priority. You want to know how many of the highest-priority requirements can be satisfied at the same time — that is, the largest kk such that the first kk requirements (the kk highest-priority ones) can all hold simultaneously.

For example, suppose there are three groups with the following five requirements, listed from highest to lowest priority:

  1. X2⊆X1X_2 \subseteq X_1
  2. X3⊆X2X_3 \subseteq X_2
  3. X1⊆X3X_1 \subseteq X_3
  4. X1≠X3X_1 \neq X_3
  5. X3⊆X1X_3 \subseteq X_1

Assigning the same set of employees to X1X_1, X2X_2, and X3X_3 satisfies the first three requirements. However, no assignment can satisfy the first four highest-priority requirements at once. Although the first three requirements together with the fifth are satisfiable, the answer is 33, because only an unbroken prefix of the priority list counts.

Input

The input contains several datasets.

The first line of a dataset contains two integers nn and mm (2≤n≤1002 \le n \le 100, 1≤m≤100001 \le m \le 10000): the number of groups and the number of requirements.

Each of the next mm lines describes one requirement with three integers ss, ii, and jj (1≤s≤51 \le s \le 5, 1≤i≤n1 \le i \le n, 1≤j≤n1 \le j \le n, j≠ij \neq i): a requirement of type ss (numbered as above) between group ii and group jj. The requirements are given in descending order of priority.

The input ends with a line containing two zeros.

Output

For each dataset, output on its own line the maximum number of highest-priority requirements that can be satisfied simultaneously (the largest kk such that the first kk requirements are all satisfiable at once).

Examples1

  1. Example 1

    Input
    4 5
    1 2 1
    1 3 2
    1 1 3
    3 1 3
    1 3 1
    4 4
    1 2 1
    1 3 2
    1 1 3
    4 1 3
    4 5
    1 2 1
    1 3 2
    1 1 3
    4 1 3
    5 1 3
    2 3
    1 1 2
    2 1 2
    3 1 2
    0 0
    
    Expected output
    3
    4
    4
    2