import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.math, std.typecons; void main() { int n, m; scan(n, m); int[int] map; auto adj = new int[][](m, 0); foreach (i ; 0 .. m) { int gi, ri; scan(gi, ri); map[gi] = i; adj[i] = readln.split.to!(int[]); } auto mat = new int[][](m, m); foreach (i ; 0 .. m) { foreach (j ; adj[i]) { if (j !in map) continue; mat[map[j]][i] = 1; } } debug { writefln("%(%(%s %)\n%)", mat); } auto ans = n - m; auto vis = new bool[](m); while (true) { if (ans == n) break; int k = -1; foreach (j ; 0 .. m) { auto b = iota(m).all!(i => !mat[i][j]); if (b && !vis[j]) { k = j; break; } } if (k == -1) { break; } debug { writeln("k:", k); } vis[k] = true; ans++; foreach (j ; 0 .. m) { mat[k][j] = 0; } debug { writefln("%(%(%s %)\n%)", mat); writeln; } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }