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
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)

#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

struct event {
	int time;
	int change;
	int color;

	bool operator<(const event& e) {
		if(time != e.time) return time < e.time;
		if(change != e.change) return change < e.change;
		return color < e.color;
	}
};

int main() {
	vector<event> timeline;
	GET(n);
	GET(m);
	while(m--) {
		GET(u);
		GET(v);
		GET(color);
		--u, v, --color;
		timeline.pb({u, +1, color});
		timeline.pb({v, -1, color});
	}
	sort(ALL(timeline));
	int state[3]{0,0,0};
	int index = 0;
	int green = 0;
	FOR(i,0,n) {
		while(index < timeline.sz && timeline[index].time == i) {
			state[timeline[index].color] += timeline[index].change;
			++index;
		}
		dprintf("state Y=%d B=%d R=%d\n", state[0], state[1], state[2]);
		if(state[0] > 0 && state[1] > 0 && state[2] == 0)
			++green;
	}
	printf("%d\n", green);
	return 0;
}