#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
constexpr int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int r;
	while (cin >> r, r) {
		vi h(2), w(2), x(2), y(2);
		vector<vvi> v(2);
		REP(i, 2) {
			cin >> w[i] >> h[i] >> y[i] >> x[i];
			x[i]--; y[i]--;
			vvi tmp(h[i], vi(w[i]));
			REP(k, h[i])REP(j, w[i]) {
				cin >> tmp[k][j];
			}
			v[i] = tmp;
		}
		vvi c(2, vi(r + 1, INF));
		REP(a, 2) {
			auto tmp = [&](pii l, pii r) {
				return v[a][l.first][l.second] > v[a][r.first][r.second];
			};
			priority_queue<pii, vector<pii>, decltype(tmp)> q(tmp);
			q.push({ x[a],y[a] });
			vvi b(h[a], vi(w[a], 0));
			b[x[a]][y[a]] = 1;
			int cnt = 0;
			c[a][0] = 0;
			while (!q.empty()) {
				pii p = q.top(); q.pop();
				cnt++;
				chmin(c[a][min(r, cnt)], v[a][p.first][p.second]);
				REP(k, 4) {
					int ni = p.first + dx[2 * k], nj = p.second + dy[2 * k];
					if (ni < 0 || nj < 0 || ni >= h[a] || nj >= w[a] || b[ni][nj] == 1)continue;
					b[ni][nj] = 1;
					q.push({ ni,nj });
				}
			}
		}
		REP(a, 2)REP(i, r)chmax(c[a][i + 1], c[a][i]);
		int ans = INF;
		REP(i, r + 1) {
			chmin(ans, c[0][i] + c[1][r - i]);
		}
		cout << ans << endl;
	}
}
