Problem
Do you know the company Just Odd Inventions? The business of this company is to make "just odd inventions". Here, we call it JOI Company for short.
JOI Company has $2$ offices, and each office consists of square rooms of the same size arranged in a grid. Between every pair of rooms that share an edge, there is a door equipped with an ID card authentication function. Since JOI Company handles confidential information of various levels, a positive integer called a security level is set for each room, and one cannot enter a room unless the non-negative integer authentication level specified for that office on the ID card is greater than or equal to the room's security level. The only entrance/exit of each office is the unique elevator hall, and the security level of the elevator hall room is the lowest, which is $1$. When the authentication level for that office is $0$, one cannot even enter the elevator hall.
At JOI Company, due to a sudden proposal by the president, it has been decided to hold a tour for the general public to visit the company. You need to decide the combination of authentication levels on the ID card to hand to the visitors. Whenever a visitor finds a door that can be opened, they will always open it and enter the room (it is also possible to visit the same room multiple times). Because of this, you do not want to make the authentication levels on the visitor's ID card higher than necessary. However, to make the tour attractive, the tour must allow the visitors to visit at least $R$ rooms in total across the $2$ offices, including the elevator hall rooms. If the authentication levels on the ID card are set too low, it might not be possible to satisfy this condition.
JOI Company has $2$ offices, and the rooms in the $k$-th office ($k = 1, 2$) are arranged with $W_k$ rooms in the east-west direction and $H_k$ rooms in the north-south direction, for a total of $W_k \times H_k$ rooms. The $i$-th room from the west and $j$-th room from the north is denoted by $(i, j)_k$.
Given the values of $W_k$, $H_k$, and $R$, the positions of the elevator halls $(X_k, Y_k)_k$, and the security levels of each room, write a program to find the minimum possible sum of the authentication levels on the visitor's ID card so that the visitor can visit at least $R$ rooms in total across the $2$ offices.
Incidentally, how JOI Company makes a profit by making "just odd inventions" is top secret even within the company, and nobody knows except the president.
Input
- The $1$st line contains a positive integer $R$ ($1 \le R \le 100000$).
- From the $2$nd line onward, the data for the $2$ offices are given in order.
- The data for each office consists of positive integers $W_k, H_k, X_k, Y_k$ ($1 \le X_k \le W_k \le 500, 1 \le Y_k \le H_k \le 500$) on the first line, followed by $H_k$ lines where the $i$-th number on the $j$-th line is an integer $L_{k, i, j}$ ($1 \le L_{k, i, j} < 100000000 = 10^8$) representing the security level of room $(i, j)_k$.
- Furthermore, $R \le W_1 \times H_1 + W_2 \times H_2$ holds.
Of the test data, for $30\%$ of the score, $R, W_k, H_k \le 100$ hold.
Output
Output a single line containing only the minimum possible sum of authentication levels on the ID card to be found.
Sample 1
5
2 2 1 2
9 5
1 17
3 2 2 1
6 1 20
8 18 3
15
Sample 2
8
5 4 1 3
5 5 4 5 5
8 2 1 9 7
1 1 3 5 1
7 2 7 1 3
6 5 6 2
2 3 5 8 2 7
1 6 9 4 5 1
2 4 5 4 2 2
5 4 2 5 3 3
7 1 5 1 5 6
4
Sample 3
6
3 3 2 2
2 9 2
9 1 9
2 9 2
2 2 1 1
1 3
5 7
9
Sample Explanation
In Sample 1, if the authentication levels on the ID card handed to the visitor are set so that the authentication level for Office $1$ is $9$ and the authentication level for Office $2$ is $6$, the visitor can visit $5$ rooms (rooms $(1, 1)_1, (1, 2)_1, (2, 1)_1$ in Office $1$, and rooms $(1, 1)_2, (2, 1)_2$ in Office $2$). At this time, the sum of the authentication levels is $15$. This is the minimum sum of authentication levels required to be able to visit at least $5$ rooms in total.

In Sample 3, if the authentication levels on the ID card handed to the visitor are set so that the authentication level for Office $1$ is $9$ and the authentication level for Office $2$ is $0$, the visitor can visit $9$ rooms (rooms $(1, 1)_1, (1, 2)_1, (1, 3)_1, (2, 1)_1, (2, 2)_1, (2, 3)_1, (3, 1)_1, (3, 2)_1, (3, 3)_1$ in Office $1$). Not even $1$ room in Office $2$ can be visited (they cannot even enter the elevator hall $(1, 1)_2$). At this time, the sum of the authentication levels is $9$. This is the minimum sum of authentication levels required to be able to visit at least $6$ rooms in total.
