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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <bits/stdc++.h>
using namespace std;


const int N = 1e6 + 7;//atencion!!!


const int yellow_input = 1, blue_input = 2, rojo_input = 3;


const int yellow_sys = 2, blue_sys = 3, rojo_sys = 5;
const int verde_sys = yellow_sys * blue_sys; 
struct item{
    int color = 1;
    
    //yellow - 2
    //blue - 3
    //rojo - 5
    //Their combinations shall be their multiplication

    int start_y, start_b, start_r;
    int end_y, end_b, end_r;

    item(){
        color = 1;
        
        start_y = 0;
        start_b = 0;
        start_r = 0;

        end_y = 0;
        end_b = 0;
        end_r = 0;
    }
    void start_colour(short c){
        if (c == blue_input) start_b++;
        else if (c == yellow_input) start_y++;
        else if (c == rojo_input) start_r++;
    }
    void end_colour(short c){
        if (c == blue_input) end_b++;
        else if (c == yellow_input) end_y++;
        else if (c == rojo_input) end_r++;
    }
};
struct coef {
    int w = 1;

    bool has_red(){
        return (w % rojo_sys == 0);
    }
    bool has_yellow(){
        return (w % yellow_sys == 0);
    }
    bool has_blue(){
        return (w % blue_sys == 0);
    }
    void add_colour(short c){
        w *= c;
    }
    void remove_color(short c){
        if (c == yellow_sys){
            if (has_yellow()) w /= yellow_sys;
        } else if (c == blue_sys){
            if (has_blue()) w /= blue_sys;
        } else if (c == rojo_sys){
            if (has_red()) w /= rojo_sys;
        }
    }
};
item color[N];
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++){
        int l,r;
        short k;
        cin >> l >> r >> k;
        color[l].start_colour(k);
        color[r].end_colour(k);
    }


    //In each iteration:
    // 1. Start new colours, if appropriate
    // 2. Modify the array
    // 3. End coloring of sth, if appropriate

    coef w;
    int b = 0, r = 0, y = 0;
    for (int i = 1; i <= n;  i++){
        //1.
        b += color[i].start_b, r += color[i].start_r, y += color[i].start_y;

        

        //if new colour has arrived, remember to posess it
        if (color[i].start_b){
            if (!w.has_blue()) w.add_colour(blue_sys);
        } 
        if (color[i].start_r){
            if (!w.has_red()) w.add_colour(rojo_sys);
        }
        if (color[i].start_y){
            if (!w.has_yellow()) w.add_colour(yellow_sys);
        }
        //2.
        color[i].color = w.w;
        b -= color[i].end_b, r -= color[i].end_r, y -= color[i].end_y;
        
        
        //3.
        if (b == 0) w.remove_color(blue_sys);
        if (r == 0) w.remove_color(rojo_sys);
        if (y == 0) w.remove_color(yellow_sys);
    }

    int ans = 0;
    for (int i = 1; i <= n; i++){
        if (color[i].color == verde_sys) ans++;
    }
    cout << ans << "\n";


}