package algo_0830;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class PrimTest2 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int V = Integer.parseInt(br.readLine());
int[][] adjMatrix = new int[V][V];
boolean[] visited = new boolean[V];
int[] minEdge = new int[V];
for(int i = 0; i < V; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 0; j < V; j++) {
adjMatrix[i][j] = Integer.parseInt(st.nextToken());
}
}
Arrays.fill(minEdge, Integer.MAX_VALUE);
minEdge[0] = 0;
int cost = 0;
int i = 0;
for(i = 0 ; i < V; i++) {
int min = Integer.MAX_VALUE;
int minVertex = -1;
for(int j = 0; j < V; j++) {
if(visited[j]) continue;
if(min > minEdge[j]) {
minVertex = j;
min = minEdge[j];
}
}
if(minVertex == -1) break;
visited[minVertex] = true;
cost += min;
for(int j = 0; j < V; j++) {
if(!visited[j] && adjMatrix[minVertex][j] > 0 && minEdge[j] > adjMatrix[minVertex][j]) {
minEdge[j] = adjMatrix[minVertex][j];
}
}
}
int result = i == V ? cost : -1;
System.out.println(result);
}
}