1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 1000
14 #define maxm 100000+5
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
23 #define mod 1000000007
24 using namespace std;
25 inline int read()
26 {
27 int x=0,f=1;char ch=getchar();
28 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
29 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
30 return x*f;
31 }
32 int n,m,k,mincost,tot=1,s,t,ss,tt,head[maxn],d[maxn],from[2*maxm];
33 bool v[maxn];
34 queue<int>q;
35 struct edge{int from,go,next,v,c;}e[2*maxm];
36 void add(int x,int y,int v,int c)
37 {
38 e[++tot]=(edge){x,y,head[x],v,c};head[x]=tot;
39 e[++tot]=(edge){y,x,head[y],0,-c};head[y]=tot;
40 }
41 inline void insert(int x,int y,int l,int r,int c)
42 {
43 if(l){add(ss,y,l,c);add(x,tt,l,0);}
44 add(x,y,r-l,c);
45 }
46 bool spfa()
47 {
48 for (int i=0;i<=t;i++){v[i]=0;d[i]=inf;}
49 q.push(s);d[s]=0;v[s]=1;
50 while(!q.empty())
51 {
52 int x=q.front();q.pop();v[x]=0;
53 for (int i=head[x],y;i;i=e[i].next)
54 if(e[i].v&&d[x]+e[i].c<d[y=e[i].go])
55 {
56 d[y]=d[x]+e[i].c;from[y]=i;
57 if(!v[y]){v[y]=1;q.push(y);}
58 }
59 }
60 return d[t]!=inf;
61 }
62 void mcf()
63 {
64 mincost=0;
65 while(spfa())
66 {
67 int tmp=inf;
68 for(int i=from[t];i;i=from[e[i].from]) tmp=min(tmp,e[i].v);
69 mincost+=d[t]*tmp;
70 for(int i=from[t];i;i=from[e[i].from]){e[i].v-=tmp;e[i^1].v+=tmp;}
71 }
72 }
73 int main()
74 {
75 freopen("input.txt","r",stdin);
76 freopen("output.txt","w",stdout);
77 n=read();
78 s=0;t=n+1;ss=t+1;tt=t+2;
79 insert(s,1,0,inf,0);
80 for1(i,n)
81 {
82 int tmp=read();
83 for1(j,tmp)
84 {
85 int x=read(),y=read();
86 insert(i,x,1,inf,y);
87 }
88 insert(i,t,0,inf,0);
89 }
90 insert(t,s,0,inf,0);
91 s=ss;t=tt;
92 mcf();
93 printf("%d\n",mincost);
94 return 0;
95 }