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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <bits/stdc++.h>

using namespace std;

const long long M = 262144;
int n;
long long tre1[2 * M + 100];
long long tre2[2 * M + 100];
long long tre3[2 * M + 100];


void add_rekur1(int x, int v, int a, int b, int l, int p) {

  if (b < l or p < a)
    return;


  if (a <= l and p <= b) {
    tre1[x] += v;
    return;
  }

  add_rekur1(x * 2, v, a, b, l, (l + p) / 2);
  add_rekur1(x * 2 + 1, v, a, b, (l + p) / 2 + 1, p);
}



long long czytaj1(int x) {
  x += M;
  long long wynik = 0;
  while (x != 0) {
    wynik += tre1[x];
    x /= 2;
  }
  return wynik;
}

void add_rekur2(int x, int v, int a, int b, int l, int p) {

  if (b < l or p < a)
    return;


  if (a <= l and p <= b) {
    tre2[x] += v;
    return;
  }

  add_rekur2(x * 2, v, a, b, l, (l + p) / 2);
  add_rekur2(x * 2 + 1, v, a, b, (l + p) / 2 + 1, p);
}

void add_rekur3(int x, int v, int a, int b, int l, int p) {

  if (b < l or p < a)
    return;


  if (a <= l and p <= b) {
    tre3[x] += v;
    return;
  }

  add_rekur3(x * 2, v, a, b, l, (l + p) / 2);
  add_rekur3(x * 2 + 1, v, a, b, (l + p) / 2 + 1, p);
}



long long czytaj3(int x) {
  x += M;
  long long wynik = 0;
  while (x != 0) {
    wynik += tre3[x];
    x /= 2;
  }
  return wynik;
}

long long czytaj2(int x) {
  x += M;
  long long wynik = 0;
  while (x != 0) {
    wynik += tre2[x];
    x /= 2;
  }
  return wynik;
}
 int main(){
 ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int m,n,a,b,x;
    cin>>m>>n;
    for (int i=0;i<n;i++){

            cin>>a>>b;
            cin>>x;
        if (x==1){
            add_rekur1(1, 1, a, b, 1, M - 1);
        }
        if (x==2){
            add_rekur2(1, 1, a, b, 1, M - 1);
        }
        if (x==3){
            add_rekur3(1, 1, a, b, 1, M - 1);
        }

    }
    int w=0;
    for (int i=0;i<m;i++){
            if (czytaj1(i)>0&&czytaj2(i)>0&&czytaj3(i)==0){
                w++;
            }

 }
 cout<<w;
 }