N, M = gets.split.map(&:to_i)
G = Array.new(N + 1){ [] }
E = M.times.map{ gets.split.map(&:to_i) }
E.each do |u,v|
  G[u] << v
  G[v] << u
end
used = Array.new(N + 1)
n = (1 .. N).count do |s|
  next false if used[s]
  
  q = [s]
  used[s] = true
  until q.empty?
    u = q.shift
    G[u].each do |v|
      next if used[v]
      used[v] = true
      q << v
    end
  end
  true
end
puts n - 1
