#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; #define all(v) v.begin(), v.end() #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, n, m) for (ll i = n; i <= m; ++i) #define rep3(i, n, m) for (ll i = n; i >= m; --i) template using pqg = priority_queue, greater>; template using pq = priority_queue; template inline bool chmax(S &a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(S &a, T b) { if (a > b) { a = b; return true; } return false; } int main() { int n, m; cin >> n >> m; vector out(n, 0); vector> G(n); rep(i, m) { int g, r; cin >> g >> r; out[g-1] += r; rep(j, r) { int h; cin >> h; // G[g-1].push_back(h-1); G[h-1].push_back(g-1); } } queue que; rep(i, n) if (out[i] == 0) que.push(i); while (!que.empty()) { int m = que.size(); while (m--) { int x = que.front(); que.pop(); for (int to : G[x]) { --out[to]; // cout << out[to] << endl; if (out[to] == 0) que.push(to); } } } int ans = 0; rep(i, n) if (out[i]) ++ans; cout << n - ans << endl; return 0; }