import core.stdc.stdio;
import std.stdio;
import std.container;
import std.typecons;
import std.algorithm;

int[] CalcTable(){
	int w,h,x,y;
	scanf("%d%d%d%d",&w,&h,&x,&y);
	int[][] map = new int[][](h,w);
	foreach(i;0..h)
		foreach(j;0..w)
			scanf("%d",&map[i][j]);
	int[] ret = new int[h*w+1];
	ret[0]=0;
	alias Tuple!(int,"cost",int,"x",int,"y") cxy;
	auto pq = BinaryHeap!(cxy[],"a>b")(new cxy[w*h],0);
	bool[][] used = new bool[][](h,w);
	pq.insert(cxy(1,--x,--y));
	used[y][x]=true;
	int c;
	static immutable int[] dxy = [0,-1,0,1,0];
	int b=0;
	while(!pq.empty){
		auto t=pq.front;
		pq.removeFront;
		b=max(b,t.cost);
		ret[++c]=b;
		foreach(i;0..4){
			int nx=t.x+dxy[i];
			int ny=t.y+dxy[i+1];
			if(0<=nx&&nx<w&&0<=ny&&ny<h)
				if(!used[ny][nx]){
					pq.insert(cxy(map[ny][nx],nx,ny));
					used[ny][nx]=true;
				}
		}
	}
	return ret;
}
	
void main(){
	while(1){
		int r;
		scanf("%d",&r);
		if(r==0)
			break;
		int[] r1 = CalcTable;
		int[] r2 = CalcTable;
		int ans=1145141919;
		foreach(int i,p;r1){
			int j=r-i;
			if(j<r2.length)
				ans = min(ans,p+r2[j]);
		}
		printf("%d\n",ans);
	}
}