#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

typedef pair<int,pair<int,int> > pp;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
int level[2][100001];
int room[500][500];
bool visited[500][500];

int main(){
    int r;
    while(cin>>r,r){
        int w,h,ex,ey;
        int e[2];
        for(int i=0;i<2;++i){
            cin>>w>>h>>ex>>ey;
            for(int j=0;j<h;++j){
                for(int k=0;k<w;++k){
                    cin>>room[j][k];
                }
            }
            memset(visited,0,sizeof(visited));
            visited[--ey][--ex]=1;
            level[i][0]=0;
            int mlevel=0,index=1;
            priority_queue<pp,vector<pp>,greater<pp> > pq;
            pq.push(make_pair(room[ey][ex],make_pair(ex,ey)));
            while(!pq.empty()){
                pp cur=pq.top();pq.pop();
                mlevel=max(mlevel,cur.first);
                level[i][index++]=mlevel;
                if(index>r) break;
                for(int j=0;j<4;++j){
                    int cx=cur.second.first+dx[j];
                    int cy=cur.second.second+dy[j];
                    if(cx>=0&&cx<w&&cy>=0&&cy<h&&visited[cy][cx]==0){
                        visited[cy][cx]=1;
                        pq.push(make_pair(room[cy][cx],make_pair(cx,cy)));
                    }
                }
            }
            e[i]=index-1;
        }
        int ans = 1000000000;
        for(int i=0;i<=r;++i){
            if(e[0]<r-i||e[1]<i) continue;
            ans=min(ans,level[1][i]+level[0][r-i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}