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

	def root(obj)
		(@obj_to_node[obj] ||= Node.new(obj)).root.obj
	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
		attr_reader :obj
	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
		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.rank += 1
			v.size += u.size
		end
	end
end

n, w, h = gets.split.map(&:to_i)
row_rep = {}
col_rep = {}
u = UnionFind.new

on_wall = false
n.times do |i|
	x, y = gets.split.map(&:to_i)
	if x == 1 || x == w || y == 1 || y == h
		on_wall = true
	end
	if j = col_rep[x]		
		u.union(i, j)
	else
		col_rep[x] = i
	end	
	if j = row_rep[y]		
		u.union(i, j)
	else
		row_rep[y] = i
	end	
end

roots = (0..n-1).map {|i| u.root(i)}.uniq
k = roots.map{|r| u.size(r) - 1}.inject(:+)
if roots.size > 1
	k += roots.size * 2 - 1
	k -= 1 if on_wall
end
puts k