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

using namespace std;

const int levels = 21;
const int st_size = 1<<levels;
const int st_first = st_size/2;
int st[st_size];

void _insert(int p, int q, int a, int b, int n, int v) {
    if(p <= a && b <= q) {
        st[n] |= (1<<(v-1));
        return;
    }

    int m = (a+b)/2;

    if(p <= m) {
        _insert(p, q, a, m, 2*n, v);
    }

    if(m+1 <= q) {
        _insert(p, q, m+1, b, 2*n+1, v);
    }
}

void insert(int p, int q, int v) {
    _insert(p, q, 1, st_first, 1, v);
}

void print() {
    for(int i = 1; i <= levels; i++) {
        for(int j = pow(2, i-1); j < pow(2, i); j++) {
            cout << st[j] << " ";
        }
        cout << endl;
    }
}

int check(int n) {
    n = st_first-1+n;

    int c = 0;
    while(n) {
        c |= st[n];
        n /= 2;
    }

    return c;
}

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

    int n, m;
    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        int l, r, k;
        cin >> l >> r >> k;
        insert(l, r, k);
    }

    // print();

    int c = 0;
    for(int i = 1; i <= n; i++) {
        if(check(i) == 3) {
            c += 1;
        }
    }
    cout << c << "\n";

    return 0;
}