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
#include<cstdio>
#include<algorithm>
using namespace std;

int n, m;
int tree[(1<<21) + 5];

void Update(int L, int R, int mno){
	L += 1<<20;
	R += 1<<20;
	L--;
	R++;
	while(L/2 != R/2){
		if(L%2 == 0) tree[L+1] = (tree[L+1] * mno)%30;
		if(R%2 == 1) tree[R-1] = (tree[R-1] * mno)%30;
		L /= 2;
		R /= 2;
	}
}

int Query(int x){
	x += 1<<20;
	int wynik = 1;
	while(x != 0){
		wynik *= tree[x];
		wynik %= 30;
		x /= 2;
	}
	return wynik;
}

int main(){
	scanf("%d %d", &n, &m);
	for(int i = 0; i < (1<<21); i++){
		tree[i] = 1;
	}
	for(int i = 1; i <= m; i++){
		int L, R, kol;
		scanf("%d %d %d", &L, &R, &kol);
		if(kol == 1) Update(L, R, 2);
		else if(kol == 2) Update(L, R, 3);
		else Update(L, R, 5);
	}
	int wynik = 0;
	for(int i = 1; i <= n; i++){
		int c = Query(i);
		if(c % 6 == 0 and c % 5 != 0) wynik++;
	}
	printf("%d", wynik);
	return 0;
}