# 整数の入力                                                                                                                           
n, m = gets.chomp.split(" ").map(&:to_i)                                                                                               
                                                                                                                                       
                                                                                                                                       
arr = Array.new(n, -1)                                                                                                                 
children_manager = Array.new(n, [])                                                                                                    
                                                                                                                                       
m.times do                                                                                                                             
  a, b = gets.chomp.split(" ").map{|x| x.to_i - 1}                                                                                     
  a_parent = arr[a]                                                                                                                    
  b_parent = arr[b]                                                                                                                    
  if a_parent == -1 && b_parent == -1                                                                                                  
    arr[b] = a                                                                                                                         
    children_manager[a] << b                                                                                                           
  elsif a_parent > -1 && b_parent == -1                                                                                                
    arr[b] = a_parent                                                                                                                  
    children_manager[a_parent] << b                                                                                                    
  elsif a_parent == -1 && b_parent > -1                                                                                                
    arr[a] = b_parent                                                                                                                  
    children_manager[b_parent] << a                                                                                                    
  elsif a_parent > -1 && b_parent > -1                                                                                                 
    arr[b] = a_parent                                                                                                                  
    children_manager[b].each do |c|                                                                                                    
      arr[c] = a_parent                                                                                                                
    end                                                                                                                                
  else                                                                                                                                 
  end                                                                                                                                  
end                                                                                                                                    
puts arr.select{|x| x == -1}.size - 1     