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
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iPair;
typedef pair<ll,ll> llPair;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<iPair> viP;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int modulo = 1e9 + 7; 
#define forr(i, b, e) for (int i = b; i < e; ++i)
#define ford(i, b, e) for (int i = b; i >= e; --i)
#define fort(i, x) for (auto &i : (x))
#define sze(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define sp <<" "
#define spa <<" "<<
#define el <<"\n"

struct oper{
	int in, st, col, id;
};

bool comp (const oper &a, const oper &b) {
	if (a.in == b.in)
		return a.st < b.st;
	return a.in < b.in;
}

int n, m;
vector<oper> ops;

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

	cin >> n >> m;
	forr(i, 0, m) {
		int a,b,c;
		cin >> a >> b >> c;
		oper o1;
		o1.in = a-1;
		o1.st = 1;
		o1.col = c-1;
		o1.id = i;
		ops.pb(o1);
		o1.in = b;
		o1.st = 0;
		ops.pb(o1);
	}

	sort(ops.begin(), ops.end(), comp);

	int it = 0;

	int curr[3];
	vi ids[3];
	forr(i, 0, 3) curr[i] = 0;

	int score = 0;

	forr(i, 0, n) {

		while(it < sze(ops) && ops[it].in == i) {
			if (ops[it].st) {
				curr[ops[it].col] = 1;
				ids[ops[it].col].pb(ops[it].id);
			}
			else {
				ids[ops[it].col].erase(ids[ops[it].col].begin());
				if (!sze(ids[ops[it].col]))
					curr[ops[it].col] = 0;
			}
			it++;
		}

		if (curr[0] && curr[1] && !curr[2])
			score++;
	}

	cout << score el;

	return 0;
}