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
#include<iostream>
#include<vector>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define MAXN 1000002

int n, m;
int st, en, k;

int main() {
    cin >> n >> m;
    vector<int> b[3];
    REP(i, 3) {
        REP(j, n + 2) {
            b[i].push_back(0);
        }
    }
    REP(i, m) {
        cin >> st >> en >> k;
        k--;
        b[k][st]++;
        b[k][en + 1]--;
    }
    int res = 0;
    FOR(j, 1, n + 1) {
        REP(i, 3) {
            b[i][j] += b[i][j - 1];
        }
        if (b[0][j] > 0 && b[1][j] > 0 && b[2][j] == 0) {
            res++;
        }
    }
    cout << res << endl;
}