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
#include <bits/stdc++.h>
using namespace std;

#define st first
#define nd second
#define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

long long n, m, a, b, c, res, t[4] = {1, 1, 1, 1}, l, r, tk[4][1000006];
pair<int, int> p[4][1000006];

int main()
{
    iamspeed;
    cin >> n >> m;
    for(int i = 1 ; i <= m ; i++)
    {
        cin >> a >> b >> c;
        p[c][t[c]].st = a;
        p[c][t[c]].nd = b;
        t[c]++;
    }
    for(int i = 1 ; i <= 3 ; i++)
    {
        sort(p[i] + 1, p[i] + t[i] + 1);
        for(int j = p[i][1].st ; j <= p[i][1].nd ; j++)
        {
            tk[i][j] = 1;
        }
        for(int j = 2 ; j <= t[i] ; j++)
        {
            l = max(p[i][j].st, p[i][j - 1].nd);
            r = p[i][j].nd;
            for(int k = l ; k <= r ; k++)
            {
                tk[i][k] = 1;
            }
        }
    }
    for(int i = 1 ; i <= n ; i++)
    {
        if(tk[1][i] == 1 && tk[2][i] == 1 && tk[3][i] == 0)
        {
            res++;
        }
    }
    cout << res << endl;
}