Vinay recently went to a tour with his classmates. On their return journey, his friends started playing cards. Being new to the game Vinay found out how to play. But as he did not understand the rules of the game, he did not show interest in playing. Instead, he created a new game of cards with his own rules.
It is a multiplayer game. Lets say N(numbered from 0 to N-1) people play the game. All the players sit in a circle. Each player is given K cards. Value of each card lies between 1 to K.(Note: It is not necessary that a person has K cards with all unique numbers).
They always arrange the cards in a sorted order and hold them in a hand fan fashion.
(i-1)th player challenges (i)th player by calling out a number (S). If (i)th player could find a contiguous subset from his set of cards whose values sums up to S, (i-1)th player gives away his largest valued card to (i)th player. If he doesn't find a contiguous subset, (i)th player gives away his largest valued card to (i+1)th player. Now, (i)th player challenges (i+1)th player and so on.
For player indexed 0, i-1 will be player indexed N-1.
A player is eliminated in middle of the game immediately after he is left with no cards. In case a player who should challenge next gets eliminated, that next player will be challenged by previous challenge winner.(if i+1 is eliminated when i challenges him, in the next call, i+2 is challenged by i.)
At any stage a player with N*K cards is the winner of this game. After a long time, if the game is not completed, they get bored and quit the game.
Now given N,K and all the calls of players in order, find who wins the game.
Input:
First line of input file contains two space separated integers N and K.
Next N lines contains K integers denoting value of cards for each player.
Then consists of numerous integers one in each line. Each of these integers defines a number with which player challenged to the next player. ith number is call of ith player.
First call will be by player indexed 0, next by player with index 1 and continues.
Output:
Print out the index of player(0 to N-1) who ends up with N*K cards at any stage and terminate.
If you reach End Of File and game isn't completed after all given calls, that implies the long time arrived and they decide to quit the game. In that case, print '-1'.
Constraints:
2 ≤ N ≤ 100
2 ≤ K ≤ 1000
1 ≤ S ≤ (K*(K+1))/2
There will be at most 106 challenges.
#Test1
Sample Input:
3 3
1 2 3
3 2 1
2 2 2
2
5
5
6
1
4
6
3
2
5
6
5
.
.
.
(few more lines containing +ve integers)
Sample Output:
0
#Test2
Sample Input:
3 3
1 2 3
3 2 1
2 2 2
2
5
5
6
1
Sample Output:
-1
Hint: Tracing for test1 after each call is provided in this link.
If you are using "Compile & Test", click on "Provide custom input", paste your own input and run the code.
No partial marking for this question.
Problem Setter : Vinay Kumar
In the first sample case, there will be a final result as player[0] wins the game after the challenges shown are over. There may be more calls after that which are to be omitted. In the second sample case, there will be no final result for given input. As they decide to quit the game.