// Arup Guha
// 3/10/2022
// Solution to 2021 SER D1 Problem: Hopscotch 500

import java.util.*;
import java.io.*;

public class hopscotch500 {

	public static int n;
	public static int k;
	public static int[][] vals;
	public static ArrayList<Integer>[] locs;
	
	public static void main(String[] args) throws Exception {
	
		// Get basic input.
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		n = Integer.parseInt(tok.nextToken());
		k = Integer.parseInt(tok.nextToken());
		
		locs = new ArrayList[k];
		for (int i=0; i<k; i++)
			locs[i] = new ArrayList<Integer>();
		
		// Get grid. Also store for each value, where it is.
		vals = new int[n][n];
		for (int i=0; i<n; i++) {
			tok = new StringTokenizer(stdin.readLine());
			for (int j=0; j<n; j++) {
				vals[i][j] = Integer.parseInt(tok.nextToken())-1;
				locs[vals[i][j]].add(n*i+j);
			}
		}
		
		// Ta da!
		System.out.println(go());
	}
	
	public static long go() {
	
		// Easy to screen out these cases.
		for (int i=0; i<k; i++)
			if (locs[i].size() == 0)
				return -1;
	
		// Will store shortest distances to current value in these maps.
		// Just need shortest distance to each row and each column,
		HashMap<Integer, Long> rPrevMap = new HashMap<Integer, Long>();
		HashMap<Integer, Long> cPrevMap = new HashMap<Integer, Long>();	
		
		// Stores 0 to each row and column where value 1 exists.
		for (Integer idx: locs[0]) {
			int r = idx/n;
			int c = idx%n;
			rPrevMap.put(r, 0L);
			cPrevMap.put(c, 0L);
		}
		
		// Now, we march through the rest of the numbers.
		for (int i=1; i<k; i++) {
		
			// Same role for current value (in this case i).
			HashMap<Integer, Long> rCurMap = new HashMap<Integer, Long>();
			HashMap<Integer, Long> cCurMap = new HashMap<Integer, Long>();
			
			// Update best to each location storing i+1. Just do rows here.
			for (Integer idx: locs[i]) {
			
				int r = idx/n;
				int c = idx%n;
				
				// Will be overwritten.
				long res = 1000000000000000000L;

				// Go through all previous locations (n rows) to coming here.
				for (Integer otherR: rPrevMap.keySet()) {
					long oldD = rPrevMap.get(otherR);
					long delta = Math.abs(r-otherR);
					long newD = oldD + delta*delta;
					res = Math.min(res, newD);
				}
				
				// And in columns.
				for (Integer otherC: cPrevMap.keySet()) {
					long oldD = cPrevMap.get(otherC);
					long delta = Math.abs(c-otherC);
					long newD = oldD + delta*delta;
					res = Math.min(res, newD);
				}				
				
				// Only update if this makes it better!
				long prevRAns = 1000000000000000000L;
				if (rCurMap.containsKey(r)) prevRAns = rCurMap.get(r);
				if (res < prevRAns) rCurMap.put(r, res);
				
				// Same for columns.
				long prevCAns = 1000000000000000000L;
				if (cCurMap.containsKey(c)) prevCAns = cCurMap.get(c);
				if (res < prevCAns) cCurMap.put(c, res);
			}			

			// Update maps.
			rPrevMap = rCurMap;
			cPrevMap = cCurMap;
		}
		
		// Just get shortest.
		long res = 1000000000000000000L;
		for (Integer row: rPrevMap.keySet())
			res = Math.min(res, rPrevMap.get(row));
		for (Integer col: cPrevMap.keySet())
			res = Math.min(res, cPrevMap.get(col));
		return res;
	}
}