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
#include <iostream>   
#include <string>
using namespace std;
int z[1000013];
int n[1000013];
int c[1000013];

void pre(int a)
{
    int s1 = 0;
    for (int i = 0; i < a; i++)
    {
        s1 = s1 + z[i];
      
        z[i] = s1;
      
        if (z[i] > 1) z[i] = 1;
       
    }
}
void pre1(int a)
{
    int s1 = 0;
   
    for (int i = 0; i < a; i++)
    {
        s1 = s1 + n[i];

        n[i] = s1;

        if (n[i] > 1) n[i] = 1;

    }
}
void pre2(int a)
{
    int s1 = 0;
   
    for (int i = 0; i < a; i++)
    {
        s1 = s1 + c[i];

        c[i] = s1;

        if (c[i] > 1) c[i] = 1;

    }
}

void solve()
{
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < b; i++)
    {
        int l, r, k;
        cin >> l >> r >> k;
        if (k == 1)
        {
            z[l-1]++;
            z[r]--;
        }
        if (k == 2)
        {
            n[l-1]++;
            n[r]--;
        }
        if (k == 3)
        {
            c[l-1]++;
            c[r]--;
        }
    }
   pre(a);
   pre1(a);
   pre2(a);
   int s=0;
   for (int i = 0; i < a; i++)
   {
       if (z[i] == 1 && n[i] == 1 && c[i] == 0) s++;
   }
   cout << s;
 
       

}
   

int main()
{   
    std::ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    solve();
    return 0;
}