#!/usr/bin/env ruby
# require 'pry'

# N
# a_1 b_1
# ...
# a_N b_N
# ↑を[[a_1,..a_N],[b_1,..b_N]]にする
# N = gets.to_i
# a, b = N.times.map{gets.split.map(&:to_i)}.transpose

n,m = gets.split.map &:to_i
lists = []
m.times { |e|
  a = gets.split.map &:to_i
  lists << a
}
lists.sort_by! { |list| list[0] }

groups = [lists[0]]
1.upto(m-1) do |i|
  groups.each do |group|
    if (lists[i] - group).length < lists[i].length
      lists[i].each { |e| group << e unless group.include?(e) }
    else
      groups << lists[i]
      break
    end
  end
end
lists.flatten!.uniq!
puts groups.length - 1 + (n - lists.length)