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
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
#define For(i,s,a) for(int i = (int)s; i < (int)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define sz size()
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
 
template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
return os << x.ff << " " << x.dd;
}
 
/*#define ONLINE_JUDGE
#ifdef ONLINE_JUDGE
	freopen("cottages.in" , "r" , stdin);
	freopen("cottages.out" , "w" , stdout);
#endif
*/

const int N = 1e6 + 1;

vector<int>events[N];
int ile[3];

int32_t main(void){
	int n, m;
	scanf("%d%d", &n, &m);
	For(i, 0, m){
		int g, h, k;
		scanf("%d%d%d", &g, &h, &k);
		events[g - 1].pb(k);
		events[h].pb(-k);
	}
	int wyn = 0;
	int mask = 0;
	For(i, 0, n){
		for(auto s : events[i]){
			if(s > 0)
				++ile[s - 1];
			else
				--ile[-s - 1];
		}
		mask = 0;
		For(bit, 0, 3)
			if(ile[bit])
				mask |= (1 << bit);
		wyn += mask == 3;
	}
	printf("%d", wyn);
}
/*
9 5
2 8 1
4 5 2
6 7 3
5 6 2
1 2 2
*/