(function(stdin) {
  var lines = stdin.trim().split('\n');
  var n = parseInt(lines.shift().trim(), 10);
  var m = lines.map(function(row) {
    return row.trim().split(' ').map(Number);
  });
  var color = [], d = [], p = [];
  for (var i = 0; i < n; i++) {
    color[i] = 'WHITE';
    d[i] = Infinity;
    p[i] = -1;
  }
  d[0] = 0;
  p[0] = -1;
  var u, minv;
  while (1) {
    minv = Infinity;
    for (var i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != 'BLACK') {
        u = i;
        minv = d[u];
      }
    }
    if (minv == Infinity) {
      break;
    }
    color[u] = 'BLACK';
    for (var v = 0; v < n; v++) {
      if (color[v] != 'BLACK' && m[u][v] != -1) {
        if (m[u][v] < d[v]) {
          d[v] = m[u][v];
          p[v] = u;
          color[v] = 'GRAY';
        }
      }
    }
  }
  var sum = 0;
  for (var i = 0; i < n; i++) {
    if (p[i] != -1) {
      //sum += m[i][p[i]];
      sum += d[i];
    }
  }
  console.log(sum);
})(require('fs').readFileSync('/dev/stdin', 'utf8'));