using static System.Math;
using System.Collections.Generic;
using System.Linq;
using System;

public class PriorityQueue<T> where T : IComparable
{
	private IComparer<T> _comparer = null;
	private int _type = 0;
	private T[] _heap;
	private int _sz = 0;
	private int _count = 0;
	public PriorityQueue(int maxSize, IComparer<T> comparer)
	{
		_heap = new T[maxSize];
		_comparer = comparer;
	}
	public PriorityQueue(int maxSize, int type = 0)
	{
		_heap = new T[maxSize];
		_type = type;
	}
	private int Compare(T x, T y)
	{
		if (_comparer != null) return _comparer.Compare(x, y);
		return _type == 0 ? x.CompareTo(y) : y.CompareTo(x);
	}
	public void Push(T x)
	{
		_count++;
		var i = _sz++;
		while (i > 0)
		{
			var p = (i - 1) / 2;
			if (Compare(_heap[p], x) <= 0) break;
			_heap[i] = _heap[p];
			i = p;
		}
		_heap[i] = x;
	}
	public T Pop()
	{
		_count--;
		T ret = _heap[0];
		T x = _heap[--_sz];
		int i = 0;
		while (i * 2 + 1 < _sz)
		{
			int a = i * 2 + 1;
			int b = i * 2 + 2;
			if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b;
			if (Compare(_heap[a], x) >= 0) break;
			_heap[i] = _heap[a];
			i = a;
		}
		_heap[i] = x;
		return ret;
	}
	public int Count() => _count;
	public T Peek() => _heap[0];
	public bool Contains(T x)
	{
		for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true;
		return false;
	}
	public void Clear()
	{
		while (this.Count() > 0) this.Pop();
	}
	public IEnumerator<T> GetEnumerator()
	{
		var ret = new List<T>();
		while (this.Count() > 0)
		{
			ret.Add(this.Pop());
		}
		foreach (var r in ret)
		{
			this.Push(r);
			yield return r;
		}
	}
	public T[] ToArray()
	{
		T[] array = new T[_sz];
		int i = 0;
		foreach (var r in this)
		{
			array[i++] = r;
		}
		return array;
	}
}

public class Room : IComparable
{
	public int x { get; set; }
	public int y { get; set; }
	public int d { get; set; }
	public int CompareTo(object obj)
	{
		var x = (Room)obj;
		if (this.d > x.d) return 1;
		else if (this.d == x.d) return 0;
		else return -1;
	}
}


public class P
{
	public int x { get; set; }
	public int y { get; set; }
}

public class hello
{
	public static int[] dx, dy;
	public static void Main()
	{
		dx = new int[] { 0, 1, 0, -1 };
		dy = new int[] { 1, 0, -1, 0 };
		while (true)
		{
			var r = int.Parse(Console.ReadLine().Trim());
			if (r == 0) break;
			string[] line = Console.ReadLine().Trim().Split(' ');
			var w1 = int.Parse(line[0]);
			var h1 = int.Parse(line[1]);
			var sy1 = int.Parse(line[2]) - 1;
			var sx1 = int.Parse(line[3]) - 1;
			var map1 = new int[h1, w1];
			for (int i = 0; i < h1; i++)
			{
				line = Console.ReadLine().Trim().Split(' ');
				for (int j = 0; j < w1; j++)
					map1[i, j] = int.Parse(line[j]);
			}
			line = Console.ReadLine().Trim().Split(' ');
			var w2 = int.Parse(line[0]);
			var h2 = int.Parse(line[1]);
			var sy2 = int.Parse(line[2]) - 1;
			var sx2 = int.Parse(line[3]) - 1;
			var map2 = new int[h2, w2];
			for (int i = 0; i < h2; i++)
			{
				line = Console.ReadLine().Trim().Split(' ');
				for (int j = 0; j < w2; j++)
					map2[i, j] = int.Parse(line[j]);
			}

			var ps1 = getList(map1, h1, w1, sx1, sy1);
			var ps2 = getList(map2, h2, w2, sx2, sy2);
			var ans = getAns(ps1, ps2, r);
			Console.WriteLine(ans);
		}
	}
	static int getAns(List<P> ps1, List<P> ps2, int r)
	{
		var ans = int.MaxValue;
		var pc = ps1.Count();
		for (int i = 0; i < pc; i++)
		{
			var w = BS(ps2, r, ps1[i].y);
			if (w != -1) ans = Min(ans, ps1[i].x + ps2[w].x);
		}
		return ans;
	}
	static int BS(List<P> ps, int r, int tv)
	{
		var pc = ps.Count();
		if (tv + ps[pc - 1].y < r) return -1;
		if (tv >= r) return 0;
		var ng = 0;
		var ok = pc - 1;
		while (ok - ng > 1)
		{
			var mid = ng + (ok - ng) / 2;
			if (tv + ps[mid].y >= r) ok = mid;
			else ng = mid;
		}
		return ok;
	}
	static List<P> getList(int[,] map, int h, int w, int sx, int sy)
	{
		var used = new bool[h, w];
		var dic = new Dictionary<int, int>();
		var pq = new PriorityQueue<Room>(h * w+ 10);
		pq.Push(new Room { x = sx, y = sy, d = 1 });
		used[sx, sy] = true;
		var Lnow = 1;
		while (pq.Count() > 0)
		{
			var t = pq.Pop();
			if (t.d > Lnow) Lnow = t.d;
			if (dic.ContainsKey(Lnow)) dic[Lnow]++;
			else dic[Lnow] = 1;
			for (int i = 0; i < 4; i++)
			{
				var nx = t.x + dx[i];
				var ny = t.y + dy[i];
				if (nx >= 0 && nx < h && ny >= 0 && ny < w && !used[nx, ny])
				{
					pq.Push(new Room { x = nx, y = ny, d = map[nx, ny] });
					used[nx, ny] = true;
				}
			}
		}
		var ans = new List<P>();
		var dicc = dic.Count();
		var dic2 = dic.OrderBy(x => x.Key).ToArray();
		ans.Add(new P { x = dic2[0].Key, y = dic2[0].Value });
		for (int i = 1; i < dicc; i++)
			ans.Add(new P { x = dic2[i].Key, y = dic2[i].Value + ans[i - 1].y });
		ans.Insert(0, new P { x = 0, y = 0 });
		return ans;
	}
}



Measured memory_bytes: 43168