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 M = (1<<4);

struct node{
    bool farbe[4];

    node (){
        farbe[0] = false; farbe[1] = false; farbe[2] = false; farbe[3] = false;
    }
};

int n, m;

node tree[2*M];

void update (int lewy, int prawy, int kolor){
    lewy += M;
    prawy += M;

    tree[lewy].farbe[kolor] = true;
    if (lewy != prawy) tree[prawy].farbe[kolor] = true;

    while (lewy/2 != prawy/2){
        if (lewy%2 == 0)    tree[lewy+1].farbe[kolor] = true;
        if (prawy%2 == 1)   tree[prawy-1].farbe[kolor] = true;
        lewy/=2;
        prawy/=2;
    }
}

/*bool ask_if_green (int poz){
    bool ans_gelb = false, ans_blue = false, ans_red = false;

    poz += M;

    while (poz){
        if (tree[poz].farbe[1]) ans_gelb = true;
        if (tree[poz].farbe[2]) ans_blue = true;
        if (tree[poz].farbe[3]) ans_red = true;
        poz/=2;
    }

    if (ans_gelb && ans_blue && !ans_red) return true;
    else return false;
}*/

void ustaw (){

    for (int i = 2; i < 2*M; i++){
        for (int j=1; j<=3; j++){
            if (tree[i/2].farbe[j]) tree[i].farbe[j] = true;
        }
    }
}


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

    cin >> n >> m;

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

    }
    ustaw();
    int ans = 0;
    for (int i=M; i<=n+M; i++){
        if (tree[i].farbe[1] && tree[i].farbe[2] && !tree[i].farbe[3]) ans++;
    }

    cout << ans << "\n";
}