// vvvvvvvvvvvv TEMPLATE vvvvvvvvvvvv
#include <bits/stdc++.h>
using namespace std; using ll = long long; using P = pair<ll, ll>;
const ll linf = 1e18; const double eps = 1e-12, pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define each(i,a) for (auto&& i : a)
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
template<typename T> ll min(ll a, const T&& b) { return a < b ? a : b; }
template<typename T> ll min(const T&& a, ll b) { return a < b ? a : b; }
template<typename T> ll max(ll a, const T&& b) { return a > b ? a : b; }
template<typename T> ll max(const T&& a, ll b) { return a > b ? a : b; }
template<typename Head> void out(Head h) { cout << h << endl; } template<typename Head, typename... Tail>void out(Head h, Tail... t) { cout << h << " "; out(t...); }
template<typename T> istream& operator>>(istream& is, vector<T>& v) { each(x,v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
ostream& operator<<(ostream& os, const vector<string>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; }
struct yes_no : std::numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } };
void solve(); int main() {
  ios::sync_with_stdio(false); cin.tie(0); locale loc(locale(), new yes_no); cout.imbue(loc); cout << fixed << setprecision(10) << boolalpha;
  solve();
}
// ^^^^^^^^^^^^ TEMPLATE ^^^^^^^^^^^^

class UnionFind {
  vector<ll> par, h, sz;
public:
  UnionFind(ll size) : par(size, 0), h(size, 0), sz(size, 1) {
    rep(i, size) par[i] = i;
  }
  void unite(ll u, ll v) {
    u = root(u), v = root(v);
    if (u == v) return;
    if (h[u] < h[v]) {
      par[u] = v;
      sz[v] += sz[u];
    }
    else {
      par[v] = u;
      sz[u] += sz[v];
    }
    if (h[u] == h[v]) ++h[u];
  }
  ll size(ll v) {
    return sz[root(v)];
  }
  bool isUnited(ll u, ll v) {
    return root(u) == root(v);
  }
  ll root(ll v) {
    if (par[v] == v) return v;
    return par[v] = root(par[v]);
  }
};

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

#define NODE(x, y) ((y) * w + (x))

vector<ll> f(ll R) {
  ll w, h, sx, sy; cin >> w >> h >> sx >> sy; --sx, --sy;
  vector<vector<ll>> a(h, vector<ll>(w));
  map<ll, vector<P>> m;
  rep(y, h) rep(x, w) {
    cin >> a[y][x];
    m[a[y][x]].eb(x, y);
  }
  UnionFind uf(h*w);
  vector<ll> res(R+1, linf);
  res[0] = 0;
  each(pp, m) {
    each(p, pp.second) {
      ll x, y; tie(x, y) = p;
      rep(d, 4) {
        ll nx = x + dx[d];
        ll ny = y + dy[d];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && a[ny][nx] <= a[y][x]) {
          uf.unite(NODE(x, y), NODE(nx, ny));
        }
      }
    }
    ll sz = uf.size(NODE(sx, sy));
    chmin(res[min(R, sz)], pp.first);
  }
  rrep(i, R) chmin(res[i], res[i+1]);
  return res;
}

ll solve(ll R) {
  vector<ll> dp1 = f(R);
  vector<ll> dp2 = f(R);
  ll ans = linf;
  rep(i, R+1) {
    chmin(ans, dp1[i] + dp2[R-i]);
  }
  return ans;
}

void solve() {
  ll R;
  while (cin >> R, R) {
    cout << solve(R) << endl;
  }
}



Measured memory_bytes: 39724