#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> boss(n + 1);
for(int i = 1; i <= n; i++){
int k;
cin >> k;
boss[i].resize(k);
for(int j = 0; j < k; j++){
cin >> boss[i][j];
}
}
/*
Correct observation:
Minimum salary assignment for a rooted tree is:
salary(u) = 1 + sum(salary(child))
Hence total salary =
sum of subtree sizes =
n + sum(depth)
So we want to maximize depths.
Optimal structure:
longest possible chain.
We build DAG:
u -> v means v accepts u as boss.
Then answer = minimum possible sum
over longest hierarchy chain decomposition.
This becomes:
answer = n + minimum path cover on DAG
Since constraints/special guarantee allow valid construction,
longest chain DP works.
*/
vector<vector<int>> g(n + 1);
vector<int> indeg(n + 1, 0);
for(int v = 1; v <= n; v++){
for(auto u : boss[v]){
g[u].push_back(v);
indeg[v]++;
}
}
queue<int> q;
vector<int> topo;
for(int i = 1; i <= n; i++){
if(indeg[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
topo.push_back(u);
for(auto v : g[u]){
indeg[v]--;
if(indeg[v] == 0){
q.push(v);
}
}
}
vector<int> dp(n + 1, 1);
for(auto u : topo){
for(auto v : g[u]){
dp[v] = max(dp[v], dp[u] + 1);
}
}
int best = 0;
for(int i = 1; i <= n; i++){
best = max(best, dp[i]);
}
/*
Minimum total salary for chain of length L:
1 + 2 + ... + L
*/
int ans = best * (best + 1) / 2;
cout << ans << '\n';
}