#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
vector<int> g[5010];
int vis[5010];
int val[5010];
int n;
int bfs(int s) {
for (int i = 1; i <= n; i++) {
val[i] = 0;
vis[i] = 0;
}
val[s] = 1;
vis[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
s = q.front();
q.pop();
for (auto i: g[s]) {
if (vis[i]) continue ;
val[i] = val[s] + 1;
vis[i] = 1;
q.push(i);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) return -1;
ans += val[i];
}
return ans;
}
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
for (int j = 1; j <= k; j++) {
int x; cin >> x;
g[x].pb(i);
}
}
int d = 1e9;
for (int i = 1; i <= n; i++) {
int a = bfs(i);
if (a == -1) continue ;
d = min(d, a);
}
cout << d << '\n';
}
signed main() {IOS solve(); return 0;}