#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <unordered_set>
//#include <unordered_map>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
vvint vv1;
vvint vv2;
vvint dp1;
vvint dp2;
int dd[]={1,0,-1,0,1};
int w1,h1,x1,y1;
int w2,h2,x2,y2;
void foo1(int x,int y,int c){
	c=max(c,vv1[y][x]);
	if(dp1[y][x]<=c) return;
	dp1[y][x]=c;
	rep(i,4){
		int yy=y+dd[i];
		int xx=x+dd[i+1];
		if(0<=xx&&xx<w1&&0<=yy&&yy<h1){
			foo1(xx,yy,c);
		}
	}
}
void foo2(int x,int y,int c){
	c=max(c,vv2[y][x]);
	if(dp2[y][x]<=c) return;
	dp2[y][x]=c;
	rep(i,4){
		int yy=y+dd[i];
		int xx=x+dd[i+1];
		if(0<=xx&&xx<w2&&0<=yy&&yy<h2){
			foo2(xx,yy,c);
		}
	}
}
void mainmain(){
	int R;
	while(cin>>R,R){
		map<int,int> ma1;
		map<int,int> ma2;
		cin>>w1>>h1>>x1>>y1;
		initvv(vv1,h1,w1);
		initvv(dp1,h1,w1,INF);
		rep(i,h1){
			rep(j,w1){
				cin>>vv1[i][j];
			}
		}
		cin>>w2>>h2>>x2>>y2;
		initvv(vv2,h2,w2);
		initvv(dp2,h2,w2,INF);
		rep(i,h2){
			rep(j,w2){
				cin>>vv2[i][j];
			}
		}
		priority_queue<pair<int,pii>> pq1;
		priority_queue<pair<int,pii>> pq2;
		pq1.push(mkp(-1,pii(y1-1,x1-1)));
		pq2.push(mkp(-1,pii(y2-1,x2-1)));
		while(pq1.size()){
			pair<int,pii> top=pq1.top();
			pq1.pop();
			int yy=top.S.F;
			int xx=top.S.S;
			top.F=min(top.F,-vv1[yy][xx]);
			if(dp1[yy][xx]!=INF){
				continue;
			}
			dp1[yy][xx]=-top.F;
			rep(i,4){
				int ny=yy+dd[i];
				int nx=xx+dd[i+1];
				if(0<=ny&&ny<h1&&0<=nx&&nx<w1){
					if(dp1[ny][nx]==INF){
						pq1.push(mkp(top.F,pii(ny,nx)));
					}
				}
			}
		}
		// rep(i,h1){
			// rep(j,w1){
				// cout<<dp1[i][j]<<" ";
			// }
			// cout<<endl;
		// }
		while(pq2.size()){
			pair<int,pii> top=pq2.top();
			pq2.pop();
			int yy=top.S.F;
			int xx=top.S.S;
			top.F=min(top.F,-vv2[yy][xx]);
			if(dp2[yy][xx]!=INF){
				continue;
			}
			dp2[yy][xx]=-top.F;
			rep(i,4){
				int ny=yy+dd[i];
				int nx=xx+dd[i+1];
				if(0<=ny&&ny<h2&&0<=nx&&nx<w2){
					if(dp2[ny][nx]==INF){
						pq2.push(mkp(top.F,pii(ny,nx)));
					}
				}
			}
		}
		// foo1(x1-1,y1-1,1);
		// foo2(x2-1,y2-1,1);
		rep(i,h1){
			rep(j,w1){
				if(ma1.count(dp1[i][j])==0) ma1[dp1[i][j]]=0;
				ma1[dp1[i][j]]++;
			}
		}
		rep(i,h2){
			rep(j,w2){
				if(ma2.count(dp2[i][j])==0) ma2[dp2[i][j]]=0;
				ma2[dp2[i][j]]++;
			}
		}
		vector<pii> v1;
		vector<pii> v2;
		int t=0;
		v1.PB(pii(0,0));
		v2.PB(pii(0,0));
		for(auto i:ma1){
			t+=i.S;
			v1.PB(pii(t,i.F));
		}
		t=0;
		for(auto i:ma2){
			t+=i.S;
			v2.PB(pii(t,i.F));
		}
		int ans=INT_MAX;
		rep(i,v1.size()){
			int tmp=v1[i].S;
			auto it=lower_bound(ALL(v2),pii(R-v1[i].F,0));
			if(it==v2.end()) continue;
			pii tt = *it;
			// cout<<v1[i].F<<" "<<v1[i].S<<" "<<tt.F<<" "<<tt.S<<endl;
			ans=min(ans,tmp+tt.S);
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

Measured memory_bytes: 18844