#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>
#include <sstream>


#define ll long long
#define ld long double
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int> 
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
ll LINF = (ll)4e18;

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.setf(ios::fixed);
	int n, m;
	cin >> n >> m;
	vector<vector<pii>> gr[2];
	gr[0].resize(n);
	gr[1].resize(n);
	forn(i, 0, m) {
		int k;
		cin >> k;
		int last = -1;
		vi path(k);
		forn(j, 0, k) {
			cin >> path[j];
			path[j]--;
		}
		forn(j, 0, k) {
			int next = n + i;
			if (j + 1 < k) {
				next = path[j + 1];
			}
			gr[0][path[j]].push_back({ j, last });
			gr[1][path[j]].push_back({ j, next });
			last = path[j];
		}
	}
	ll sum = 0;
	forn(i, 0, n) {
		forn(q, 0, 2) {
			if (gr[q][i].size() != 0) {
				sort(all(gr[q][i]));
				map<int, int> cnt;
				int last = gr[q][i][0].first;
				gr[q][i].push_back({ -2, -2 });
				cnt[gr[q][i][0].second]++;
				forn(j, 1, gr[q][i].size()) {
					if (gr[q][i][j].first != last) {
						int curs = 0, cur_cnt = 0;
						for (auto& t : cnt) {
							curs += t.second;
						}
						for (auto& t : cnt) {
							cur_cnt += t.second * (curs - t.second);
						}
						sum += cur_cnt / 2;
						cnt.clear();
						cnt[gr[q][i][j].second]++;
					}
					else {
						cnt[gr[q][i][j].second]++;
					}
					last = gr[q][i][j].first;
				}
			}
		}
	}
	cout << sum;
	return 0;
}