1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
#define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x) cerr<<#x<<" "<<x<<endl
#define ll long long 
#define st first
#define nd second
using namespace std;

int n, t, l, r, c, temp[4], res, m;
pair<int, int> kol[4][1000006];
int main()
{
	qio;
	cin >> n >> m;

	for (int i = 1; i <= m; i++) {
		cin >> l >> r >> c;
		kol[c][l].st++;
		kol[c][r].nd++;
	}

	for (int i = 1; i <= n; i++) {
		temp[1] += kol[1][i].st;
		temp[2] += kol[2][i].st;
		temp[3] += kol[3][i].st;

		if (temp[1] >= 1 && temp[2] >= 1 && temp[3] == 0) res++;

		temp[1] -= kol[1][i].nd;
		temp[2] -= kol[2][i].nd;
		temp[3] -= kol[3][i].nd;
	}

	cout << res << endl;

}