#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <complex>
#include <cstdio>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORE(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,a) FOR(i,0,a)
#define SQR(a) ((a)*(a))
#define X first
#define Y second
using namespace std;
typedef long long int ll;
typedef pair<int,int> pr;
typedef pair<pr,ll> ppr;
typedef vector<pr> vpr;
typedef pair<int,int> ci;
typedef pair<int,ci> pci;
typedef priority_queue<pci,vector<pci>,greater<pci> > pq;
const int INF=1<<30;
bool finished[502][502];
int level[502][502];
ci dc[4]={ci(1,0),ci(0,1),ci(-1,0),ci(0,-1)};
int main() {
	int R;
	while(cin >> R && R){
	vpr ables[2];
	REP(l,2){
		int W,H,sx,sy,mlevel=0,sum=0;
		cin >> W  >>H >>sx >> sy;
		FORE(i,1,H)
			FORE(j,1,W)
				scanf("%d",&level[i][j]);
		FORE(i,0,H+1)
			FORE(j,0,W+1)
				if(i&&j&&i<=H&&j<=W)
					finished[i][j]=false;
				else
					finished[i][j]=true;
		finished[sy][sx]=false;
		level[sy][sx]=INF;
		pq dungeon;
		dungeon.push(pci(1,ci(sx,sy)));
		while(!dungeon.empty()){
			ci point=dungeon.top().second;
			int nowlevel=dungeon.top().first;
			dungeon.pop();
			if(mlevel<nowlevel){
				ables[l].push_back(pr(sum,mlevel));
				mlevel=nowlevel;
			}
			sum++;
			REP(i,4){
				ci d=dc[i];
				if(!finished[point.Y+d.Y][point.X+d.X]){
					finished[point.Y+d.Y][point.X+d.X]=true;
					dungeon.push(pci(level[point.Y+d.Y][point.X+d.X],ci(point.X+d.X,point.Y+d.Y)));
				}
				
			}
		}
	}
	int e=ables[1].size()-1,minium=INF;
	REP(i,ables[0].size()){
		while(e && ables[0][i].first+ables[1][e-1].first>=R) e--;
		if(ables[0][i].first+ables[1][e].first>=R)
			minium=min(minium,ables[0][i].second+ables[1][e].second);
	}
	cout << minium << endl;
	}
	// your code goes here
	return 0;
}