n = gets().to_i
xiyi = []
space = {}
n.times{|i|
	xiyi[i] = gets().split(' ').map(&:to_i)
	space["#{xiyi[i][0]}_#{xiyi[i][1]}"] = 1
}

count = 0

begin
	flag = false

	(0 ... (xiyi.length - 1)).each {|i|
		((i + 1) ... xiyi.length).each{|j|
			xi = xiyi[i][0]
			yi = xiyi[i][1]
			xj = xiyi[j][0]
			yj = xiyi[j][1]

			if xi != xj && yi != yj
				c = 0
				c += 1 if space.include?("#{xi}_#{yj}")
				c += 1 if space.include?("#{xj}_#{yi}")

				if c == 1
					
					if space.include?("#{xi}_#{yj}"); xn = xj; yn = yi; end
					if space.include?("#{xj}_#{yi}"); xn = xi; yn = yj; end
					xiyi.push([xn, yn])
					space["#{xn}_#{yn}"] = 1
					count += 1
					flag = true
				end
			end
		}
	}
end while flag

puts count
