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
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
using namespace std;
void input(int &m, vector<vector<int>> &first, vector<vector<int>> &last) {
  int l, r, k;
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> k;
    first[k][l]++;
    last[k][r]++;
  }
}
int solve(int &n, vector<vector<int>> &first, vector<vector<int>> &last) {
  int result = 0, arr_k[4] = {};
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= 3; k++) arr_k[k] += first[k][i];
    if (arr_k[1] > 0 && arr_k[2] > 0 && arr_k[3] == 0) result++;
    for (int k = 1; k <= 3; k++) arr_k[k] -= last[k][i];
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> first(4, vector<int>(n + 1, 0)), last(4, vector<int>(n + 1, 0));
  input(m, first, last);
  cout << solve(n, first, last) << "\n";
  return 0;
}