You are a programmer who loves pretty girl games (a sub-genre of dating simulation games). A game, which is titled "Floatable Heart" and was released last Friday, has arrived at your home just now. This game has multiple stories. When you complete all of those stories, you can get a special figure of the main heroine, Megumi. So, you want to hurry to play the game! But, let's calm down a bit and think how to complete all of the stories in the shortest time first.

In fact, you have the special skill that allows you to know the structure of branching points of games. By using the skill, you have found out that there are n branching points in this game and i-th branching point has k_{i} choices. This game is so complicated that multiple routes get to i-th branching point probably. You also noticed that it takes c_{ij} minutes to proceed from i-th branching point to another branching point (or an ending), if you select j-th choice of i-th branching point. Of course you need to select all the choices on all the branching points and read stories between a branching point and another branching point (or an ending) to complete all of those stories. In addition, you can assume it only takes negligible time to return to the beginning of the game ("reset") and to play from the beginning to the first branching point.

The game's manual says that this game has an additional feature called "Quick Save", and the feature allows you to record the point where you are currently playing and return there at any time later. However, this feature is not working for some bug. Thus you have to restart from the first branching point every time, if you reach an ending or quit the game on the way. Any patch to fix this bug has not been yet published. This is an imposed tribulation for the fastest players.

Well, let's estimate how long it will take for completing all of the stories in the shortest time.

Input

A data set is given in the following format.


n
k_{1} t_{11} c_{12} ... t_{1k_{1}} c_{1k_{1}}
:
:
k_{n} t_{n1} c_{n2} ... t_{nk_{n}} c_{nk_{n}}


The first line of the data set contains one integer n (2 \leq n \leq 1{,}000), which denotes the number of the branching points in this game. The following n lines describe the branching points. The i-th line describes the branching point of ID number i. The first integer k_{i} (0 \leq k_{i} \leq 50) is the number of choices at the i-th branching point. k_{i} ≥ 0 means that the i-th branching point is an ending. Next 2k_{i} integers t_{ij} (1 \leq t_{ij} \leq n) and c_{ij} (0 \leq c_{ij} \leq 300) are the information of choices. t_{ij} denotes the ID numbers of the next branching points when you select the j-th choice. c_{ij} denotes the time to read the story between the i-th branching point and the t_{ij}-th branching point. The branching point with ID 1 is the first branching point. You may assume all the branching point and endings are reachable from the first branching point. You may also assume that there is no loop in the game, that is, no branching point can be reached more than once without a reset.

Output

Print the shortest time in a line.

Sample Input 1


2
1 2 2
0


Output for the Sample Input 1


2


Sample Input 2


6
2 2 1 3 2
2 4 3 5 4
2 5 5 6 6
0
0
0


Output for the Sample Input 2


24


Sample Input 3


6
3 2 100 3 10 3 10
1 4 100
1 4 10
3 5 1 5 1 6 1
0
0


Output for the Sample Input 3


243






Example

Input

2
1 2 2
0


Output

2
#include<bits/stdc++.h>
#define MAX 2001
#define inf 1<<29
#define linf (1e16)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0+sqrt(5.0))/2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define pd(a) printf("%.10f\n",(double)(a))
#define pld(a) printf("%.10Lf\n",(ld)(a))
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)
#define Unique(v) v.erase(unique(all(v)),v.end())
#define equals(a,b) (fabs((a)-(b))<eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,double> pid;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;

struct edge{ ll to,cap,cost,rev; };

int v;
vector<edge> e[MAX];
ll h[MAX];
ll dist[MAX];
ll prevv[MAX],preve[MAX];

void add_edge(int from,int to,ll cap,ll cost){
  e[from].push_back((edge){to,cap,cost,(ll)e[to].size()});
  e[to].push_back((edge){from,0,-cost,(ll)e[from].size()-1});
}
 
ll min_cost_flow(ll s,ll t,ll f){
  ll res=0;
  fill(h,h+v,0);
  fill(prevv,prevv+MAX,0);
  fill(preve,preve+MAX,0);
  while(f>0){
    priority_queue<pll,vector<pll>,greater<pll> > pq;
    fill(dist,dist+v,linf);
    dist[s]=0;
    pq.push(pll(0,s));
    while(pq.size()){
      pll p=pq.top();
      pq.pop();
      int u=p.second;
      if(dist[u]<p.first)continue;
      for(int i=0;i<e[u].size();i++){
        edge &E=e[u][i];
        if(E.cap>0 && dist[E.to]>dist[u]+E.cost+h[u]-h[E.to]){
          dist[E.to]=dist[u]+E.cost+h[u]-h[E.to];
          prevv[E.to]=u;
          preve[E.to]=i;
          pq.push(pll(dist[E.to],E.to));
        }
      }
    }
    if(dist[t]==linf)return -1;
    for(int i=0;i<v;i++)h[i]+=dist[i];
 
    ll d=f;
    for(int u=t;u!=s;u=prevv[u]){
      d=min(d,e[prevv[u]][preve[u]].cap);
    }
    f-=d;
    res+=d*h[t];
    for(int u=t;u!=s;u=prevv[u]){
      edge &E=e[prevv[u]][preve[u]];
      E.cap-=d;
      e[u][E.rev].cap+=d;
    }
  }
  return res;
}


ll n,k,flow=0;
vector<pii> E[MAX];
ll ind[MAX],outd[MAX];
ll cost[MAX][MAX];

void dijkstra(int root){
  priority_queue<pll,vector<pll>,greater<pll> > pq;
  cost[root][root]=0;
  pq.push(mp(0,root));

  while(pq.size()){
    pll u=pq.top();
    pq.pop();

    if(cost[root][u.s]<u.f)continue;

    FOR(i,0,E[u.s].size()){
      int next=E[u.s][i].f;
      ll c = cost[root][u.s]+E[u.s][i].s;
      if(c<cost[root][next]){
        cost[root][next]=c;
        pq.push(mp(c,next));
      }
    }
  }
}

ll solve(){
  ll ans = 0;
  fill(ind,ind+MAX,0);
  fill(outd,outd+MAX,0);
  FOR(i,0,MAX)fill(cost[i],cost[i]+MAX,inf);
  FOR(i,0,n){
    outd[i] = E[i].size();
    FOR(j,0,E[i].size()){
      ind[E[i][j].f]++;
      ans+=E[i][j].s;
    }
  }
  FOR(i,0,n)dijkstra(i);
  vector<pii> L,R;
  FOR(i,1,n){
    if(outd[i]==0)continue;
    if(outd[i]<ind[i])L.pb(mp(i,ind[i]-outd[i]));
    if(ind[i]<outd[i]){
      R.pb(mp(i,outd[i]-ind[i]));
      flow+=(outd[i]-ind[i]);
    }
  }
  int s=L.size()+R.size(),t=s+1,u=t+1;
  v = u+1;
  FOR(i,0,L.size()){
    FOR(j,0,R.size()){
      int a = L[i].f, b = R[j].f;
      if(cost[a][b]==inf)continue;
      add_edge(i,j+L.size(),R[j].s,cost[a][b]);
    }
  }
  FOR(i,0,L.size())add_edge(s,i,L[i].s,0);
  FOR(i,0,R.size())add_edge(i+L.size(),t,R[i].s,0);
  add_edge(s,u,inf,0);
  FOR(i,0,R.size())add_edge(u,i+L.size(),R[i].s,cost[0][R[i].f]);
  ll res = min_cost_flow(s,t,flow);
  return ans+res;
}

int main()
{
  cin>>n;
  FOR(i,0,n){
    cin>>k;
    FOR(j,0,k){
      ll a,b;
      cin>>a>>b;
      E[i].pb(mp(a-1,b));
    }
  }
  cout<<solve()<<endl;
  return 0;
}

