#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

int R;
int W[2], H[2];
int X[2], Y[2];
int A[2][502][502];

bool used[502][502];

vpii data[2];

void bfs(int num)
{
    typedef pair<int, pii> Data;
    memset(used, false, sizeof(used));

    priority_queue<Data> q;

    q.push(Data(-1, pii(Y[num], X[num])));
    used[Y[num]][X[num]] = true;

    if (num == 0) {
        data[num].PB(pii(1, 0));
    } else {
        data[num].PB(pii(0, 0));
        data[num].PB(pii(0, 1));
    }

    int ma = 1;
    int cnt = 0;
    int hoge = 0;

    while (q.size()) {
        Data p = q.top(); q.pop();

        int cur = -p.fst;

        if (ma < cur) {
            if (num == 0) {
                data[num].back().scd = cnt;
            } else {
                data[num].back().fst = cnt;
            }

            ma = cur;

            if (num == 0) {
                data[num].PB(pii(ma, 0));
            } else {
                data[num].PB(pii(0, ma));
            }
        }

        cnt++;

        rep(i, 4) {
            int ny = p.scd.fst + dy[i], nx = p.scd.scd + dx[i];

            if (ny < 0 || ny >= H[num] || nx < 0 || nx >= W[num] || used[ny][nx]) continue;
            used[ny][nx] = true;

            q.push(Data(-A[num][ny][nx], pii(ny, nx)));
        }
    }

    if (num == 0) {
        data[num].back().scd = cnt;
    } else {
        data[num].back().fst = cnt;
    }
}

signed main()
{ // #define int long long ?????????????????§scanf???????????¨???????????????????????\?????????????????????
    while (cin >> R, R) {
        data[0].clear();
        data[1].clear();

        rep(i, 2) {
            cin >> W[i] >> H[i] >> X[i] >> Y[i];

            --X[i], --Y[i];

            rep(j, H[i]) rep(k, W[i]) {
                cin >> A[i][j][k];
            }
        }

        bfs(0);
        bfs(1);

        int mi = inf;

        data[1].PB(pii(inf, inf));

        rep(i, data[0].size()) {
            pii a = data[0][i];

            chmin(mi, a.fst + lower_bound(all(data[1]), pii(R - a.scd, -inf))->scd);
        }

        cout << mi << endl;
    }
}

Measured memory_bytes: 12156