n, m = gets.split(" ").map(&:to_i)
c = Array.new(n, -1)
def root(c, x, y)
  if c[x] < 0
    c[x] = y
  else
    return root(c, c[x], y)
  end
end
m.times{ |i|
  a, b = gets.split(" ").map(&:to_i)
  root(c, a-1, b-1)
}
puts c.count(-1) -1 