COOK 133 Zebra crossing

Anyone know what’s wrong with my code Solution: 51337908 | CodeChef. I failed to figure out the wrong case.

I debugged with this case

7
12 1
100101000101
12 2
100101000101
12 3
100101000101
12 4
100101000101
12 5
100101000101
12 6
100101000101
12 7
100101000101

—Output
11
12
11
12
11
12
11

And this
3
5 1
11110
5 1
11111
5 1
01111

—Output
5
-1
5

seems correct. sucks

you can compare with my solution…
Solution: 51331052 | CodeChef