using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace petljTakm2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int N = int.Parse(s[0]);
            int K = int.Parse(s[1]);
            int[,] polje = new int[N, N];
            int k=0;
            for (int i = 0; i < N; i++)
            {
                string[] st = Console.ReadLine().Split(' ');
                for (int j = 0; j < N; j++)
                {
                    polje[i, j] = int.Parse(st[j]);
                }
            }
            int[] dostupnost = new int[K + 1];
            int resenje = 0;
            /*
            5 10
3 2 2 1 4
3 2 2 7 4
8 2 2 1 3
8 8 9 9 9
8 8 1 5 1
             */
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (dostupnost[polje[i, j]]==0)
                    {
                        dostupnost[polje[i, j]] = 1;
                        //Console.WriteLine("Dostupno je " + polje[i,j]);
                        if (j != N - 1)
                        {
                            if (polje[i, j + 1] != polje[i, j])
                            {
                                for (int l = i; l < N; l++)
                                {
                                    for (int m = j + 2; m < N; j++)
                                    {
                                        if (polje[l, m] == polje[i, j])
                                        {
                                            //Console.WriteLine("{0} {1} == {2} {3}",l,m,i,j);
                                            goto a;
                                        }
                                    }
                                }
                                //Console.WriteLine(polje[i, j]);
                                resenje++;
                            }
                            else { 
                                //Console.WriteLine(polje[i, j]);
                                resenje++; 
                            }
                        }
                        else { 
                            //Console.WriteLine(polje[i, j]); 
                            resenje++;
                        }
                    }
                a: k = 1;
                }
            }
            Console.WriteLine(resenje);
        }
    }
}