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
#include <bits/stdc++.h>
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define debug(x) cerr << #x << " " << x << endl
#define ll long long
#define st first
#define nd second
const int N = 1e6 + 13;
using namespace std;

int yellow[N], blue[N], red[N];
int n, m, l, r, k, res;
int main() {
  boost;

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> l >> r >> k;
    if (k == 1) {
      yellow[l]++;
      yellow[r + 1]--;
    } else if (k == 2) {
      blue[l]++;
      blue[r + 1]--;
    } else {
      red[l]++;
      red[r + 1]--;
    }
  }

  for (int i = 1; i <= n; i++) {
    yellow[i] += yellow[i - 1];
    blue[i] += blue[i - 1];
    red[i] += red[i - 1];
    res += (yellow[i] && blue[i] && !red[i]);
  }

  cout << res << endl;
}