2021-03-01 00:30 AKST

Midterm Team Contest


2021-03-01 04:30 AKST
The end is near!
Contest is over.
Not yet started.
Contest is starting in -331 days 19:13:07

Time elapsed


Time remaining


Problem J

Picture by: Michalio Lorenzo

In a final attempt to complete an impossible Super Mario level, you decided to write an AI and let it play the game for you.

As a first step, we forget about badguys and only implement navigation. In this problem, you will implement navigation over a river. The river has width $W$ and occupies $x=[0,W]$. Transportation over water happens using boats. Each boat moves in a range $[L,R]$; all boats remain at height $y=0$. At $t=0$, Mario is at $x=0$ and each boat is at its left endpoint (we assume boats are infinitely thin). Each boat moves periodically between its endpoints at the speed of one unit per second. So if a boat’s parameters are $[L,R]$, then it is at $x=L$ at $t=0$, at $x=R$ at $t=R-L$, at $x=L$ again at $t=2(R-L)$, at $x=R$ again at $t=3(R-L)$, etc. Mario cannot jump yet, so he can move between two boats if and only if their $x$-coordinates are equal. Furthermore, although there could be many boats sharing the same $x$-coordinate at some point in time, we assume that it is possible to move to any of the boats sharing the same $x$-coordinate.

Find the minimal time it takes to go from $x=0$ to $x=W$, or determine that it is impossible to reach $x=W$.


The input starts with a line containing an integer $T$ ($1 \leq T \leq 34$), the number of test cases. Then for each test case:

  • One line with two space-separated integers $N$ and $W$ ($0 \leq N \leq 100$, $1 \leq W \leq 500$), the number of boats and the width of the river, respectively.

  • $N$ lines, each containing two space-separated integers $L_ i$ and $R_ i$ ($0 \leq L_ i < R_ i \leq W$), representing the range $[L_ i,R_ i]$ covered by boat $i$.


For each test case, output one line containing an integer, the earliest possible time to reach $x=W$, or IMPOSSIBLE if it is impossible to reach $x=W$.

In the first test case, the two boats move synchronously so it is not possible to reach one from the other.

In the second test case, we can transfer to boat 3 at time 2, then to boat 2 at time 16, so that we finish at time 24.

Sample Input 1 Sample Output 1
2 2
0 1
1 2
3 10
0 8
2 10
2 3