N, M = readline.split(' ').map(&:to_i)
src = Array.new(N+1) {|idx| idx}
M.times do
  x,y, = readline.split(/\s/).map(&:to_i)
  min_idx = [src[x], src[y]].min
  src[x]=min_idx
  src[y]=min_idx
end

changed = true
while changed
  changed = false
  src.each_with_index do |v, i|
    if v != src[v]
      src[i] = src[v]
      changed=true
    end
  end
end

puts(src.uniq.size - 1)
