SP4390 CARDSHUF - Cards shuffing

Description

[English](/problems/CARDSHUF/en/) [Tiếng Việt](/problems/CARDSHUF/vn/)"Phú ông" has a card deck consits of n cards. He writes on each card a number from 1 to n from the top to the bottom of the deck. Then he does shuffle the card deck several times, each time is described by **S(i, j)** meaning: pull out the i $ ^{th} $ card then put it on the j $ ^{th} $ of the remaining cards (1 For example (**n=6**): ![](https://cdn.luogu.com.cn/upload/vjudge_pic/SP4390/4466959b28a0cb25b9211a9a2372831e10c5d168.png) Afer x times of shuffing, "Phú ông" gives "Bờm" the card deck and chanllenges him to make it into the original order. Please help "Bờm"!

Input Format

\- The first line contains two integer n, x. \- Next x line(s), the p $ ^{th} $ line contains two integer i $ _{p} $ , j $ _{p} $ describing the p $ ^{th} $ time of shuffing (**S(i $ _{p} $ , j $ _{p} $ )**).

Output Format

\- A single integer means the minimal number of times of shuffing the card deck to help "Bờm".