#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int w[maxn], in[maxn], out[maxn], cost[maxn];
vector<int>g[maxn];
queue<int>q;
int main() {
int n, x, len, v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x >> len;
w[x] = len;
while (cin >> v) {
if (!v)
break;
g[v].push_back(x);
in[x]++;
out[v]++;
}
}
for (int i = 1; i <= n; i++)
if (!in[i]) {
q.push(i);
cost[i] = w[i];
}
while (!q.empty()) {
int p = q.front();
q.pop();
for (int i = 0; i < g[p].size(); i++) {
int t = g[p][i];
in[t]--;
cost[t] = max(cost[t], cost[i] + w[t]);
if (in[t] == 0)
q.push(t);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (out[i] == 0)
ans = max(ans, cost[i]);
}
cout << ans << endl;
return 0;
}