#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <climits>
#include <cstring>
using namespace std;

class P{
public:
  int x, y, level;
  P(){}
  P(int x, int y, int level) : x(x), y(y), level(level) {}

  bool operator<(const P &p)const{
    return level > p.level;
  }
};

int R;
int H[2], W[2];
int SX[2], SY[2];
int T[2][502][502];
vector<int> LEVEL[2], ROOM[2];

bool closed[502][502];

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

void solve_small(int h, int w, int sx, int sy, int t[502][502], vector<int> &level, vector<int> &room){
  priority_queue<P> open;
  memset(closed, 0, sizeof(closed));
  open.push(P(sx, sy, 1));

  int nowLevel = 1;
  int nowRoom = 0;

  while(!open.empty()){
    P p = open.top();
    open.pop();

    if(closed[p.y][p.x]){
      continue;
    }
    closed[p.y][p.x] = true;

    if(p.level > nowLevel){
      level.push_back(nowLevel);
      room.push_back(nowRoom);
      nowLevel = p.level;
    }

    nowRoom++;

    for(int i = 0; i < 4; i++){
      int nx = p.x + dx[i];
      int ny = p.y + dy[i];
      if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
      if(closed[ny][nx]) continue;
      open.push(P(nx, ny, t[ny][nx]));
    }
  }

  level.push_back(nowLevel);
  room.push_back(nowRoom);
}

void solve(){
  for(int i = 0; i < 2; i++){
    solve_small(H[i], W[i], SX[i], SY[i], T[i], LEVEL[i], ROOM[i]);
  }

  int ans = INT_MAX;

  for(int i = 0; i < ROOM[0].size(); i++){
    if(ROOM[0][i] >= R){
      ans = min(ans, LEVEL[0][i]);
      continue;
    }

    int rem = R - ROOM[0][i];
    int pos = lower_bound(ROOM[1].begin(), ROOM[1].end(), rem) - ROOM[1].begin();
    if(pos == ROOM[1].size()) continue;

    ans = min(ans, LEVEL[0][i] + LEVEL[1][pos]);
  }

  cout << ans << endl;
}

int main(){
  while(cin >> R, R){
    for(int i = 0; i < 2; i++){
      LEVEL[i].clear();
      ROOM[i].clear();

      cin >> W[i] >> H[i] >> SX[i] >> SY[i];
      SX[i]--;
      SY[i]--;

      for(int j = 0; j < H[i]; j++){
        for(int k = 0; k < W[i]; k++){
          cin >> T[i][j][k];
        }
      }
    }

    solve();
  }
}