#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct pqitem{
	int v,x,y;
	bool operator<(pqitem b)const
	{return v>b.v;}
};
int n;
int w1,h1,x1,y1;
int w2,h2,x2,y2;
int g[2][500][500];
int c[2][250000];
vector<pqitem> fourway(int x,int y,int w,int h)
{
	vector<pqitem> ans;
	if(x>0)ans.push_back({0,x-1,y});
	if(y>0)ans.push_back({0,x,y-1});
	if(x<h-1)ans.push_back({0,x+1,y});
	if(y<w-1)ans.push_back({0,x,y+1});
	return ans;
}
void bfs(int i,int x,int y,int w,int h)
{
	bool u[500][500]={};
	priority_queue<pqitem> q;
	q.push({1,x,y});
	u[x][y]=1;
	int j=1;
	c[i][0]=0;
	while(!q.empty())
	{
		pqitem now=q.top();
		q.pop();
		c[i][j++]=now.v;
		vector<pqitem> fw=fourway(now.x,now.y,w,h);
		for(pqitem &t:fw)
			if(!u[t.x][t.y])
			{
				t.v=g[i][t.x][t.y];
				q.push(t);
				u[t.x][t.y]=1;
			}
	}
	for(int k=1;k<j;k++)
		c[i][k]=max(c[i][k],c[i][k-1]);
	for(int k=j;k<=n;k++)
		c[i][k]=1e9;
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>n;
	cin>>w1>>h1>>x1>>y1;
	for(int i=0;i<h1;i++)
		for(int j=0;j<w1;j++)
			cin>>g[0][i][j];
	cin>>w2>>h2>>x2>>y2;
	for(int i=0;i<h2;i++)
		for(int j=0;j<w2;j++)
			cin>>g[1][i][j];
	x1--; y1--; x2--; y2--;
	swap(x1,y1); swap(x2,y2);
	bfs(0,x1,y1,w1,h1);
	bfs(1,x2,y2,w2,h2);
	int ans=1e9;
	for(int i=0;i<=n;i++)
	{
		ans=min(ans,c[0][i]+c[1][n-i]);
	}
	cout<<ans<<endl;
}
