#include <cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define MAX_H 504 
#define MAX_W 504
#define MAX_LEVEL 100000000
#define mp make_pair
#define INF 1001001001
#define ppp pair<int, pair<int,int> >
using namespace std;
#define rep(i,n) for(int i=0; i<n ; i++)
vector< int > LEVEL_ABILITY[2];
vector<int> LEVEL_ABILITY_UNIQUE[2];
vector<int> LEVEL_ABILITY_BOX[2];
int main(void){
    while(1){
        LEVEL_ABILITY_UNIQUE[0].clear();LEVEL_ABILITY_UNIQUE[1].clear();
        LEVEL_ABILITY[0].clear();LEVEL_ABILITY[1].clear();
        LEVEL_ABILITY_BOX[0].clear();LEVEL_ABILITY_BOX[1].clear();
        int R;
        scanf("%d",&R);if(R==0)break;
        rep(i,2){
            int W,H,X,Y;int LEVEL[MAX_H][MAX_W];int DLEVEL[MAX_H][MAX_W];
            scanf("%d %d %d %d",&W,&H,&X,&Y);X--;Y--;
            rep(j,H)rep(k,W){
                scanf("%d",&LEVEL[j][k]);
                DLEVEL[j][k]=INF;
            }
            //探索
            priority_queue< ppp ,vector< ppp > , greater< ppp > > D;
            D.push( mp(1, mp(Y,X) ) );
            while(!D.empty() ){
                pair<int, pair<int,int> >p= D.top(); D.pop();
                int pL=p.first;int pY=p.second.first;int pX=p.second.second; 
                if( pL>=DLEVEL[pY][pX] )continue;
                DLEVEL[pY][pX]=max( pL , LEVEL[pY][pX] );
                if(pY>0)D.push( mp(DLEVEL[pY][pX], mp(pY-1,pX) ) );
                if(pY<H-1)D.push( mp(DLEVEL[pY][pX], mp(pY+1,pX) ) );
                if(pX>0)D.push( mp(DLEVEL[pY][pX], mp(pY,pX-1) ) );
                if(pX<W-1)D.push( mp(DLEVEL[pY][pX], mp(pY,pX+1) ) );
            }
            //LEVEL_ABILITYを作る
            LEVEL_ABILITY[i].push_back(0);
            rep(j,H)rep(k,W){
                LEVEL_ABILITY[i].push_back( DLEVEL[j][k] );
            }
            sort( LEVEL_ABILITY[i].begin(),LEVEL_ABILITY[i].end() );
            //rep(test,LEVEL_ABILITY[i].size() ){printf("%d-",LEVEL_ABILITY[i][test]);}
            LEVEL_ABILITY_UNIQUE[i]=LEVEL_ABILITY[i];
            LEVEL_ABILITY_UNIQUE[i].erase( unique(LEVEL_ABILITY_UNIQUE[i].begin(),LEVEL_ABILITY_UNIQUE[i].end() ) , LEVEL_ABILITY_UNIQUE[i].end() );
            int ite=1;
            rep(j,LEVEL_ABILITY[i].size() ){
                if(j==0){LEVEL_ABILITY_BOX[i].push_back(0);continue;}
                if(LEVEL_ABILITY[i][j]!=LEVEL_ABILITY[i][ite]){LEVEL_ABILITY_BOX[i].push_back(j-1);ite=j;}
                if(j==LEVEL_ABILITY[i].size()-1){
                    LEVEL_ABILITY_BOX[i].push_back(j);
                }
            }LEVEL_ABILITY_UNIQUE[i].push_back(INF);
        }
        //一件目の認定レベルを決め、二回目に何個行けるか調べる。
        int ans=INF;
        rep(i,LEVEL_ABILITY_UNIQUE[0].size() ){
            int firstLV=LEVEL_ABILITY_UNIQUE[0][i];
            int firstscore=LEVEL_ABILITY_BOX[0][i];
            int secondscore=R-firstscore;int secondLV;
            if(secondscore<=0)secondLV=0;
            else{
                int ite=lower_bound(LEVEL_ABILITY_BOX[1].begin(), LEVEL_ABILITY_BOX[1].end(), secondscore )- LEVEL_ABILITY_BOX[1].begin();
                secondLV=LEVEL_ABILITY_UNIQUE[1][ite];
            }
            ans=min(ans,firstLV+secondLV);
        }
        printf("%d\n",ans);
    }
}



Measured memory_bytes: 13164