using System;
using System.Linq;

namespace takkvalifikacije
{
    class Program
    {
        static void Main()
        {
            /*gen();
            return;*/
            int N, K; int[] input;

            input = Console.ReadLine().Split(' ').Select(v => int.Parse(v)).ToArray();
            N = input[0];
            K = input[1] - 1;

            short[,] poljeSirina = new short[K, 2];

            for (int i = 0; i < K; i++)
                poljeSirina[i, 0] = -2;
            for (short i = 0; i < N; i++)
            {
                input = Console.ReadLine().Split(' ').Select(v => int.Parse(v) - 1).ToArray();
                for (short j = 0; j < N; j++)
                {
                    int poljeTip = input[j];
                    short iStart = j, iEnd = j;
                    while (++iEnd < N && input[iEnd] == poljeTip) ;
                    if (poljeSirina[poljeTip, 0] == -2)
                    {
                        poljeSirina[poljeTip, 0] = iStart;
                        poljeSirina[poljeTip, 1] = iEnd;
                    }
                    else if (poljeSirina[poljeTip, 0] != -1)
                    {
                        short iStartPolje = poljeSirina[poljeTip, 0],
                              iEndPolje = poljeSirina[poljeTip, 1];
                        if (iStartPolje != iStart || iEndPolje != iEnd)
                            poljeSirina[poljeTip, 0] = -1;
                    }
                    j = --iEnd;
                }
            }
            int pogodnihTipova = 0;
            for (int i = 0; i < K; i++)
                if (poljeSirina[i, 0] >= 0) pogodnihTipova++;
            Console.WriteLine(pogodnihTipova);
            Console.ReadKey();
        }

        /*static void gen()
        {
            Random random = new Random();
            int N = 20, K = 50;
            Console.WriteLine("{0} {1}", N, K);
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                    Console.Write(random.Next(1, K) + (j == N - 1 ? "" : " "));
                Console.WriteLine();
            }
            Console.ReadKey();
        }*/
    }
}