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
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> pi;
vector<pi> v;
int X[4];
int sum = 0;

void update(int diff) {
  // printf("UPDATE: (%d) %d %d %d\n", diff, X[1], X[2], X[3]);
  if (X[1] > 0 && X[2] > 0 && X[3] == 0) {
    sum += diff;
  }
}

int main() {
  X[1] = X[2] = X[3] = 0;
  pi tmp;
  int a, b, c;
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    tmp.first = a;
    tmp.second = c;
    v.push_back(tmp);
    tmp.first = b + 1;
    tmp.second = -c;
    v.push_back(tmp);
  }
  sort(v.begin(), v.end());

  int last = 1;
  for (int i = 0; i < v.size(); i++) {
    int pos = v[i].first;
    int col = v[i].second;
    if (pos > last) {
      update(pos - last);
    }
    // printf("POS: %d, COL: %d\n", pos, col);
    if (col > 0)
      X[col]++;
    else
      X[-col]--;
    last = pos;
  }
  if (last < n + 1) {
    update(n + 1 - last);
  }
  printf("%d\n", sum);
}