#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int>* > v;
vector<int> pret;
int main()
{
	//ios_base::sync_with_stdio(false);
	int n, k, x, p;
	cin >> n >> k;
	v.resize(k + 10);
	pret.resize(n + 10);
	bool r = 1;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(r) cin >> x;
			r = 1;
			//cout << j << "l\n";
			if(!v[x])
			{
				v[x] = new pair<int, int>(j, -1);
				j++;
				pret[j - 1] = x;
				for(; j < n && r; j++)
				{
					cin >> x;
					//cout << j << "h" << x;
					r = (pret[j - 1] == x);
					pret[j] = x;
				}
				if(!r) j-=2;
				else j--;
				//cout << pret[j] << "t";
				v[pret[j]]->second = j;
				//cout << "u";
			}
			else if(v[x]->first != -1 && ((j < v[x]->first || j > v[x]->second) || x != pret[j]))
			{
				v[x] = new pair<int, int>(-1, -1);
				pret[j] = x;
			}
			//cout << v[pret[j]]->first << " " << v[pret[j]]->second << "l\n";
			
		}
	}
	int s = 0;
	for(int i = 0; i < k; i++)
		if(v[i] && v[i]->first != -1)
		{
			//cout << i << " ";
			s++;
		}
	cout << s;
			
}