n,m = gets.split.map(&:to_i)
a,b = m.times.map{gets.split.map(&:to_i)}.transpose

tags = Array.new(n+1,0)
for i in 1..n
  tags[i] = i
end

nds = Array.new(n+1).map{Array.new(0)}
for i in 0..(m-1)
  nds[a[i]].push(b[i])
  nds[b[i]].push(a[i])
end
for i in 1..n
  nds[n].push(0)
end

scd = Array.new(n+1, 0)
id = 1
while true
  scd[id] = 1
  ids = Array.new(0)
  idsln = 0
  nds[id].each do |e|
    if scd[e] == 0 && e != 0
      scd[e] = 1
      tags[e] = id
      ids.push(e)
      idsln += 1
    end
  end
  for i in 0..(idsln-1)
    nds[ids[i]].each do |e|
      if scd[e] == 0 && e != 0
        scd[e] = 1
        tags[e] = id
        ids.push(e)
        idsln += 1
      end
    end
  end
  
  while id <= n
    id += 1
    break if scd[id] == 0
  end
  
  break if id > n
end

puts tags.uniq!.length - 2