Problem list
Log in
#coci00720 · COCI 2025/2026 Contest 5

Škare

Unrated 3.0 s 512 MB

Wanting to perfect his scissors using skills, Fran came up with a new exercise. He got himself a paper strip of length $n$ centimetres and, of course, a pair of scissors. He also asked Lana to assist him for the exercise. Lana will give Fran $k$ instructions of the following type: "Cut the $x$-th strip at $l$ centimetres". At the beginning, Fran has one strip. After the first action, he will have two strips: the first strip $l$ centimetres long, and the second one $n - l$ centimetres long (the remainder of the original strip). After that, Fran will cut either the first or the second strip - depending on Lana’s instruction. Each time he cuts a strip, the two new pieces replace the original strip in the sequence. More formally, suppose Fran has $m$ strips of lengths $a_{1}$, $a_{2}$, ..., $a_{m}$. If Lana tells him to cut the $x$-th strip at $l$ centimetres, the new sequence of strip lengths becomes: $a_{1}$, $a_{2}$, ..., a_{$x - 1$}, l, $a_{x} - l$, a_{$x+1$}, ..., $a_{m}$. After performing all cuts, he and Lana want to verify the cutting process. One way to do that is to count how many distinct strip lengths there are at the end. Your task is to compute this number.

Input

The first line contains two natural numbers $n$ and $k$ ($2 \le n \le 500$, $1 \le k < n$), the length of the original strip and the number of instructions Lana gives Fran. Each of the following $k$ lines contains two natural numbers $x_{i}$ and $l_{i}$ ($1 \le x_{i} \le i$, $1 \le l_{i} \le L - 1$, where $L$ is the length of the $x_{i}-th$ strip at that moment). These numbers indicate that Fran should cut the $x_{i}-th$ strip at $l_{i}$ centimetres (counting strips from the beginning).

Output

In the first and only line, write one number - the number of distinct strip lengths that Fran will be left with after he performs all the cuttings.

Example 1

Input
5 1
1 2
Output
2

Example 2

Input
6 2
1 4
1 2
Output
1

Example 3

Input
10 3
1 2
2 3
3 2
Output
2

Clarification of the first example: [5] →[2, 3] Clarification of the second example: [6] →[4, 2] →[4, 2] →[2, 2, 2] Clarification of the third example: [10] →[2, 8] →[2, 8] →[2, 3, 5] →[2, 3, 5] →[2, 3, 2, 3]

Source
COCI 2025/2026 Contest 5
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code