#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<map>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

vector<int> need[2];
int masu[555][555];
int visit[555][555];
class S{
public:
  int x,y;
  int n;
  S(int x,int y,int n):x(x),y(y),n(n){};
  bool operator<(const S& a)const{
    return n>a.n;
  }
};
int main(){
  while(1){
  int n;
  cin>>n;
  if(n==0)break;
  int dx[]={1,0,-1,0};
  int dy[]={0,1,0,-1};
  need[0].clear();
  need[1].clear();
  rep(p,2){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    rep(i,b){
      rep(j,a){
	cin>>masu[j][i];
      }
    }
    rep(i,b){
      rep(j,a){
	visit[j][i]=0;
      }
    }
    priority_queue<S> que;
    que.push(S(c-1,d-1,1));
    need[p].push_back(0);
    int now=-1;
    while(1){
      if(que.empty()==1){
	break;
      }
      S u=que.top();
      que.pop();
      if(u.x<0 || u.y<0||u.y>=b||u.x>=a)continue;
      if(visit[u.x][u.y]==1){
	continue;
      }
      //printf("!%d %d %d\n",u.x,u.y,u.n);
      visit[u.x][u.y]=1;
      now=max(now,masu[u.x][u.y]);
      need[p].push_back(now);
      
      rep(i,4){
	que.push(S(u.x+dx[i],u.y+dy[i],masu[u.x+dx[i]][u.y+dy[i]]));
      }
    }
  }
  /*
  rep(i,2){
    rep(j,need[i].size()){
      printf("%d\n",need[i][j]);
    }puts("");
    }*/
  int ans=101000000;
  rep(i,max(need[0].size(),need[1].size())){
    if(i>=need[0].size())continue;
    if(n-i<0)continue;
    if(n-i>=need[1].size())continue;
    ans=min(ans,need[0][i]+need[1][n-i]);
  }
  printf("%d\n",ans);
}
}