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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void klejent(int pos, vector<int> &results, vector<pair<int,int>> &t){
    for(int i=0; i<t.size(); i++){
        if(pos<t[i].first-1){
            pos=t[i].first-1;
        }
        if(t[i].second-1<pos){
            continue;
        }
        while(pos<=t[i].second-1){
            results[pos]++;
            pos++;
        }
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    vector<int> results(n+1,0);
    vector<pair<int,int>> t1;
    vector<pair<int,int>> t2;
    vector<pair<int,int>> t3;
    for(int i=0; i<m; i++){
        int l,r,k;
        cin>>l>>r>>k;
        if(k==1){
            t1.push_back({l,r});
        }
        else if(k==2){
            t2.push_back({l,r});
        }
        else if(k==3){
            t3.push_back({l,r});
        }
    }
    sort(t1.begin(),t1.end());
    sort(t2.begin(),t2.end());
    sort(t3.begin(),t3.end());
    int pos=0;
    klejent(pos,results,t1);
    klejent(pos,results,t2);
    for(int i=0; i<t3.size(); i++){
        if(pos<t3[i].first-1){
            pos=t3[i].first-1;
        }
        if(t3[i].second-1<pos){
            continue;
        }
        while(pos<=t3[i].second-1){
            results[pos]+=5;
            pos++;
        }
    }
    int res=0;
    for(int i=0; i<n; i++){
        if(results[i]==2){
            res++;
        }
    }
    cout<<res<<endl;
    return 0;
}