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
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)

typedef pair<int,int> PII;
typedef vector<PII> VII;

int c[3];

int main() {
	INT(n);
	INT(m);
	VII v;
	REP(i,m) {
		INT(l);
		INT(r);
		INT(k);
		v.PB(MP(l, k));
		v.PB(MP(r + 1, -k));
	}
	sort(ALL(v));
	int r = 0, x = 0;
	FORE(it,v) {
		if (c[0] && c[1] && !c[2]) r += it->FT - x;
		if (it->SD > 0) ++c[it->SD - 1];
		else --c[-it->SD - 1];
		x = it->FT;
	}
	printf("%d\n", r);
}