#include<bits/stdc++.h>
#define int long long
#define p pair<int,int>
#define P pair<int,p>
using namespace std;
 
int x[4] = { -1,0,0,1 }, y[4] = { 0,-1,1,0 };
int f[500][500];
bool i[500][500];
vector<p>l,r;
signed main() {
    int a;
    while (cin >> a, a) {
        l.clear(); r.clear();
        l.push_back(p(0, 0)); r.push_back(p(0, 0));
        int b, c, d, e; cin >> c >> b >> e >> d;
        d--; e--;
        for (int g = 0; g < b; g++) {
            for (int h = 0; h < c; h++) {
                scanf("%lld", &f[g][h]);
            }
        }
        memset(i, true, sizeof(i));
        int sum = 0, MAX = 0;
        priority_queue<P, vector<P>, greater<P>>Q;
        Q.push(P(1, p(d, e)));
        while (Q.size()) {
            P o = Q.top(); Q.pop();
            MAX = o.first; 
            if(i[o.second.first][o.second.second])sum++;
            i[o.second.first][o.second.second] = false;
            queue<p>q;
            q.push(p(o.second.first, o.second.second));
            while (q.size()) {
                p t = q.front(); q.pop();
                for (int i = 0; i < 4; i++) {
                    int dx = t.first + x[i], dy = t.second + y[i];
                    if (dx >= 0 && dx < b&&dy >= 0 && dy < c&&::i[dx][dy]) {
                        if (f[dx][dy] <= o.first) {
                            sum++;
                            q.push(p(dx,dy));
                            ::i[dx][dy] = false;
                        }
                        else {
                            Q.push(P(f[dx][dy],p(dx,dy)));
                        }
                    }
                }
            }
            l.push_back(p(sum, MAX));
        }
        cin >> c >> b >> e >> d;
        d--; e--;
        for (int g = 0; g < b; g++) {
            for (int h = 0; h < c; h++) {
                scanf("%lld", &f[g][h]);
            }
        }
        memset(i, true, sizeof(i));
        sum = 0; MAX = 0;
        priority_queue<P, vector<P>, greater<P>>U;
        U.push(P(1, p(d, e)));
        while (U.size()) {
            P o = U.top(); U.pop();
            MAX = o.first;
            if (i[o.second.first][o.second.second])sum++;
            i[o.second.first][o.second.second] = false;
            queue<p>q;
            q.push(p(o.second.first, o.second.second));
            while (q.size()) {
                p t = q.front(); q.pop();
                for (int i = 0; i < 4; i++) {
                    int dx = t.first + x[i], dy = t.second + y[i];
                    if (dx >= 0 && dx < b&&dy >= 0 && dy < c&&::i[dx][dy]) {
                        if (f[dx][dy] <= o.first) {
                            sum++;
                            q.push(p(dx, dy));
                            ::i[dx][dy] = false;
                        }
                        else {
                            U.push(P(f[dx][dy], p(dx, dy)));
                        }
                    }
                }
            }
            r.push_back(p(sum, MAX));
        }
        int MIN = LLONG_MAX/3;
        for (auto i = l.begin(); i != l.end(); i++) {
            auto j = lower_bound(r.begin(),r.end(),p(a-(*i).first,0));
            if (j != r.end()) {
                MIN = min(MIN, (*i).second + (*j).second);
            }
        }
        cout << MIN << endl;
    }
}

Measured memory_bytes: 29284