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
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;


#define BIGMOD 1000012177LL

#define MAX_N 1000010
unsigned char colorMix[MAX_N];


void process_color(const vector<pair<int, int> >& v, unsigned char mask, int n)
{
    int ci = -1;
    for (auto pr : v)
    {
        int left = pr.first;
        int right = pr.second;
        if (left >= ci)
        {
            ci = left;
        }
        if (right >= ci)
        {
            while (ci <= right)
            {
                colorMix[ci] |= mask;
                ci++;
            }
        }
    }
}

int main() {
    for (int i=0; i < MAX_N; i++)
    {
        colorMix[i] = 0;
    }
    int n,m;
    scanf("%d%d", &n, &m);
    vector<pair<int, int> > intervals[3];
    REP(i,m)
    {
        int left, right, k;
        scanf("%d%d%d", &left, &right, &k);
        intervals[k-1].emplace_back(make_pair(left-1, right-1));
    }
    REP(i,3)
    {
        sort(intervals[i].begin(), intervals[i].end());
        process_color(intervals[i], 1 << i, n);
    }
    int answer = 0;
    REP(i, n)
    {
        //printf("i=%u\n", colorMix[i]);
        if (colorMix[i] == 3)
        {
            ++answer;
        }
    }
    printf("%d\n", answer);
    return 0;
}