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

using namespace std;

constexpr size_t MAX_N = 1000 * 1000;
constexpr size_t MAX_M = 1000 * 1000;
constexpr size_t COLORS = 3;

constexpr size_t YELLOW = 0,
                 BLUE = 1,
                 RED = 2;

int colors_inc[COLORS][MAX_N + 1];

int main() {
  ios_base::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int l, r, k;
    cin >> l >> r >> k;
    colors_inc[k - 1][l - 1]++;
    colors_inc[k - 1][r]--;
  }

  int colors_cnt[COLORS] = {0};
  int result = 0;
  for (int i = 0; i < n; ++i) {
    colors_cnt[YELLOW] += colors_inc[YELLOW][i];
    colors_cnt[BLUE] += colors_inc[BLUE][i];
    colors_cnt[RED] += colors_inc[RED][i];

    if (colors_cnt[YELLOW] > 0 &&
        colors_cnt[BLUE] > 0 &&
        colors_cnt[RED] == 0) {
      result += 1;
    }
  }

  cout << result << endl;

  return 0;
}