class Heap
	require 'forwardable'
	extend Forwardable

	def initialize
		@array = []
		@index = {}
	end

	def add(obj, key)
		@array << [obj, key]
		@index[obj] = @array.size - 1
		lift_up(@array.size - 1)
	end

	def swap(i, j)
		u = @array[i]
		v = @array[j]
		@index[u[0]], @index[v[0]] = j, i
		@array[i], @array[j] = v, u
	end

	def lift_up(i)
		return if i == 0
		j = (i - 1) / 2
		if (@array[i][1] <=> @array[j][1]) < 0
			swap(i, j)
			lift_up(j)
		end
	end

	def del_min
		raise "heap is empty!" if empty?
		min = @array[0]
		if size > 1
			swap(0, @array.size-1)
			@array.pop
			lift_down(0)
		else
			@array.pop
		end
		@index.delete(min[0])
		min
	end

	def inc_key(obj, diff)
		i = @index[obj]
		@array[i][1] += diff
		lift_down(i)
	end

	def dec_key(obj, diff)
		i = @index[obj]
		@array[i][1] -= diff
		lift_up(i)
	end

	def update_key(obj, new_key)
		i = @index[obj]
		return unless i
		return if (new_key <=> @array[i][1]) >= 0
		@array[i][1] = new_key
		lift_up(i)
	end

	def min_obj
		@array[0][0]
	end

	def min_key
		@array[0][1]
	end

	def lift_down(i)
		j = 2 * i + 1
		k = j + 1
		if k >= size
			return if j >= size
			if (@array[i][1] <=> @array[j][1]) > 0
				swap(i, j)
				lift_down(j)
			end
		else
			l = ((@array[j][1] <=> @array[k][1]) < 0 ? j : k)
			if (@array[i][1] <=> @array[l][1]) > 0
				swap(i, l)
				lift_down(l)
			end
		end
	end

	def_delegators :@array, :empty?, :size
end

module Graphical
	require 'set'

	def dijkstra(start)
		raise "start must be Enumerable" if !start.kind_of?(Enumerable)
		h = Heap.new
		reached = Set[*start.to_a]
		start.each{|s| h.add(s, 0)}

		dist = {}
		until h.empty?
			u, key = h.del_min
			return key if block_given? && yield(u, key)
			dist[u] = key
			each_out_connection_of(u){|v, l|
				d = dist[u] + l
				if reached.include?(v)
					h.update_key(v, d)
				else
					h.add(v, d)
					reached << v
				end
			}
		end
		block_given? ? nil : dist
	end
end

class Graph
	include Graphical

	def initialize(*options)
		@options = Set[*options]
	end

	def add_node(property = {})
		Node.new(property)
	end

	def add_edge(u, v, property = {})
		e = Edge.new(u, v, property)
		u.add_edge(e)
		v.add_edge(e)
		e
	end
	
	def del_edge(e)
		u, v = e.opposite.keys
		u.del_edge(e)
		v.del_edge(e)
		e
	end

	class Node
		def initialize(property)
			@property = property
			@edges = Set.new
		end

		def add_edge(e)
			@edges << e
		end

		def del_edge(e)
			@edges.delete(e)
		end

		def to_s
			@property[:name] || super
		end

		def each_edge
			@edges.each{|e| 
				yield e
			}
		end

		attr_reader :property
	end

	class Edge
		def initialize(u, v, property)
			@opposite = {u => v, v => u}
			@property = property
		end
		attr_reader :opposite, :property

		def to_s
			@property[:name] || super
		end

		def length
			@property[:length] || 1
		end

		def actual_capacity(from)
			v = @opposite[from]
			cap = @property[:capacity]
			flow = (@property[:flow] ||= 0)
			to = (@property[:flow_to] ||= v)
			to == v ? cap - flow : cap + flow
		end
	end

	def each_out_connection_of(u)
		u.each_edge{|e|
			yield(e.opposite[u], e.length)
		}
	end

	alias :each_in_connection_of :each_out_connection_of

	def get_augmenting_path(s, t)
		reached = Set[s]
		queue = [s]
		comming_edge = {}
		until queue.empty?
			u = queue.shift
			u.each_edge do |e|	
				v = e.opposite[u]
				next if reached.include?(v)

				next if e.actual_capacity(u) <= 0

				reached << v
				comming_edge[v] = e
				queue << v

				if v == t
					queue = []
					break
				end
			end	
		end

		if comming_edge[t]
			path = [t]
			v = t
			until path[0] == s
				e = comming_edge[v]
				u = e.opposite[v]
				path = [u, e, *path]
				v = u
			end
			return path
		end
		return nil
	end
end

class Array
	def -(other)
		[self[0] - other[0], self[1] - other[1]]
	end
	def +(other)
		[self[0] + other[0], self[1] + other[1]]
	end
end

w, h, n = gets.split.map(&:to_i)

g = Graph.new
nodes = (1..h).map { (1..w).map { g.add_node } }
edges = {}
(0..h-1).each do |i|
	(0..w-1).each do |j|
		if j < w-1
			e = g.add_edge(nodes[i][j], nodes[i][j+1])
			edges[[i, j, i, j+1]] = e
		end
		if i < h-1
			e = g.add_edge(nodes[i][j], nodes[i+1][j])
			edges[[i, j, i+1, j]] = e
		end
	end
end

n.times do
	x1, y1, x2, y2 = gets.split.map(&:to_i)
	x1, y1, x2, y2 = x2, y2, x1, y1 if x1 > x2 || y1 > y2
	d = ([x2, y2] - [x1, y1]).map {|z| z == 0 ? 0 : z / z.abs}
	x, y = x1, y1
	until [x, y] == [x2, y2] do
		key = nil
		if d[1] > 0
			key = [y, x-1, y, x]
		else
			key = [y-1, x, y, x]
		end
		e = edges[key]
		g.del_edge(e) if e
		edges.delete(key)	

		x, y = [x, y] + d
	end
end

xs, ys = gets.split.map(&:to_i)
xt, yt = gets.split.map(&:to_i)
s = nodes[ys][xs]
t = nodes[yt][xt]

orig = g.dijkstra([s]){|v| v == t}

dists = edges.map do |key, e|
	g.del_edge(e)
	d = g.dijkstra([s]){|v| v == t}
	y1, x1, y2, x2 = key
	f = g.add_edge(nodes[y1][x1], nodes[y2][x2])
	d || orig
end

p dists.max - orig

Measured memory_bytes: 13312