#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define endl '\n'
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> PII;
int d[5001];
void solve() {
int n;
cin >> n;
vector<vector<int>> g(n + 1);
queue<int> q, ans;
int cnt=1;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
for (int j = 1; j <= t; j++) {
int x;
cin >> x;
cnt++;
g[i].push_back(x);
d[x]++;
}
}
for (int i = 1; i <= cnt; i++) {
if (d[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
int h = q.front();
q.pop();
ans.push(h);
for (auto e : g[h]) {
d[e]--;
if (d[e] == 0) {
q.push(e);
}
}
}
cout << ans.size() << endl;
}
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int t;
t=1;
while (t--)
{
solve();
}
return 0;
}