#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iterator>
#include <queue>
#include <stack>
#include <deque>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <complex>
#include <functional>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <cctype>

#define REP(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define rp(i,c) rep(i,(int)c.size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(u) (u).rbegin(),(u).rend()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl
#define v_delete(a,b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector< T > >
#define init(a,b) memset((a), (b), sizeof((a)))

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double EPS=1e-9;
const long long INF = 100001000010000100ll;

template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}

template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
typedef pair<int,int> pii;

int R;
int W[2], H[2], X[2], Y[2];

int m[4][500+10][500+10];
int visited[2][500+10][500+10];

int wa[2][500*500*2+10];

vi ls;

map<int ,int> cnt[2];

bool exists(const map<int ,int> &mapp, int key){
	return mapp.find(key) != mapp.end();
}

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

class BIT{
private:
	vi bit;
	int n;
public:
	BIT(int _n){
		n = _n;
		bit.assign(n+1,0 );
	}
	void add(int i, int x){
		while(i <= n){
			bit[i] += x;
			i += (i & -i);
		}
	}
	int sum(int i){
		int s = 0;
		while(i > 0){
			s += bit[i];
			i -= (i & -i);
		}
		return s;
	}
};


int main(){
	while(cin >> R, R){

		memset(visited, 0, sizeof(visited));

		memset(wa, 0, sizeof(wa));

		ls.clear();

		fill_n(**m, 4 * (500+10) * (500+10), inf);

		rep(c, 2){
			cin >> W[c] >> H[c] >> X[c] >> Y[c];
			rep(i, H[c]){
				rep(j, W[c]){
					cin >> m[c][i+1][j+1];
					ls.pb(m[c][i+1][j+1]);
				}
			}
		}
		ls.pb(0);
		ls.pb(inf);
		sort(all(ls));
		ls.erase(unique(all(ls)), ls.end());
		rep(c, 2){
			priority_queue<pair<int, pii>, vector<pair<int, pii> >, greater<pair<int, pii> > >Q;
			Q.push(mp(1, mp(X[c], Y[c])));
			while(!Q.empty()){
				int x,y,level;
				x=Q.top().second.first;y=Q.top().second.second;level=Q.top().first;
				Q.pop();
				if(visited[c][y][x]){
					continue;
				}
				visited[c][y][x] = 1;

				if(level > m[c][y][x]){
					m[c][y][x] = level;
				}else{
					level = m[c][y][x];
				}
				//printf("%d %d %d %d\n",c, x, y, level);
				rep(k, 4){
					if(m[c][y+dy[k]][x+dx[k]]!=inf){
						if(!visited[c][y+dy[k]][x+dx[k]]){
							Q.push(mp(max(m[c][y+dy[k]][x+dx[k]], level), mp(x+dx[k], y+dy[k])));
						}
					}
				}

			}
		}/*
		rep(c, 2){
			rep(i, H[c]){
				rep(j, W[c]){
					cout <<  m[c][i+1][j+1] << " ";
				}
				cout << endl;
			}
			cout << endl;
			cout << endl;
			cout << endl;
		}*/
		rep(c, 2){
			rep(i, H[c]){
				rep(j, W[c]){
					int ma=-1;
					int tmp=inf;
					/*
					rep(k, 4){
						tmp=min(tmp, m[c][i + 1 + dx[k]][j + 1 + dy[k]]);
					}
					if(i + 1 == Y[c] && j + 1 == X[c]){
						ma=m[c][i + 1][j + 1];
					}else{
						ma = max(m[c][i + 1][j + 1], tmp);
					}*/
					ma=m[c][i + 1][j + 1];
					int l = lower_bound(all(ls), ma) - ls.begin();
					wa[c][l]++;

				}
			}
			rp(i, ls){
				if(i - 1 >= 0)
					wa[c][i] = wa[c][i - 1] + wa[c][i];
			}

		}
		/*
		rep(c, 2){
			rp(i, ls){
				cout << ls[i] << " " << wa[c][i] << endl;
			}
			cout << endl;
			cout << endl;
		}*/
		int ans=inf;
		rp(i, ls){

			int l=lower_bound(wa[1],(wa[1]+(int)ls.size()) , R - wa[0][i]) - wa[1];
			if(l < ls.size())
			if(wa[0][i] + wa[1][l] >= R){
				//cout << ls[i] << " " << ls[l] << endl;
				ans=min(ans, ls[i] + ls[l]);
			}
		}
		cout << ans << endl;
	}

	return 0;

}

Measured memory_bytes: 14480