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

#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)

template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}

#define _upgrade                \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)

// ********************** CODE ********************** //

int main() {
  _upgrade;
  int n, q;
  cin >> n >> q;
  vector<vector<int>> V(n + 7, vector<int>(4));
  while (q--) {
    int l, r, val;
    cin >> l >> r >> val;
    V[l][val]++;
    V[r + 1][val]--;
  }

  int res = 0;
  int yellow = 0;
  int blue = 0;
  int red = 0;
  for (int i = 1; i <= n; i++) {
    yellow += V[i][1];
    blue += V[i][2];
    red += V[i][3];

    if (yellow > 0 && blue > 0 && red == 0) {
      res++;
    }
  }
  cout << res << "\n";
  return 0;
}