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
// solving problems by declaring parts of your life to be non-canon code
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, a, n) for (int i = a; i < (int)(n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= (int)(a); i--)
template <typename T, typename U>
ostream& operator<<(ostream& _s, pair<T, U> _t) {
    _s << "(" << _t.first << "," << _t.second << ")";
    return _s;
}
template <typename T, typename U>
istream& operator>>(istream& _s, pair<T, U>& _t) {
    _s >> _t.first >> _t.second;
    return _s;
}
template <typename T>
ostream& operator<<(ostream& _s, vector<T> _t) {
    rep(i, 0, _t.size()) _s << (i ? " " : "") << _t[i];
    return _s;
}
template <typename T>
istream& operator>>(istream& _s, vector<T>& _t) {
    rep(i, 0, _t.size()) _s >> _t[i];
    return _s;
}
template <typename T>
ostream& operator<<(ostream& _s, vector<vector<T>> _t) {
    rep(i, 0, _t.size()) { _s << i << ": " << _t[i] << endl; }
    return _s;
}

int main() {
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> c(n+1, vector<int>(3,0));

    rep(i,0,m) {
        int l, r, k;
        cin >> l >> r >> k;
        l--; r--; k--;
        c[l][k]++;
        c[r+1][k]--;
    }

    int res = 0;

    rep(i,0,n) {
        rep(j,0,3) {
            if (i > 0) {
                c[i][j] += c[i-1][j];
            }
            assert(c[i][j] >= 0);
        }

        if (c[i][0] > 0 && c[i][1] > 0 && c[i][2] == 0) {
            res++;
        }
    }

    cout << res << endl;
}