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 <iostream>

#define N 1000007

using namespace std;

int add_color[N][4];
int remove_color[N][4];
int current_color[4];

void apply_add(int index) {
  for (int i = 1; i <= 3; i++) {
    current_color[i] += add_color[index][i];
  }
}

void apply_remove(int index) {
  for (int i = 1; i <= 3; i++) {
    current_color[i] -= remove_color[index][i];
  }
}

bool is_current_green() {
  return current_color[1] > 0
    && current_color[2] > 0
    && current_color[3] == 0;
}

int get_result(int n) {
  int result;
  for (int i = 1; i <= n; i++) {
    apply_add(i);
    result += is_current_green();
    apply_remove(i);
  }

  return result;
}

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int beg, end, color;
    cin >> beg >> end >> color;

    add_color[beg][color]++;
    remove_color[end][color]++;
  }

  cout << get_result(n);

  return 0;
}