#include<iostream>
#include<queue>
#include<functional>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

#define T1 1000000LL
#define T2 1000LL

long long W, H, X, Y, R;
long long x[1000][1000];
long long d[1000][1000];
long long p1, p2, p3;
long long y_1[1000000], y_2[1000000], cnt, cnt2, v, minx;

priority_queue<long long, vector<long long>, greater<long long>> Q, S;

int main() {
	while (true) {
		cnt = 1; cnt2 = 1; minx = 1145141919810LL;
		memset(x, 51, sizeof(x));
		memset(d, 51, sizeof(d));
		cin >> R;
		if (R == 0) { break; }
		cin >> W >> H >> X >> Y;
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				scanf("%lld",&x[i][j]);
			}
		}
		d[Y][X] = 0;
		Q.push(Y*T2 + X);
		while (!Q.empty()) {
			p1 = Q.top() / T1;
			p2 = (Q.top() / T2) % T2;
			p3 = Q.top() % T2;
			if (max(p1, x[p2 - 1][p3]) < d[p2 - 1][p3]) {
				d[p2 - 1][p3] = max(p1, x[p2 - 1][p3]);
				S.push(d[p2 - 1][p3] * T1 + (p2 - 1)*T2 + p3);
			}
			if (max(p1, x[p2 + 1][p3]) < d[p2 + 1][p3]) {
				d[p2 + 1][p3] = max(p1, x[p2 + 1][p3]);
				S.push(d[p2 + 1][p3] * T1 + (p2 + 1)*T2 + p3);
			}
			if (max(p1, x[p2][p3 - 1]) < d[p2][p3 - 1]) {
				d[p2][p3 - 1] = max(p1, x[p2][p3 - 1]);
				S.push(d[p2][p3 - 1] * T1 + p2*T2 + (p3 - 1));
			}
			if (max(p1, x[p2][p3 + 1]) < d[p2][p3 + 1]) {
				d[p2][p3 + 1] = max(p1, x[p2][p3 + 1]);
				S.push(d[p2][p3 + 1] * T1 + p2*T2 + (p3 + 1));
			}
			Q.pop();
			while (!S.empty()) {
				Q.push(S.top());
				S.pop();
			}
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				y_1[cnt] = d[i][j];
				cnt++;
			}
		}
		memset(x, 51, sizeof(x));
		memset(d, 51, sizeof(d));
		cin >> W >> H >> X >> Y;
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				scanf("%lld",&x[i][j]);
			}
		}
		d[Y][X] = 0;
		Q.push(Y*T2 + X);
		while (!Q.empty()) {
			p1 = Q.top() / T1;
			p2 = (Q.top() / T2) % T2;
			p3 = Q.top() % T2;
			if (max(p1, x[p2 - 1][p3]) < d[p2 - 1][p3]) {
				d[p2 - 1][p3] = max(p1, x[p2 - 1][p3]);
				S.push(d[p2 - 1][p3] * T1 + (p2 - 1)*T2 + p3);
			}
			if (max(p1, x[p2 + 1][p3]) < d[p2 + 1][p3]) {
				d[p2 + 1][p3] = max(p1, x[p2 + 1][p3]);
				S.push(d[p2 + 1][p3] * T1 + (p2 + 1)*T2 + p3);
			}
			if (max(p1, x[p2][p3 - 1]) < d[p2][p3 - 1]) {
				d[p2][p3 - 1] = max(p1, x[p2][p3 - 1]);
				S.push(d[p2][p3 - 1] * T1 + p2*T2 + (p3 - 1));
			}
			if (max(p1, x[p2][p3 + 1]) < d[p2][p3 + 1]) {
				d[p2][p3 + 1] = max(p1, x[p2][p3 + 1]);
				S.push(d[p2][p3 + 1] * T1 + p2*T2 + (p3 + 1));
			}
			Q.pop();
			while (!S.empty()) {
				Q.push(S.top());
				S.pop();
			}
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				y_2[cnt2] = d[i][j];
				cnt2++;
			}
		}

		sort(y_1, y_1 + cnt);
		sort(y_2, y_2 + cnt2);

		for (int i = 0; i < cnt; i++) {
			v = R - i;
			if (v >= 0 && v < cnt2) {
				minx = min(minx, y_1[i] + y_2[v]);
			}
		}
		cout << minx << endl;
	}
}