def change(num)
  if !@group[num]
    return
  end
  @group[num] = false
  for i in @arr[num]
    change(i)
  end
end

n,m = gets.split.map(&:to_i)
@arr = Array.new(n){Array.new(0)}
@group = Array.new(n,true)

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

cnt = 0

for i in 0...n
  if @group[i]
    cnt += 1
  end
  change(i)
end

puts cnt-1