#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
int R;
int W1,H1,X1,Y1;
int W2,H2,X2,Y2;
int L1[501][501];
int L2[501][501];
int AA[250010];
int BB[250010];
int num11[250010];
int num22[250010];
bool field1[501][501];
bool field2[501][501];
vector<P>XX1;vector<P>XX2;
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
int solve1(int x,int y,int num){
	bool FF=true;
	for(int k=0;k<4;k++){
		if(x+dx[k]<0)continue;
		if(x+dx[k]>=W1)continue;
		if(y+dy[k]<0)continue;
		if(y+dy[k]>=H1)continue;
		if(field1[y+dy[k]][x+dx[k]])FF=false;
	}
	if(FF)return 0;
	int res=0;
	queue<P>que;
	que.push(P(y,x));
	while(!que.empty()){
		P p=que.front();que.pop();
		int pf=p.first;int ps=p.second;
		if(field1[pf][ps])continue;
		field1[pf][ps]=true;res++;
		for(int k=0;k<4;k++){
			if(ps+dx[k]<0)continue;
			if(ps+dx[k]>=W1)continue;
			if(pf+dy[k]<0)continue;
			if(pf+dy[k]>=H1)continue;
			if(L1[pf+dy[k]][ps+dx[k]]>num)continue;
			if(!field1[pf+dy[k]][ps+dx[k]])que.push(P(pf+dy[k],ps+dx[k]));
		}
	}
	return res;
}
int solve2(int x,int y,int num){
	bool FF=true;
	for(int k=0;k<4;k++){
		if(x+dx[k]<0)continue;
		if(x+dx[k]>=W2)continue;
		if(y+dy[k]<0)continue;
		if(y+dy[k]>=H2)continue;
		if(field2[y+dy[k]][x+dx[k]])FF=false;
	}
	if(FF)return 0;
	int res=0;
	queue<P>que;
	que.push(P(y,x));
	while(!que.empty()){
		P p=que.front();que.pop();
		int pf=p.first;int ps=p.second;
		if(field2[pf][ps])continue;
		field2[pf][ps]=true;res++;
		for(int k=0;k<4;k++){
			if(ps+dx[k]<0)continue;
			if(ps+dx[k]>=W2)continue;
			if(pf+dy[k]<0)continue;
			if(pf+dy[k]>=H2)continue;
			if(L2[pf+dy[k]][ps+dx[k]]>num)continue;
			if(!field2[pf+dy[k]][ps+dx[k]])que.push(P(pf+dy[k],ps+dx[k]));
		}
	}
	return res;
}
int main()
{
	while(true){
		XX1.clear();XX2.clear();
		scanf("%d",&R);
		if(R==0)break;
		scanf("%d%d%d%d",&W1,&H1,&X1,&Y1);
		for(int i=0;i<H1;i++){
			for(int j=0;j<W1;j++){
				scanf("%d",&L1[i][j]);
				XX1.push_back(P(L1[i][j],i*W1+j));
			}
		}
		sort(XX1.begin(),XX1.end());
		for(int i=0;i<H1;i++){
			for(int j=0;j<W1;j++){
				field1[i][j]=false;
			}
		}
		field1[Y1-1][X1-1]=true;
		num11[1]=1;
		for(int i=2;i<=XX1.size();i++){
			num11[i]=num11[i-1];
			num11[i]+=solve1(XX1[i-1].second%W1,XX1[i-1].second/W1,XX1[i-1].first);
		}
		for(int i=XX1.size();i>=2;i--){
			if(XX1[i-1].first==XX1[i-2].first){
				num11[i-1]=num11[i];
			}
		}
		for(int i=0;i<XX1.size();i++){
			//printf("%d:%d\n",XX1[i].first,num11[i+1]);
		}
		scanf("%d%d%d%d",&W2,&H2,&X2,&Y2);
		for(int i=0;i<H2;i++){
			for(int j=0;j<W2;j++){
				scanf("%d",&L2[i][j]);
				XX2.push_back(P(L2[i][j],i*W2+j));
			}
		}
		sort(XX2.begin(),XX2.end());
		for(int i=0;i<H2;i++){
			for(int j=0;j<W2;j++){
				field2[i][j]=false;
			}
		}
		field2[Y2-1][X2-1]=true;
		num22[1]=1;
		for(int i=2;i<=XX2.size();i++){
			num22[i]=num22[i-1];
			num22[i]+=solve2(XX2[i-1].second%W2,XX2[i-1].second/W2,XX2[i-1].first);
		}
		for(int i=XX2.size();i>=2;i--){
			if(XX2[i-1].first==XX2[i-2].first){
				num22[i-1]=num22[i];
			}
		}
		for(int i=0;i<XX2.size();i++){
			//printf("%d:%d\n",XX2[i].first,num22[i+1]);
		}
		int ans=1000000000;
		for(int i=0;i<=H1*W1;i++){
			int RRR=R-num11[i];
			int l=0;int r=H2*W2+1;
			while(r-l>1){
				int c=(l+r)/2;
				if(num22[c]<RRR)l=c;
				else r=c;
			}
			l-=H1;
			if(l<0)l=0;
			while(l<=H2*W2&&num11[i]+num22[l]<R)l++;
			if(num11[i]+num22[l]<R)continue;
			//printf("%d %d\n",num11[i],num22[l]);
			int I=i-1;l--;
			if(I==-1)ans=min(ans,XX2[l].first);
			else if(l==-1)ans=min(ans,XX1[I].first);
			else ans=min(ans,XX1[I].first+XX2[l].first);
		}
		printf("%d\n",ans);
	}
}

Measured memory_bytes: 12884