    N, M = readline.split(/\s/).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], x].min
      src[x]=min_idx
      src[y]=min_idx
    end
     
    puts(src.sort.uniq.size - 1)