#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF INT_MAX/3
#define mod 1000000007

#define MAX_V 1010

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

int V;
vector<edge> g[MAX_V];
ll h[MAX_V];
ll dist[MAX_V];
int prevv[MAX_V],preve[MAX_V];

void init(int v){
  V=v;
  for(int i=0;i<MAX_V;i++){
    g[i].clear();
  }
}

void add_edge(int from,int to,int cap,int cost){
  g[from].push_back((edge){to,cap,cost,(int)g[to].size()});
  g[to].push_back((edge){from,0,-cost,(int)g[from].size()-1});
}

ll min_cost_flow(int s,int t,int f){
  ll res=0;
  memset(h,0,sizeof(h));
  while(f>0){
    priority_queue<P,vector<P>,greater<P> > que;
    fill(dist,dist+V,INF);
    dist[s]=0;
    que.push(P(0,s));
    while(que.size()){
      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&&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.push(P(dist[e.to],e.to));
        }
      }
    }
    if(dist[t]==INF)return -1;
    for(int v=0;v<V;v++) h[v]+=dist[v];

    ll d=f;
    for(int v=t;v!=s;v=prevv[v]){
      d=min(d,(ll)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;
    }
  }
  return res;
}

int N;
vector<P> g0[1001];
int d[1001][1001];
int indeg[1001],outdeg[1001];

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin>>N;
  ll res=0;
  rep(i,N){
    int k;
    cin>>k;
    rep(j,k){
      int a,b;
      cin>>a>>b;
      a--;
      g0[i].push_back(P(a,b));
      outdeg[i]++; indeg[a]++;
      res+=b;
    }
  }

  rep(i,N)rep(j,N)d[i][j]=INF;
  rep(i,N){
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(0,i));
    while(que.size()){
      P p=que.top(); que.pop();
      if(d[i][p.se]!=INF)continue;
      d[i][p.se]=p.fi;
      for(P e : g0[p.se]){
        que.push(P(p.fi+e.se,e.fi));
      }
    }
  }

  init(N+2);
  int source=N,sink=N+1;
  int fsum=0;
  rep(i,N){
    if(i==0||indeg[i]>outdeg[i]){
      if(i==0) add_edge(source,i,INF,0);
      else add_edge(source,i,indeg[i]-outdeg[i],0);
    }
    if(indeg[i]<outdeg[i]){
      add_edge(i,sink,outdeg[i]-indeg[i],0);
      fsum+=outdeg[i]-indeg[i];
    }
  }
  rep(i,N){
    for(P e : g0[i]){
      add_edge(i,e.fi,INF,e.se);
    }
  }
  cout<<res+min_cost_flow(source,sink,fsum)<<endl;

  return 0;
}

