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
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define IOS cin.tie(0); ios_base::sync_with_stdio(0)
 
using ll = long long;
using namespace std;

const int N = 1e6 + 20;

int n, q, cnt[N][3], out;
 
int main() {
	scanf("%d%d", &n, &q);
	while (q--) {
		int l, r, k;
		scanf("%d%d%d", &l, &r, &k);
		k--;
		cnt[l][k]++;
		cnt[r + 1][k]--;
	}
	for (int a = 0; a < 3; ++a)
		for (int i = 1; i <= n; ++i)
			cnt[i][a] += cnt[i - 1][a];
	for (int i = 1; i <= n; ++i)
		out += (cnt[i][0] > 0 && cnt[i][1] > 0 && cnt[i][2] == 0);
	printf("%d", out);
	return 0;
}