#P6704. [COCI 2010/2011 #7] GITARA

[COCI 2010/2011 #7] GITARA

Description

You have a 6×P6 \times P matrix AA, and initially all entries are 00.

For each required position (i,j)(i, j), you must satisfy:

  1. At that moment, Ai,jA_{i, j} is 11.

  2. For Ai,j+kA_{i, j+k} where k>0k>0, the value is 00.

You need to find the minimum number of state changes while satisfying all requirements.

Input Format

The first line contains two positive integers n,Pn, P, which represent the number of notes in the melody and the number of frets on each string.

The next nn lines each contain two positive integers i,ji, j, representing the position (string number and fret number) that can produce the corresponding note, in the order played by the alien.

Output Format

Output one non-negative integer: the minimum number of finger movements made by the alien.

5 15
2 8
2 10
2 12
2 10
2 5
7
7 15
1 5
2 3
2 5
2 7
2 4
1 5
1 3
9

Hint

Explanation for Sample 1

All notes are produced on the second string. First press frets 8,10,128, 10, 12 in order (count=3count=3). Then release fret 1212 (count=4count=4). Finally, press fret 55 and release frets 8,108, 10 (count=7count=7).

Explanation for Sample 2

For each operation, the required numbers of finger movements are 1,1,1,1,3,0,21, 1, 1, 1, 3, 0, 2.

Constraints

Pressing or releasing a fret counts as one finger movement. Plucking the string does not count as a finger movement; it is a guitar-playing action (meaning you do not need to care how it is plucked, only the pressing is considered. Maybe the alien can use superpowers.)

For 100%100\% of the testdata, n5×105n \le 5 \times 10^5, 2P3×1052 \le P \le 3 \times 10^5.

Notes

This problem is worth 7070 points in total.

Translated from COCI2010-2011 CONTEST #7 T3 GITARA.

Translated by ChatGPT 5