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
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;

const int TSIZE = 1<<18;
int T[2*TSIZE], n, m, lazy[2*TSIZE];

void propagate(int x) {
	T[x] |= lazy[x];

	if(x < TSIZE) {
		lazy[2*x] |= lazy[x];
		lazy[2*x+1] |= lazy[x];
	}
	lazy[x] = 0;
}

void Update(int type, int l, int r, int L = 0, int R = TSIZE-1, int node = 1) {
	if(lazy[node] != 0) propagate(node);
	if(r < L or R < l)	return;

	if(l <= L and R <= r) {
		lazy[node] |= (1 << type);
		propagate(node);
		return;
	}
	int M = (L+R)/2;
	Update(type, l, r, L, M, 2*node);	Update(type, l, r, M+1, R, 2*node+1);
	T[node] = T[2*node] + T[2*node+1]; 
}

int Query(int l, int r, int L = 0, int R = TSIZE-1, int node = 1) { 
	if(lazy[node] != 0) propagate(node);
	if(r < L or R < l) return 0;
	if(l <= L and R <= r) return T[node];
	int M = (L+R)/2;
	return Query(l, r, L, M, 2*node) + Query(l, r, M+1, R, 2*node+1);
}

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

	cin >> n >> m;
	for(int i = 0; i < m; i++) {
		int a, b, c;	cin >> a >> b >> c;
		Update(c, a, b);
	}
	Query(1, n);
	int r = 0;
	for(int i = 1; i <= n; i++)
		if(Query(i, i) == 6) r++;
	return cout << r, 0;
}