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 <iostream>

using namespace std;

const int N = 1 << 20;
const int LOG = 20;

int n, m;
bool T[4][N+N];

bool query(int k, int a) {
    a += N;
    
    bool val = T[k][a];
    
    while (a > 1) {
        a >>= 1;
        val |= T[k][a];
    }
    
    return val;
}

void update(int k, int a, int b) {
    a += N;
    b += N;
    
    T[k][a] = 1;
    T[k][b] = 1;
    
    while ((a >> 1) < (b >> 1)) {
        if ((a & 1) == 0) {
            T[k][a+1] = 1;
        }
        
        if ((b & 1) == 1) {
            T[k][b-1] = 1;
        }
        
        a >>= 1;
        b >>= 1;
    }
}

void init() {
    cin >> n >> m;
}

void solve() {
    for (int i=0; i<m; i++) {
        int l, r, k;
        cin >> l >> r >> k;
        
        update(k, l, r);
    }
    
    int cnt = 0;
    
    for (int i=1; i<=n; i++) {
        if (query(1, i) == 1 && query(2, i) == 1 && query(3, i) == 0) {
            cnt++;
        }
    }
    
    cout << cnt << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    init();
    solve();
    
    return 0;
}