You work as a courier in Alba Iulia. The city is represented as a matrix with rows and columns. Initially, the congestion level in each intersection (cell) of the matrix is .
Today is City Day, and the city hall has approved street events. Each event partially blocks a rectangular area, defined by the top-left corner and the bottom-right corner , adding a value to the congestion level of all cells in that area. If the areas overlap, the congestion level is cumulative.
The headquarters of the company where you pick up the packages is located at coordinates . Your scooter has a maximum crowd navigation power equal to . This means you cannot enter a cell where the congestion level is strictly greater than .
Traversing from a valid cell to another valid adjacent cell (Up, Down, Left, Right) takes exactly 1 minute.
Task
You received a list of orders. For each order, you must leave the central headquarters and reach the client located at coordinates . Calculate the minimum time (in minutes) needed to reach each of the clients. (Deliveries are independent, it is considered that for each order you always start from the headquarters).
Input data
The first line contains five integer numbers and .
The second line contains two integer numbers and , representing the coordinates of the headquarters.
The next lines contain five integer numbers each: and , representing the coordinates of the areas where events take place and the added congestion level.
The next lines contain two integer numbers each and , representing the coordinates where the clients are waiting.
Output data
You will print lines. The -th line will contain a single integer number, representing the minimum time needed to reach the -th client. If it is impossible to reach that client, it will print .
Constraints and clarifications
- It is guaranteed that the headquarters has a final congestion level .
- For of the score: and
Example
aglomeratie.in
4 4 2 3 5
4 4
1 1 2 2 4
2 2 3 3 3
1 4
2 2
3 1
aglomeratie.out
3
-1
4
Explanation
The initial matrix has a dimension of . The scooter's power limit is . The headquarters is at .
After applying the street events, the congestion matrix looks like this:
4 4 0 0
4 7 3 0
0 3 3 0
0 0 0 0
We observe that cell has a congestion of , so it is completely forbidden because .
- Client 1 is located at : The optimal path is . Time = minutes.
- Client 2 is located at : It is located in a completely blocked intersection. Time = .
- Client 3 is located at : The path bypasses the crowded center: . Time = minutes.