class Array
	def bsearch(l = 0, r = size - 1, &pred)
		if r - l < 100
			return (l..r).find{|j| pred[self[j]]}
		end

		c = (l + r) / 2 
		if pred[self[c]]
			bsearch(l, c-1, &pred) || c
		else
			bsearch(c+1, r, &pred)
		end
	end
end

class Line
	def initialize(*args)
		@x1, @y1, @x2, @y2 = args
	end
	attr_reader :x1, :y1, :x2, :y2
end

class IntervalTree
	def initialize(l, r)
		@l = l
		@r = r
		@c = (l + r) / 2
		@left_child = @right_child = nil
		@start_sorted = []
		@end_sorted = []
	end

	def add(l, r, x)
		if r < @c
			@left_child ||= self.class.new(l, @c - 1)
			@left_child.add(l, r, x)
		elsif @c < l
			@right_child ||= self.class.new(@c + 1, r)
			@right_child.add(l, r, x)
		else
			i = @start_sorted.bsearch{|p, q, _| p >= l} || @start_sorted.size
			@start_sorted.insert(i, [l, r, x])
			i = @end_sorted.bsearch{|p, q, _| q >= r} || @end_sorted.size
			@end_sorted.insert(i, [l, r, x])
		end
	end

	def query(x)
		if x < @c
			ans = @start_sorted.take_while{|l, r| l <= x} 
			ans += @left_child.query(x) if @left_child
			ans
		elsif x > @c
			ans = []
			ans += @right_child.query(x) if @right_child
			i = @end_sorted.bsearch{|l, r| x <= r}
			ans += @end_sorted[i..-1] if i
			ans
		else
			@start_sorted
		end
	end
end

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
			@rank = 0
		end

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

		attr_accessor :parent, :size, :rank
	end

	def union(x, y)
		u = v = nil
		if x.class == Node
			u = x.root
		else
			u = (@obj_to_node[x] ||= Node.new(x)).root
		end

		if y.class == Node
			v = y.root
		else
			v = (@obj_to_node[y] ||= Node.new(y)).root
		end

		return if u == v
		
		if u.rank > v.rank
			v.parent = u
			u.size += v.size
		elsif v.rank > u.rank
			u.parent = v
			v.size += u.size
		else
			u.parent = v
			v.size += u.size
			v.rank += 1
		end
	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

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


uf = UnionFind.new

w, h, n = gets.split.map(&:to_i)
h_tree = IntervalTree.new(0, h)
h_line = {}

l1 = Line.new(0,0,w,0)
l2 = Line.new(0,h,w,h)

h_line[[0,w,0]] = l1
h_line[[0,w,h]] = l2

uf.union(l1, l2)

h_tree.add(0, w, 0)
h_tree.add(0, w, h)

v_lines = []

count = 1
n.times {|hoge|
	#p hoge
	x1, y1, x2, y2 = gets.split.map(&:to_i)

	if x1 == x2
		l = Line.new(x1, y1, x2, y2)
		v_lines << l
	end

	if y1 == y2
		l = Line.new(x1, y1, x2, y2)
		h_line[[x1, x2, y1]] = l
		uf.add(l)
		uf.union(l, l1) if x1 == 0 || x2 == w 
count += 1 if x1 == 0 && x2 == w
		h_tree.add(x1, x2, y1)
	end
}

v_lines.each_with_index{|l, i|
	#p i
	#break
	a = h_tree.query(l.x1).map{|arr| h_line[arr]}
	a = a.select{|line| l.y1 <= line.y1 && line.y2 <= l.y2}

	group_count = Hash.new(-1)
	a.each{|hl| group_count[uf.root_of(hl)] += 1}
	count += group_count.values.inject(0, :+)
	#p a.size
	#a.each_cons(2){|foo, bar| uf.union(foo, bar)}
	group_count.keys.each_cons(2){|foo, bar| uf.union(foo, bar)}
}

p count