local mmi, mma = math.min, math.max local n = io.read("*n") local imosvel = {} local imos = {} local base = {} for i = 1, n do imosvel[i] = {} imos[i] = {} base[i] = 0 for j = 1, n do imosvel[i][j] = 0 imos[i][j] = 0 end end for ih = 1, n do for jw = 0, n - 1 do local a = io.read("*n") base[a] = base[a] + jw --cost zero space: [ih - jw, ih + jw] if ih + jw < n then imosvel[a][ih + jw + 1] = imosvel[a][ih + jw + 1] + 1 end if 1 < ih - jw then imos[a][1] = imos[a][1] + ih - jw imosvel[a][1] = imosvel[a][1] - 1 if ih - jw + 1 <= n then imosvel[a][ih - jw + 1] = imosvel[a][ih - jw + 1] + 1 end end end end local tot = 0 for i = 1, n do imos[i][1] = imos[i][1] + imosvel[i][1] local cand = imos[i][1] for j = 2, n do imosvel[i][j] = imosvel[i][j] + imosvel[i][j - 1] imos[i][j] = imos[i][j] + imos[i][j - 1] + imosvel[i][j] cand = mmi(cand, imos[i][j]) end tot = tot + base[i] + cand end print(tot)