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
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (b); i >= (a); --i)
#define TRAV(x,T) for(auto& (x): (T))
#define ALL(T) T.begin(), T.end()
#define TAB(T,a,b) (T)+a, (T)+((b)+1)
#define VAR(x) #x<<" = "<<x<<" " 
#define sz(x) (int)(x).size()
#define nwd __gcd
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define deb if(0)
const int N = 1e6, NT = N + 2;
const int ZIELONY = 3;

int T[4*N+2];
int path[102], idx = 1;

void update(int v, int tl, int tr, int l, int r, int val) {
    if(l <= tl and r >= tr) {
        T[v] |= val;
        return;
    }
    int tm = tl + (tr - tl) / 2;
    if(l <= tm) update(v*2, tl, tm, l, r, val);
    if(r > tm) update(v*2+1, tm + 1, tr, l, r, val);
}

void query_util(int v, int tl, int tr, int p) {
    path[idx++] = v;
    if(p == tl and p == tr) {
        return;
    }
    int tm = tl + (tr - tl) / 2;
    if(p <= tm) query_util(v*2, tl, tm, p);
    else query_util(v*2+1, tm+1, tr, p);
}

bool query(int n, int p) {
    query_util(1, 1, n, p);
    int ans = 0;
    FORD(i, 1, idx - 1) {
        ans |= T[path[i]];
        path[i] = 0;
    }
    idx = 1;
    return (ans == ZIELONY);
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, l, r, k;
    cin>>n>>m;
    FOR(i, 1, m) {
        cin>>l>>r>>k;
        if(k == 3) k = 4;
        update(1, 1, n, l, r, k);
    }
    int ans = 0;
    FOR(i, 1, n) {
        if(query(n, i)) ++ans;
    }
    cout<<ans<<"\n";
    return 0;
}