#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define REP(i,n) for(int i=0;i<(int)(n);i++)
typedef pair<int, int> P;
typedef pair<int, P> Elm;

const int WMAX = 500, HMAX = 500;
const int LMAX = 100000000;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int office[WMAX][HMAX], level[WMAX][HMAX];

void dijkstra(int w, int h, int sx, int sy) {
  REP(y, h) {
    REP(x, w) {
      level[x][y] = LMAX;
    }
  }
  priority_queue<Elm, vector<Elm>, greater<Elm> > que;
  que.push( Elm(1, P(sx, sy)) );
  level[sx][sy] = 1;

  while (!que.empty()) {
    int lev = que.top().first; P p = que.top().second;
    que.pop();

    if (level[p.X][p.Y] < lev) continue;

    REP(i, 4) {
      int x = p.X + dx[i], y = p.Y + dy[i];
      if (x < 0 or w <= x or y < 0 or h <= y) continue;
      
      int newlevel = max(office[x][y], lev);
      if (newlevel < level[x][y]) {
        level[x][y] = newlevel;
        que.push(Elm(newlevel, P(x, y)));
      }
    }
  }
}

vector<int> count_level(int w, int h) {
  vector<int> v;
  v.push_back(0);
  REP(y, h) {
    REP(x, w) {
      v.push_back(level[x][y]);
    }
  }
  sort(v.begin(), v.end());
  return v;
}

vector<int> make_level_table() {
  int w, h, sx, sy;
  cin >> w >> h >> sx >> sy;
  REP(y, h) {
    REP(x, w) {
      cin >> office[x][y];
    }
  }
  dijkstra(w, h, sx - 1, sy - 1);
  return count_level(w, h);
}

int search(int key, 
    vector<int>::iterator begin, vector<int>::iterator end) {
  if (key == 0) return 0;
  pair<vector<int>::iterator, vector<int>::iterator> eqr = 
    equal_range(begin, end, key);
  return eqr.second - eqr.first;
}

int main() {
  int r, w, h, sx, sy;
  while (cin >> r and r) {
    vector<int> ltab1 = make_level_table();
    vector<int> ltab2 = make_level_table();

    vector<int>::iterator lev1 = ltab1.end() - 1, 
      lev2 = ltab2.begin();
    int rooms = ltab1.size() - 1;
    int ans = 2 * LMAX + 1;

    while (lev1 != ltab1.begin()) {
      while (rooms < r and lev2 != ltab2.end()) {
        lev2 = upper_bound(lev2, ltab2.end(), *lev2);
        rooms += search(*lev2, lev2, ltab2.end());
      }
      
      if (rooms < r or lev2 == ltab2.end()) break;
      ans = min(ans, *lev1 + *lev2);
      
      rooms -= search(*lev1, ltab1.begin(), lev1 + 1);
      lev1 = lower_bound(ltab1.begin(), lev1, *lev1) - 1;
    }
    cout << ans << endl;
  }
}