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
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>

using namespace std;

int n, m;

int l[1000005], r[1000005], k[1000005];
int pref[1000005][4];

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> n >> m;
	for (int i = 0; i < m; ++i)
	{
		cin >> l[i] >> r[i] >> k[i];
	}

	for(int kol = 1; kol <= 3; kol++){
		for (int i = 0; i < m; ++i)
		{
			if(k[i] == kol){
				pref[l[i]][kol]++;
				pref[r[i] + 1][kol]--;
			}
		}
	}

	long long ans = 0;
	int yel, blu, red;
	yel = blu = red = 0;
	for (int i = 1; i <= n; ++i)
	{
		//cout << yel << " " << blu << " " << red << endl;
		yel += pref[i][1];
		blu += pref[i][2];
		red += pref[i][3];
		if(yel > 0 and blu > 0 and red == 0){
			ans++;
		}
	}

	cout << ans << endl;


}