William the monkey finds himself in a bar! Immediately upon entering the bar William notices that something isn't right. All the monkeys in the bar are under a spell! William decides to split the bar into a grid of
So far the monkeys haven't moved, but they will soon! Since William can read minds, he knows exactly when each monkey will start moving forward. It's also possible that a monkey is so spellbound that it won't move at all! After a monkey starts moving, it continues moving at exactly one cell per second, only stopping when it leaves the grid. The monkeys at the beginning of the rows move horizontally to the right and the monkeys at the top of the columns move vertically downwards.
Given William's information, he wants to know the number of collisions that will occur between any two monkeys because he is a sadistic monkey and enjoys watching monkeys crash into each other. Note that a collision is when two monkeys arrive at the same cell at the same time. After a collision occurs, both monkeys are awakened from the spell and go home, meaning they can no longer be part of any further collisions.
If a monkey starts at the topmost row and starts moving at time
Constraints
Input Specification
The first line contains
The next line contains -1
, the monkey will not move.
The next and final line contains -1
, the monkey will not move.
Output Specification
Output one integer representing the total number of collisions that will occur.
Sample Input 1
1 2
0
0 1
Sample Output 1
1
Explanation for Sample 1
For the purposes of this explanation, assume that in the above diagrams, cell
The initial configuration at time
At time
At time
At time
Sample Input 2
6 9
7 27 0 4 20 -1
-1 -1 9 -1 22 31 1 0 24
Sample Output 2
3
Comments