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

using namespace std;

struct paint_compare {

  bool operator()(const pair<int, int>& a, const pair<int,int>& b) {
    return a.first < b.first; 
  }
};


int main(){
  int n, m;

  cin >> n >> m;
  
  std::vector<pair<int, int>> points;
  for(int i = 0; i < m; i ++){
    int start, end, color;
    cin >> start >> end >> color;
    points.push_back({start - 1, color});
    points.push_back({end, -color});
  }

  points.push_back({n, 0});
  sort(points.begin(), points.end(), paint_compare());
  
  std::map<int, int> counts{};
  int last = 0;
  long long ret = 0;
  for(auto a : points){
    if(counts[1] > 0 and counts[2] > 0 and counts[3] == 0){
      ret += a.first - last;
    }

    counts[abs(a.second)] += a.second > 0 ? 1 : -1;

    last = a.first;
  }
  cout << ret << std::endl;

}