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
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct XYZ{
	int a, b, c;
};
int n, m;

bool sort_f(XYZ a, XYZ b){
	if(a.a==b.a && b.b==a.b){
		return a.c<b.c;
	}
	if(a.a==b.a) return a.b<b.b;
	return a.a<b.a;
}


int main(){
	scanf("%d %d",&n, &m);
	int a, b, c;
	
	vector<XYZ> v;
	while(m--){
		XYZ x;
		scanf("%d %d %d", &a, &b, &c);
		x.a=a;
		x.b=1;
		x.c=c;
		v.push_back(x);
		x.a=b+1;
		x.b=-1;
		x.c=c;
		v.push_back(x);
	} 
	sort(v.begin(), v.end(), sort_f);
	a=b=c=0;
	int x=1;
	int sum=0;
	for(auto it=v.begin(); it!=v.end(); it++){
		int p=it->a;
		if(a>0 && b>0 && c==0) sum+=(p-x);
		x=p;
		if(it->c==1)a+=it->b;
		if(it->c==2)b+=it->b;
		if(it->c==3)c+=it->b;
	}
	printf("%d\n", sum);
	
	
	return 0;
}