class UnionFind
	def initialize
		@obj_to_node = {}
	end

	def add(obj)
		@obj_to_node[obj] ||= Node.new(obj)
	end

	def size(x = nil)
		x ? @obj_to_node[x].root.size : @obj_to_node.size
	end

	class Node
		def initialize(obj)
			@obj = obj
			@parent = self
			@size = 1
		end

		def root
			return self if @parent == self
			@parent = @parent.root
		end	

		attr_accessor :parent, :size
	end

	def union(x, y)
		u = (@obj_to_node[x] ||= Node.new(x)).root
		v = (@obj_to_node[y] ||= Node.new(y)).root
		return if u == v
		u.parent = v.root
		v.size += u.size
	end

	def same?(x, y)
		return false unless u = @obj_to_node[x]
		return false unless v = @obj_to_node[y]
		u.root == v.root
	end
end

loop {
	r = gets.to_i
	break if r == 0

	room_auth = []
	2.times {
		w, h, x, y = gets.split.map &:to_i
		a = (1..h).map{gets.split.map &:to_i}
		edges = []
		(0..h-1).each{|i|
			(0..w-1).each{|j|
				if i < h-1
					p1, p2 = a[i][j], a[i+1][j]
					edges << [[i, j], [i+1, j], p1 > p2 ? p1 : p2]
				end
				if j < w-1
					p1, p2 = a[i][j], a[i][j+1]
					edges << [[i, j], [i, j+1], p1 > p2 ? p1 : p2]
				end
			}
		}
		edges.sort_by!(&:last)
		uf = UnionFind.new
		uf.add([y-1,x-1])
		hash = {0 => 0, 1 => 1}
	
		until edges.empty?
			auth = edges[0][-1]
			while !edges.empty? && edges[0][-1] == auth
				e = edges.shift
				uf.union(e[0], e[1])
			end
			hash[uf.size([y-1, x-1])] = auth
		end
		room_auth << hash.to_a
	}

	min = Float::INFINITY
	room_auth[0].each{|room1, auth1|
		room2, auth2 = room_auth[1].find{|room2, auth2|
			room1 + room2 >= r
		}
		if auth2
			auth = auth1 + auth2
			min = auth if auth < min
		end
	}
	p min
}