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

struct segtree {
  vector<int> T;
  int n;

  segtree(const int &_n) : n(_n) {
    T.resize(2*n);
  }

  void update(int l, int r, int m) {
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l&1) T[l++] |= m;
      if (r&1) T[--r] |= m;
    }
  }

  int query(int p) {
    int res = 0;
    for (p += n; p > 0; p >>= 1)
      res |= T[p];
    return res;
  }
};

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

  int n, m; cin >> n >> m;
  segtree S(n);
  
  while (m--) {
    int l, r, k; cin >> l >> r >> k;
    --l;
    // update [l, r)
    S.update(l, r, 1 << (k-1));
  }

  int cnt = 0;
  for (int i = 1; i < n; ++i) {
    S.T[i << 1] |= S.T[i];
    S.T[i << 1 | 1] |= S.T[i];
  }

  for (int i = 0; i < n; ++i) {
    cnt += S.T[i+n] == 3;
  }

  cout << cnt;
}