#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<unordered_map>
using namespace std;
typedef long long ll;
ll c[2500001];
ll b[501][501];
struct A {
	ll ko, level;
};
A a[2][250001];
ll u[501][501];
struct P {
	ll x, y, level;
};
ll d[2];
bool operator<(P a, P b) { return a.level < b.level; }
bool operator>(P a, P b) { return a.level > b.level; }
const ll inf = 800000000000000000;
int main() {
	ll r;
	int lp[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
	while (cin >> r&&r != 0) {
		for (int z = 0; z < 2; z++) {
			ll h, w, x, y;
			cin >> w >> h >> y >> x;
			x--;
			y--;
			queue<P>p;
			for (ll i = 0; i < h; i++) {
				for (ll j = 0; j < w; j++) {
					cin >> u[i][j];
					b[i][j] = inf;
				}
			}
			p.push(P{ x,y,1 });
			b[x][y] = 1;
			while (!p.empty()) {
				P t = p.front(); p.pop();
				ll x = t.x, y = t.y;
				if (t.level == b[x][y]) {
					for (int i = 0; i < 4; i++) {
						ll x2 = x + lp[i][0], y2 = y + lp[i][1];
						if (b[x2][y2] > t.level) {
							if (u[x2][y2] <= t.level) {
								b[x2][y2] = t.level;
								p.push(P{ x2,y2,t.level });
							}
							else {
								if (u[x2][y2] < b[x2][y2]) {
									b[x2][y2] = u[x2][y2];
									p.push(P{ x2,y2,u[x2][y2] });
								}
							}
						}
					}
				}
			}
			ll o = 0;
			for (ll i = 0; i < h; i++) {
				for (ll j = 0; j < w; j++) {
					c[o] = b[i][j];
					o++;
				}
			}
			sort(c, c + o);
			ll sum = 0, g = 1;
			a[z][0] = A{ 0,0 };
			for (ll i = 0; i < (h*w);) {
				ll k = c[i], sum2 = 0;
				while (i < (h*w) && c[i] == k) {
					sum2++;
					i++;
				}
				a[z][g] = A{ sum2 + sum,k };
				g++;
				sum += sum2;
			}
			d[z] = g;
		}
		ll sum = inf;
		for (ll i = 0; i < d[0]; i++) {
			ll s = 0, e = d[1] - 1, t, m = r - a[0][i].ko;
			while (e - s > 1) {
				t = (e + s) / 2;
				if (a[1][t].ko <= m)
					s = t;
				if (a[1][t].ko >= m)
					e = t;
			}
			if (a[0][i].ko + a[1][s].ko >= r)
				sum = min(sum, a[0][i].level + a[1][s].level);
			if (a[0][i].ko + a[1][e].ko >= r)
				sum = min(sum, a[0][i].level + a[1][e].level);
		}
		cout << sum << endl;
	}
}