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
#include <iostream>
#include <vector>

#define YELLOW 1
#define BLUE 2
#define RED 3

std::vector<std::pair<int, int>> yellow_ranges;
std::vector<std::pair<int, int>> blue_ranges;
std::vector<std::pair<int, int>> red_ranges;

bool is_in_range(std::pair<int, int> range, int num) {
    return num >= range.first && num <= range.second;
}

void insert_range(std::vector<std::pair<int, int>>& vec, std::pair<int, int> range) {
    bool inserted_range = false;
    for(auto &x: vec) {
        if(is_in_range(range, x.first) && is_in_range(range, x.second)) {
            x = range;
            inserted_range = true;
            break;
        }
        else if(is_in_range(range, x.first) && !is_in_range(range, x.second)) {
            x.first = range.first;
            inserted_range = true;
            break;
        }
        else if(!is_in_range(range, x.first) && is_in_range(range, x.second)) {
            x.second = range.second;
            inserted_range = true;
            break;
        }

        if(!is_in_range(x, range.first) && !is_in_range(x, range.second)) {
            continue;
        }
        else if(is_in_range(x, range.first) && !is_in_range(x, range.second)) {
            x.second = range.second;
            inserted_range = true;
            break;
        }
        else if(!is_in_range(x, range.first) && is_in_range(x, range.second)) {
            x.first = range.first;
            inserted_range = true;
            break;
        }
        else {
            inserted_range = true;
            break;
        }
    }
    if(!inserted_range) {
        vec.push_back(range);
    }
}

int main() {
    std::iostream::sync_with_stdio(false);
    int n, m;
    std::cin >> n >> m;

    while(m--) {
        int low, high, paint;
        std::cin >> low >> high >> paint;

        auto pair = std::make_pair(low, high);
        switch(paint) {
            case YELLOW: {
                insert_range(yellow_ranges, pair);
                break;
            }
            case BLUE: {
                insert_range(blue_ranges, pair);
                break;
            }
            case RED: {
                insert_range(red_ranges, pair);
                break;
            }
        }  
    }

    int count = 0;
    for(int i = 1; i <= n; i++) {
        bool is_yellow = false;
        bool is_blue = false;
        bool is_red = false;
        // Skip red
        for(auto &x: red_ranges) {
            is_red = is_in_range(x, i);
            if(is_red) break;
        }
        if(is_red) continue;

        for(auto &x: yellow_ranges) {
            is_yellow = is_in_range(x, i);
            if(is_yellow) break;
        }
        for(auto &x: blue_ranges) {
            is_blue = is_in_range(x, i);
            if(is_blue) break;
        }
        
        if(is_yellow && is_blue) count++;
    }
    std::cout << count << std::endl;
}