Task
The 2026 Foo(1)ball World Cup is approaching, and you, the Head Coach of the Info(1)Cup National Team, are preparing intense training sessions. Unfortunately, the hotel you booked has only one football pitch. After inspection, you find that the pitch has height meters and width meters. That would be perfectly fine if and had proper proportions. The issue is that while .
Each of the players is placed at a distinct position on the pitch, where and . Each player belongs to exactly one team; let denote the team of the player at position .
You have two assistants with very different philosophies: Attack and Defense.
They prepared the following game, which is played independently for each team.
- Defense chooses one player of the team who receives the ball first.
- Then, the assistants alternate turns. Attack moves first, followed by Defense, and so on.
- On a turn, the current assistant chooses the player who currently has the ball and tells them to which player the ball should be passed. This player must be from the same team.
- Since the players are not in the best shape, a player may only pass the ball to a teammate who is located either in the same row or in the same column of the pitch.
- Due to exhaustion, if a player receives the ball more than once, they will get injured. The assistants will never choose this option, as it is strongly against the team's interests.
- When the current player has no one to pass the ball to, the assistant whose turn it is loses the game.
Both assistants play optimally.
You want to test how players adapt in different situations, so you perform substitutions. Each substitution has the following form:
The player at position is assigned to team .
That is, we set to . (It is possible that was already equal to .)
Before any substitutions, and after each substitution, determine the number of teams for which Attack wins the game.
Input data
The first line contains and . The next lines contain elements each. On the -th of these lines, the -th value is . The next line contains the value . The following lines each contain three integers: , , and .
Output data
Output lines. The first line should contain the number of winning teams for Attack before any substitutions. The -th line should contain the number of winning teams for Attack after the -th substitution.
Constraints and clarifications
- For every substitution: , ,
| # | Points | Constraints |
|---|---|---|
| 1 | 19 | |
| 2 | 20 | |
| 3 | 8 | |
| 4 | 18 | |
| 5 | 17 | |
| 6 | 18 | No further restrictions |
Example 1
stdin
1 6
1 2 2 1 2 1
3
1 1 2
1 2 3
1 1 3
stdout
0
2
1
3
Explanation
Let's look at the first sample. Before making any substitutions, Defense can win the game for both teams.
For example, let's analyze team :
Suppose Defense gives the ball to the player standing at . Then, Attack makes the player at pass the ball to the player at .
Now, the only option for Defense is to make the player at pass the ball to the player at .
Since there are no other players in team , the player at cannot pass the ball to anyone else.
Since it is Attack's turn, they lose.
After the first substitution, the pitch looks as follows:
2 2 2 1 2 1
For team , if Defense gives the ball to the player at , Attack can make them pass the ball to .
Now, there is nowhere to pass the ball. Hence, Attack wins the game for team .
Example 2
stdin
2 4
1 2 3 1
2 2 2 3
3
1 4 2
1 2 1
2 4 1
stdout
2
0
1
0
Example 3
stdin
3 3
3 9 1
9 3 3
3 9 9
3
2 1 3
2 3 9
3 3 1
stdout
1
0
2
2