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
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;

struct Point{
    int coordinate;
    int color;
    bool begin;


};

bool compareByCoordinate(const Point &a, const Point &b)
{
    if(a.coordinate != b.coordinate)
        return a.coordinate <= b.coordinate;
    else
        return a.begin > b.begin;
}

int main() {
    ll n, m, color, begin, end;
    cin >> n >> m;
    vector<Point> points;
    vector<ll> colors;
    colors.push_back(0);
    colors.push_back(0);
    colors.push_back(0);
    int ll result = 0;
    while(m--)
    {
        cin >> begin >> end >> color;
        Point point1, point2;
        point1.coordinate = begin;
        point1.color = color;
        point1.begin = true;

        point2.coordinate = end;
        point2.color = color;
        point2.begin = false;

        points.push_back(point1);
        points.push_back(point2);
    }
    sort(points.begin(), points.end(), compareByCoordinate);


   int i = 0;
   if(points.size() == 1){
       cout << 0;
       return 0;
   }else{
       if(points[i].color == 1)
           colors[0]++;
       else if(points[i].color == 2)
           colors[1]++;
       else
           colors[2]++;
   }

   i = 0;

   while(i + 1 < points.size())
   {
       bool moved = false;
       do {
           if(points[i + 1].begin)
           {
               if (points[i + 1].color == 1)
                   colors[0]++;
               else if (points[i + 1].color == 2)
                   colors[1]++;
               else
                   colors[2]++;
               i++;
               moved = true;
           }
       }while(i + 1 < points.size() && points[i + 1].begin && points[i+1].coordinate == points[i].coordinate);

       if(colors[0] >= 1 && colors[1] >= 1 && colors[2] == 0)
       {
           result++;
       }

       do {
           if(!points[i + 1].begin)
           {
               if (points[i + 1].color == 1)
                   colors[0]--;
               else if (points[i + 1].color == 2)
                   colors[1]--;
               else
                   colors[2]--;
               i++;

               moved = true;
           }
       } while(i + 1< points.size() && !points[i + 1].begin && points[i+1].coordinate == points[i].coordinate);
       if(!moved)
           i++;
   }
   cout << result;


}