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 <bits/stdc++.h>
using namespace std;

const int MAXTREE = 1 << 20;

int tree[2 * MAXTREE];

void update(int l, int r, int b) {
  l += MAXTREE - 1;
  r += MAXTREE + 1;
  while (l / 2 != r / 2) {
    if (l % 2 == 0) tree[l + 1] |= b;
    if (r % 2 == 1) tree[r - 1] |= b;
    l /= 2;
    r /= 2;
  }
}

int query(int x) {
  int r = 0;
  x += MAXTREE;
  while (x != 0) {
    r |= tree[x];
    x /= 2;
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);

  int n, q;
  cin >> n >> q;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    update(l, r, 1 << (x-1));
  }

  int result = 0;
  for (int i = 1; i <= n; i++)
    if (query(i) == 3)
      result++;
  cout << result << "\n";

  return 0;
}