n, m = gets.split.map(&:to_i)
union_find = Array(0..n)
rank = Array.new(n + 1, 0)

def union_find.root(idx)
  self[idx] == idx ? idx : (self[idx] = root(self[idx]))
end

m.times do
  a, b = gets.split.map { |e| union_find.root(e.to_i) }
  next if a == b

  if rank[a] > rank[b]
    union_find[b] = a
  else
    union_find[a] = b
    rank[b] += 1 if rank[a] == rank[b]
  end
end

h = {}
union_find.each { |e| h[union_find.root(e)] = 1 }

puts h.size - 2
