#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1<<29;
int R;
int n[2], m[2];
int s[2], t[2];
int fi[2][510][510], fi2[510][510];
int need[2][510][510], need2[510][510];

int main() {
    while (cin >> R) {
        if (R == 0) break;
        for (int it = 0; it < 2; ++it) {
            cin >> m[it] >> n[it] >> t[it] >> s[it]; --t[it]; --s[it];
            for (int i = 0; i < n[it]; ++i) for (int j = 0; j < m[it]; ++j) cin >> fi[it][i][j];
        }
        
        for (int it = 0; it < 2; ++it) {
            for (int i = 0; i < 510; ++i) for (int j = 0; j < 510; ++j) need[it][i][j] = INF;
            need[it][s[it]][t[it]] = 1;
            priority_queue<pair<int,pint>, vector<pair<int,pint> >, greater<pair<int,pint> > > que;
            que.push( MP(1,pint(s[it],t[it])) );
            while (!que.empty()) {
                int dist = que.top().first;
                int x = que.top().second.first;
                int y = que.top().second.second;
                que.pop();
            
                if (need[it][x][y] < dist) continue;
                
                //cout << it << ", " << MP(x,y) << " : " << dist << endl;
                
                for (int i = 0; i < 4; ++i) {
                    int nx = x + dx[i], ny = y + dy[i];
                    if (nx < 0 || nx >= n[it] || ny < 0 || ny >= m[it]) continue;
                    int num = max(fi[it][nx][ny], dist);
                    if (chmin(need[it][nx][ny], num)) {
                        que.push( MP(need[it][nx][ny],pint(nx,ny)) );
                    }
                }
            }
        }
        map<int,int> ma[2];
        for (int it = 0; it < 2; ++it) {
            ma[it][0] = 0;
            for (int i = 0; i < n[it]; ++i) for (int j = 0; j < m[it]; ++j) ma[it][need[it][i][j]]++;
            int pre = 0;
            EACH(t, ma[it]) {
                t->second += pre;
                pre = t->second;
            }
            //COUT(ma[it]);
        }
        vector<pint> rem;
        EACH(t, ma[1]) {
            rem.PB( pint(t->second, t->first) );
        }
        rem.PB( pint(0,0) );
        rem.PB( pint(INF,INF) );
        sort(ALL(rem));
        
        int res = INF;
        EACH(t, ma[0]) {
            int r = R - t->second;
            int level1 = t->first;
            int id = lower_bound(ALL(rem), pint(r,0)) - rem.begin();
            int level2 = rem[id].second;
            
            chmin(res, level1 + level2);
        }
        
        cout << res << endl;
    }
    return 0;
}


Measured memory_bytes: 17988