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
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <map>
#include <stdlib.h>
#include <vector>
#include <cstring>
#include <algorithm>  

using namespace std;




class Range {
    public:
     int start;
     int stop;
     bool inRange(int i ) {
         return i >= start && i <= stop;
     }

     bool isLessThan(int i ) {
         return i < start;
     }

     bool isGreaterThan(int i) {
         return i > stop;
     }

    bool operator < (const Range& struct2) const
    {
        if (start == struct2.start) return (stop > struct2.stop);
        return (start < struct2.start);
    }
};

struct less_than_key
{
    inline bool operator() (const Range& struct1, const Range& struct2)
    {
        return (struct1.start < struct2.start);
    }
};

bool isPainted(int i, int &vectorIndex, vector<Range> &vec) {
    if (vectorIndex >= vec.size()) return false;
    Range& current = vec[vectorIndex];

    while( current.isGreaterThan(i) ) {
        vectorIndex++;
        if (vectorIndex == vec.size()) break;
        current = vec[vectorIndex];
    }

    if (vectorIndex >= vec.size()) return false;
    if (current.isLessThan(i)) return false;
    if (current.inRange(i)) return true;
    return false;
}

int main() {
    int pusz;
	scanf("%d", &pusz);

    int mod;
	scanf("%d", &mod);

    vector<Range> red;
    red.reserve(1000001);

    vector<Range> yellow;
    yellow.reserve(1000001);

    vector<Range> blue;
    blue.reserve(1000001);

    int zolty = 1;
    int niebieski = 2;
    int czerwony = 4;


    for (int i = 0 ; i < mod; i++) {
        
        int l,r,k;
        Range tmp;
        scanf("%d %d %d", &tmp.start, &tmp.stop, &k);
        tmp.start--;
        tmp.stop--;
        if (k == 1) {
            yellow.push_back( tmp );
        } else if (k == 2) {
            blue.push_back(tmp);
        } else if (k == 3) {
            red.push_back(tmp);
        }
    }

    std::sort(red.begin(), red.end());
    std::sort(yellow.begin(), yellow.end());
    std::sort(blue.begin(), blue.end());

    int indexRed = 0;
    int indexYellow = 0;
    int indexBlue = 0;

    int zielonyKolor = 0;
    for (int i = 0 ; i < pusz  ; i++) {
        if(isPainted(i, indexRed, red) == false && isPainted(i, indexYellow, yellow)  && isPainted(i, indexBlue, blue) ) {
                zielonyKolor++;
        }
    }


    
    printf("%d", zielonyKolor);
    return 0;
}