n, m = gets.split(' ').map{|s| s.to_i}
r = []
n.times{|i|
  r << [i + 1, i + 1]
}

m.times{
  r << gets.split(' ').map{|s| s.to_i}
}
#p r

f = 1
while(f == 1) do
  s1 = r.size
  r.each{|set|
    set[0] = r[-1][1] if set[0] == r[-1][0]
    set[1] = r[-1][1] if set[1] == r[-1][0]
  }
  r.uniq!
  s2 = r.size
  if s1 == s2
    f = 0
  end
#  p r
end
#p r

puts (r.uniq.size - 1)