#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
void reader(int *x) {
	int k = getchar_unlocked(); *x = k - '0';
	while (1) {
		k = getchar_unlocked();
		if (k < '0' || k > '9') break;
		*x = ((*x) << 1) + ((*x) << 3) + k - '0';
	}
}
struct state {
	int sx, sy, cost;
};
bool operator<(const state& s1, const state& s2) {
	return s1.cost < s2.cost;
}
int R, H[2], W[2], sx[2], sy[2], a[2][509][509], cnt[2], b[2][250009]; bool vis[509][509];
int dir[] = { 0, 1, 0, -1 };
int main() {
	while (scanf("%d", &R), R) {
		for (int i = 0; i < 2; i++) {
			scanf("%d%d%d%d", &W[i], &H[i], &sy[i], &sx[i]); sy[i]--, sx[i]--;
			for (int j = 0; j < H[i]; j++) {
				for (int k = 0; k < W[i]; k++) {
					reader(&a[i][j][k]);
				}
			}
			priority_queue<state> que; que.push(state{ sx[i], sy[i], -1 });
			memset(vis, 0, sizeof(vis));
			vis[sx[i]][sy[i]] = true; cnt[i] = 1;
			while (!que.empty()) {
				state u = que.top(); que.pop();
				b[i][cnt[i]++] = a[i][u.sx][u.sy];
				for (int j = 0; j < 4; j++) {
					int tx = u.sx + dir[j], ty = u.sy + dir[j ^ 1];
					if (0 <= tx && tx < H[i] && 0 <= ty && ty < W[i] && !vis[tx][ty]) {
						a[i][tx][ty] = max(a[i][tx][ty], a[i][u.sx][u.sy]);
						vis[tx][ty] = true;
						que.push(state{ tx, ty, -a[i][tx][ty] });
					}
				}
			}
		}
		int ret = 2000000009;
		for (int i = 0; i <= H[0] * W[0]; i++) {
			int z = max(R - i, 0);
			if (z > H[1] * W[1]) continue;
			ret = min(ret, b[0][i] + b[1][z]);
		}
		printf("%d\n", ret);
	}
	return 0;
}