def root(a)
  if @arr[a] == a
    return a
  else
    return root(@arr[a])
  end
end

n,m = gets.split.map(&:to_i)
@arr = (0...n).to_a

m.times do |i|
  a,b = gets.split.map(&:to_i)
  a -= 1
  b -= 1
  if root(a) < root(b)
    @arr[b] = root(a)
  else
    @arr[a] = root(b)
  end
end

for i in 0...n
  @arr[i] = root(i)
end


puts @arr.uniq.size - 1