1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#include <vector>

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  std::vector<int> y(n + 1), b(n + 1), r(n + 1);
  for (int i = 0; i < m; ++i) {
    int u, v, k;
    scanf("%d%d%d", &u, &v, &k);
    if (k == 1) y[u - 1]++, y[v]--;
    if (k == 2) b[u - 1]++, b[v]--;
    if (k == 3) r[u - 1]++, r[v]--;
  }
  int ret = 0;
  for (int i = 0; i < n; ++i) {
    if (i) y[i] += y[i - 1];
    if (i) b[i] += b[i - 1];
    if (i) r[i] += r[i - 1];
    ret += y[i] && b[i] && !r[i];
  }
  printf("%d\n", ret);
  return 0;
}