This page is still under construction.

Parts of this page are still being built. What you see may change.

Memory Match

Time limit5sMemory limit512 MB

Summary
Given the history of a Memory match game, find how many pairs you can guarantee scoring on the current turn.
Level

Medium5 of 10

Topics
Simulation, Hash map, Greedy
Solved
No attempts yet

Problem

You are playing the game "Memory Match".

The game uses a set of NN picture cards. The cards come in pairs: there are N/2N/2 different pictures, and each picture appears on exactly two cards.

At the start of the game the cards are shuffled and laid face down on the table. Players then take turns looking for two cards with the same picture. A turn consists of picking a face-down card and turning it over to reveal its picture, then picking another face-down card and turning that one over as well. If the two pictures are identical, both cards stay face up, the player scores one point and takes another turn. If the pictures differ, both cards are turned face down again and the turn passes to the next player.

It is now your turn. You are given a description of every action played in the game so far. Compute how many pairs you can score with certainty on this turn. Several card layouts may agree with everything observed so far, so report the largest number of pairs you are guaranteed to match in every one of those layouts.

Figure 1: A game with 8 cards. Only cards 3 and 6 have been matched and lie face up, and every other card is face down. How many pairs can you score?

Input

The first line contains an even integer NN, the number of cards on the table (2≤N≤10002 \le N \le 1000).

The second line contains an integer KK, the number of turns played so far (0≤K≤10000 \le K \le 1000).

Each of the following KK lines describes one turn, in the order the turns were played. A line holds integers C1C_1 and C2C_2 followed by words P1P_1 and P2P_2. The numbers C1C_1 and C2C_2 are card positions on the table (1≤C1,C2≤N1 \le C_1, C_2 \le N and C1≠C2C_1 \ne C_2), and P1P_1 and P2P_2 are the pictures on the cards at those positions. Each word consists of between 1 and 20 lowercase letters a to z. If P1=P2P_1 = P_2, the two cards stay face up and the positions C1C_1 and C2C_2 are never chosen again.

At least two cards are still face down.

Output

Print one line with an integer SS, the number of matching pairs you can score with certainty.

Examples3

  1. Example 1

    Input
    8
    5
    1 3 earth sun
    2 6 mars sun
    6 3 sun sun
    7 5 earth moon
    2 7 mars earth
    
    Expected output
    3
    
  2. Example 2

    Input
    10
    6
    1 2 moon earth
    9 10 venus sun
    8 7 moon venus
    1 8 moon moon
    4 10 sun sun
    9 6 venus mars
    
    Expected output
    3
    
  3. Example 3

    Input
    8
    2
    1 3 moon earth
    2 6 sun earth
    
    Expected output
    1