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>
using namespace std;
using Int = long long;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b)a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b)a=b;};

struct TopologicalSort{
  Int n;
  vector<vector<Int> > G;
  vector<bool> used;
  vector<Int> indeg, p;

  TopologicalSort(){}
  TopologicalSort(Int sz):n(sz), G(n), used(n), indeg(n), p(0){}

  void add_edge(Int s, Int t){
    G[s].emplace_back(t);
  }

  void bfs(Int s){
    queue<Int> q;
    q.push(s);
    used[s] = 1;
    while(!q.empty()){
      Int v = q.front(); q.pop();
      p.push_back(v);
      for(Int u:G[v]){
        indeg[u] --;
        if(indeg[u] == 0 && !used[u]){
          used[u] = 1;
          q.push(u);
        }
      }
    }
  }

  vector<Int> build(){
    fill(used.begin(), used.end(), 0);
    fill(indeg.begin(), indeg.end(), 0);

    for(Int i=0;i<n;i++)
      for(Int v:G[i]) indeg[v]++;

    for(Int i=0;i<n;i++)
      if(indeg[i] == 0 && !used[i]) bfs(i);
    return p;
  }

};

template<typename T>
struct PrimalDual{
  struct edge{
    Int to;
    T cap, cost;
    Int rev;
    edge(){}
    edge(Int to, T cap, T cost,Int rev):
      to(to), cap(cap), cost(cost), rev(rev){}
  };

  T INF;
  vector<vector<edge> > G;
  vector<T> h, dist;
  vector<Int> prevv, preve;
  TopologicalSort ts;

  PrimalDual(){}
  PrimalDual(Int n, T INF):
    INF(INF), G(n), h(n), dist(n), prevv(n), preve(n),ts(n){}

  void add_edge(Int from, Int to, T cap, T cost){
    G[from].emplace_back(to, cap, cost, G[to].size());
    G[to].emplace_back(from, 0, -cost, G[from].size()-1);
    ts.add_edge(from,to);
  }

  T flow(Int s,Int t, Int f, Int &ok){
    T res = 0;
    fill(h.begin(), h.end(), 0);

    {
      auto ord=ts.build();
      for(Int v:ord){
        if(dist[v] == INF) continue;
        for(Int i=0;i<(Int)G[v].size();i++){
          edge &e = G[v][i];
          if(e.cap == 0) continue;
          if(dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
          }
        }
      }
      for(Int v=0;v<(Int)h.size();v++) h[v] += dist[v];
    }

    
    while(f > 0){
      //using P = pair<T, Int>;
      struct P{
        T first;
        Int second;
        P(T first,Int second):first(first),second(second){}
        bool operator<(const P&a) const{return first>a.first;}
      };
      
      priority_queue<P> que;
      fill(dist.begin(), dist.end(), INF);

      dist[s] = 0;
      que.emplace(dist[s], s);
      while(!que.empty()){
        P p = que.top(); que.pop();
        Int v = p.second;
        if(dist[v] < p.first) continue;
        for(Int i=0;i<(Int)G[v].size();i++){
          edge &e = G[v][i];
          if(e.cap == 0) continue;
          if(dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            que.emplace(dist[e.to], e.to);
          }
        }
      }
      if(dist[t] == INF) return ok = 0;
      for(Int v=0;v<(Int)h.size();v++) h[v] += dist[v];

      T d = f;
      for(Int v=t;v!=s;v = prevv[v])
        d = min(d, G[prevv[v]][preve[v]].cap);

      f -= d;
      res += d * h[t];
      for(Int v=t;v!=s;v=prevv[v]){
        edge &e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    ok = 1;
    return res;
  }
};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  
  Int n;
  cin>>n;
  const Int INF2 = 1e9;
  const Int INF3 = 1e15;
  PrimalDual<Int> G(n+1,INF3);
  Int num=0,ans=0;
  for(Int i=0;i<n;i++){   
    Int k;
    cin>>k;
    num+=k;
    map<Int, Int> cnt;
    for(Int j=0;j<k;j++){
      Int t,c;
      cin>>t>>c;
      t--;
      cnt[t]++;
      ans+=c+INF2;
      G.add_edge(i,t,INF2,c);
    }
    
    for(auto p:cnt)
      G.add_edge(i,p.first,p.second,-INF2);
      
    G.add_edge(i,n,INF2,0);
  }
  
  Int ok=1;
  ans+=G.flow(0,n,INF2,ok);  
  cout<<ans<<endl; 
  assert(ok);
  return 0;
}


