CF1423F.Coins

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A famous gang of pirates, Sea Dogs, has come back to their hideout from one of their extravagant plunders. They want to split their treasure fairly amongst themselves, that is why You, their trusted financial advisor, devised a game to help them:

All of them take a sit at their round table, some of them with the golden coins they have just stolen. At each iteration of the game if one of them has equal or more than 2 coins, he is eligible to the splitting and he gives one coin to each pirate sitting next to him. If there are more candidates (pirates with equal or more than 2 coins) then You are the one that chooses which one of them will do the splitting in that iteration. The game ends when there are no more candidates eligible to do the splitting.

Pirates can call it a day, only when the game ends. Since they are beings with a finite amount of time at their disposal, they would prefer if the game that they are playing can end after finite iterations, and if so, they call it a good game. On the other hand, if no matter how You do the splitting, the game cannot end in finite iterations, they call it a bad game. Can You help them figure out before they start playing if the game will be good or bad?

输入格式

The first line of input contains two integer numbers nn and kk ( 1n1091 \le n \le 10^{9} , 0k21050 \le k \le 2\cdot10^5 ), where nn denotes total number of pirates and kk is the number of pirates that have any coins.

The next kk lines of input contain integers aia_i and bib_i ( 1ain1 \le a_i \le n , 1bi1091 \le b_i \le 10^{9} ), where aia_i denotes the index of the pirate sitting at the round table ( nn and 11 are neighbours) and bib_i the total number of coins that pirate aia_i has at the start of the game.

输出格式

Print 11 if the game is a good game: There is a way to do the splitting so the game ends after finite number of iterations.

Print 1-1 if the game is a bad game: No matter how You do the splitting the game does not end in finite number of iterations.

输入输出样例

  • 输入#1

    4 2
    1 2
    2 2

    输出#1

    1
  • 输入#2

    6 2
    2 3
    4 1

    输出#2

    1
  • 输入#3

    3 2
    1 1
    2 2

    输出#3

    -1

说明/提示

In the third example the game has no end, because You always only have only one candidate, after whose splitting you end up in the same position as the starting one.

首页