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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>

using namespace std;

//000 biały
//001 zółty
//010 niebieski
//100 czerwony
//011 zielony

const int tree_base = 1 << 20;//20

int tree[tree_base * 2] = {};

void tree_or(int a, int b, int c) {
	a += tree_base;
	b += tree_base;
	while (a < b) {
		if (a % 2 == 1) {
			tree[a] = tree[a] | c;
			a++;
		}
		if (b % 2 == 0) {
			tree[b] = tree[b] | c;
			b--;
		}
		a /= 2;
		b /= 2;
	}
	if (a == b) {
		tree[a] = tree[a] | c;
	}
}

int check_tree(int a) {
	a += tree_base;
	int ret = 0;
	while (a > 0) {
		ret = tree[a] | ret;
		a /= 2;
	}
	return ret;
}

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

	int n, m;
	cin >> n >> m;
	int l, r, k;
	for (int i = 0; i < m; i++) {
		cin >> l >> r >> k;
		if (k == 1) {
			k = 1;
		}
		else if (k == 2) {
			k = 2;
		}
		else {
			k = 4;
		}
		tree_or(l, r, k);
	}
	int wyn = 0;
	for (int i = 1; i <= n; i++) {
		if (check_tree(i) == 3) {
			wyn++;
		}
	}
	cout << wyn;

	return 0;
}
/*
9 5
1 7 1
3 4 2
5 6 3
4 5 2
0 1 2

*/