P12952 [HackerEarth] rani and ladders 2
普及
题目描述

Do you remember the game of Snakes and Ladders ? We have removed all the snakes from the board. However to compensate for the loss of trouble, we have enlarged the board from a maximum limit (Target Square) of 100 to a maximum limit (Target Square) of N ≥ 100, N being a perfect square.

Rani has a match with Nandu on this new kind of board next week.

Now Rani is practising alone on the board. Suddenly Rani starts wondering : "Suppose I have total control over both my dice (each die being a standard 1-6 die) and I am the only player playing all the turns such that in each turn I throw both the dice, what is the minimum number of turns in which I can reach the Target Square N ? "

Given the configuration of the board ie. given all the information about the location of Ladders on the board, find the answer to Rani's question.

Note (to player) :
You start from Square Number 1.
If you reach a square which contains the base of a ladder, you have to climb up that ladder in the same turn itself (it is compulsory to climb the ladder). Note that you can only climb up ladders, not climb down.
Say you are at square number N-2. The only way you can complete the
game is by getting a 2 by throwing both your dice. If you throw a 4
or a 5 or anything else, your turn goes wasted.

Input :

First line consisits of T denoting the number of test cases. The first line of each test case consists of 2 space-separated integers N an L denoting the Target Square and number of ladders respectively. The next L lines are such that each line consists of 2-space separated integers B and U denoting the squares where the bottom and upper parts of each ladder lie.

Ouptut:

Print a single integer per test case on a new line, the answer to Rani's question.

Constraints :

1 ≤ T ≤ 3

100 ≤ N ≤ 160000, N being a perfect square

0 ≤ L ≤ SquareRoot(N)

1 ≤ B<U ≤ N

Author : Shreyans

Tester : Sandeep

(By IIT Kgp HackerEarth Programming Club)

SAMPLE INPUT
1
100 1
5 97

SAMPLE OUTPUT
2

Explanation

In the first turn, Rani will drop 4 using both her dice. So, 1 -> 5. Now there is a ladder going from 5 to 97.
So, now Rani's piece will climb the ladder and reach 97.

In her second turn, Rani will drop a 3 using both her dice. So, 97 -> 100.
Hence Rani reaches the Target Square in 2 turns.

输入格式

见题面

输出格式

见题面

样例输入
1 11025 64 297 345 3266 10057 5366 9583 1815 5123 1255 1449 315 1580 160 513 2557 3726 452 4632 560 3339 6643 9342 2591 5273 1473 5777 826 10203 1572 3833 101 323 2311 5011 2124 5492 6325 6700 7925 9037 5995 6129 1157 3090 463 3925 2823 5693 7447 10803 10770 10822 2843 6756 5929 10030 1828 2322 2283 5106 650 990 7692 9230 2714 6464 1013 2048 1823 5533 1970 2335 2935 4125 2415 5692 8225 8503 173 387 16 21 436 4497 5445 7856 1491 2587 1254 5232 3957 7596 3242 3545 2036 7254 124 149 3026 3320 2775 2947 3336 8725 7463 9039 1378 6787 3 37 147 7121 5399 10784 3851 4285 4131 9784 226 980 3743 10620 1536 1771 2932 2987 1673 4942
样例输出
58