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
#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto & i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

typedef std::array<int, 3> T;

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

	int n, m;
	std::cin >> n >> m;
	std::vector<T> A(n+1, T{0, 0, 0});
	FOR(i, m){
		int a, b, c;
		std::cin >> a >> b >> c;
		a--;b--;c--;
		A[a][c]++;
		A[b+1][c]--;
	}

	int ans = 0;
	T have{0, 0, 0};
	FOR(i, n){
		FOR(j, 3) have[j] += A[i][j];
		int mask = 0;
		FOR(j, 3){
			assert(have[j] >= 0);
			if(have[j]) mask |= (1<<j);
		}
		if(mask == 3) ans++;
	}

	std::cout << ans << "\n";

	return 0;
}