#include<bits/stdc++.h>
using namespace std;
struct node
{ int x;
int y;
int z;
}d[2250001];
int f[2000001];
int m,n,cnt,ans,tot;
int find(int x)
{ if (f[x]!=x) f[x]=find(f[x]);
return f[x];
}
int cmp(node a,node b)
{ return a.z<b.z;
}
int main()
{ freopen("mcst.in","r",stdin);
freopen("mcst.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{ int x;
scanf("%d",&x);
if (x!=-1) {tot++;d[tot].x=i;d[tot].y=j;d[tot].z=x;}}
sort(d+1,d+tot+1,cmp);
for (int i=1;i<=n;i++)
f[i]=i;
for (int i=1;i<=tot;i++)
{ int u=find(d[i].x),v=find(d[i].y);
if (u==v) continue;
cnt++;
ans+=d[i].z;
f[u]=v;
if (cnt==n-1) break;
if (i==10000) {printf("-1");return 0;}
}
printf("%d",ans);
return 0;
}