use std::collections::VecDeque; fn topologycal_sort(paths: &Vec>) -> Vec { let n = paths.len(); let mut ret = vec![]; let mut ins = vec![0usize; n]; for i in 0..n { for &v in paths[i].iter() { ins[v] += 1; } } let mut que = VecDeque::new(); for i in 0..n { if ins[i] == 0 { que.push_back(i); } } while let Some(x) = que.pop_back() { ret.push(x); for &v in paths[x].iter() { ins[v] -= 1; if ins[v] == 0 { que.push_back(v); } } } ret } fn main() { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let mut paths = vec![vec![]; n]; let mut targets = vec![]; let mut base = vec![None; n]; for i in 0..m { let mut gr = String::new(); std::io::stdin().read_line(&mut gr).ok(); let gr: Vec = gr.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let g = gr[0]-1; let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse::().unwrap()-1).collect(); for &v in temp.iter() { paths[v].push(g); } targets.push(temp); base[g] = Some(i); } let order = topologycal_sort(&paths); let mut used = vec![false; n]; for i in 0..n-m { used[order[i]] = true; } for i in 0..m { if order.len() > i+n-m { let idx = order[i+n-m]; if base[idx].is_none() || !targets[base[idx].unwrap()].iter().any(|&v| !used[v]) { used[idx] = true; } } } println!("{}", used.iter().filter(|&&v| v).count()); }