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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
#include <algorithm>

using namespace std;

#ifdef USE_CERR_LOG
#define LOG if (true) cerr
const bool LogEnabled = true;
#else
#define LOG if (false) cerr
const bool LogEnabled = false;
#endif

bool LogBigEnabled = true;

#ifdef USE_FILE_CIN
ifstream fin("kol0.in");
#define cin fin
#endif


typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;


const int White = 0;
const int Yellow = 1;
const int Blue = 2;
const int Red = 3;

const int Remove = 0;
const int Add = 1;

typedef array<int, 4> Layers;


struct Mix {
    int can;
    int color;
    int op;
};

bool operator < (const Mix& mix1, const Mix& mix2) {
    if (mix1.can == mix2.can) return mix1.op < mix2.op;
    else return mix1.can < mix2.can;
}

inline bool isGreen(Layers& layers) {
    return layers[Yellow] > 0 && layers[Blue] > 0 && layers[Red] == 0;
}

int canCount, opCount;
vector<Mix> mixes;

void readData() {
    int from, to, color;
    cin >> canCount >> opCount;
    
    for (int i=0; i < opCount; i++) {
        cin >> from >> to >> color;
        mixes.push_back(Mix{from, color, Add});
        mixes.push_back(Mix{to+1, color, Remove});
    }
}

void solve() {
    Layers layers = {};
    int prevCan = 0;
    int result = 0;
    
    sort(mixes.begin(), mixes.end());
    
    for (Mix& mix : mixes) {
        int can = mix.can;
        
        if (can != prevCan && isGreen(layers)) {
            result += can - prevCan;
        }
        
        layers[mix.color] += (mix.op == Add ? +1 : -1);
        prevCan = can;
    }
    
    cout << result;
}


int main() {
    ios_base::sync_with_stdio(false);
    
    readData();
    solve();
    
    return 0;
}