#include <bits/stdc++.h>

using namespace std;


int main ()
{
    ios_base::sync_with_stdio(false);

    int n, brojac=0;
    long long k;
    unordered_map<long long, vector<int>> mapa;

    cin >> n >> k;
    
    long niz[n*n];


    for (int i=0;i<n*n;i++) cin >> niz[i];

    int i=0;
    while (i<n*n)
    {
        vector<int> v = vector<int>(3);
        v[0] = i%n;
        v[2] = i/n;
        int elem = niz[i];
        while(niz[++i] == elem && (i/n)==v[2]);
        v[1] = (i-1) % n;
        if (mapa.count(elem) == 0) { mapa[elem] = v; brojac++; }
        else if (mapa[elem][0]==v[0] && mapa[elem][1]==v[1] && (v[2]-mapa[elem][2])==1) mapa[elem][2]++;
        else if (mapa[elem][2]!=-1) {mapa[elem][2]=-1; brojac--;}

    }

       /* for(auto& pair : mapa )
        {
            cout << "key: " << pair.first << "  value: [  " ;
            for( int d : pair.second ) cout << d << "  " ;
            cout << "]\n" ;
        }*/
        cout<<brojac;

        return 0;
}