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
#include<cstdio>
#include<utility>
#include<vector>
#include<algorithm>

typedef std::pair<int, int> pii;
typedef std::vector<pii> v;

int n, m;
v yellow_events;
v blue_events;
v red_events;

void input() {
  scanf("%d %d", &n, &m);
  int l, r, k;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &l, &r, &k);
    pii p = std::make_pair(l, r);
    if (k == 1) {
      yellow_events.push_back(p);
    } else if (k == 2) {
      blue_events.push_back(p);
    } else {
      red_events.push_back(p);
    }
  }
}

void print_v(v vec) {
  printf("%lu elements\n", vec.size());
  for (v::iterator it = vec.begin(); it != vec.end(); ++it) {
    printf("%d %d\n", it -> first, it -> second);
  }
}

bool cmp_p(pii & p1, pii & p2) {
  if (p1.first < p2.first) {
    return true;
  } else if (p1.first == p2.first) {
    return p1.second < p2.second;
  } else {
    return false;
  }
}

void sort_v() {
  std::sort(yellow_events.begin(), yellow_events.end(), cmp_p);
  std::sort(blue_events.begin(), blue_events.end(), cmp_p);
  std::sort(red_events.begin(), red_events.end(), cmp_p);
}

void print_p(v::iterator & p, v & vec) {
  if (p == vec.end()) {
    printf("NULL\n");
  } else {
    printf("(%d %d) ", p -> first, p -> second);
  }
}

v::iterator forward(v::iterator & current, v & vec, int i) {
  while (current != vec.end() && i > current -> second) {
    ++current;
  }
  // if (current == vec.end()) {
  //   printf("END forward\n");
  // }
  return current;
}

bool marked(v::iterator & current, v & vec, int i) {
  if (current == vec.end()) {
    return false;
  }
  return current -> first <= i && current -> second >= i;
}

int count() {
  v::iterator yellow = yellow_events.begin();
  v::iterator blue = blue_events.begin();
  v::iterator red = red_events.begin();

  int result = 0;

  for (int i = 1; i <= n; i++) {
    yellow = forward(yellow, yellow_events, i);
    blue = forward(blue, blue_events, i);
    red = forward(red, red_events, i);

    // printf("i = %d ", i);
    // print_p(yellow, yellow_events);
    // print_p(blue, blue_events);
    // print_p(red, red_events);
    // printf("\n");

    if (marked(yellow, yellow_events, i) && marked(blue, blue_events, i) && !marked(red, red_events, i)) {
      ++result;
    }
  }

  return result;
}

int main() {
  input();
  sort_v();

  // printf("yellow\n");
  // print_v(yellow_events);
  // printf("\nblue\n");
  // print_v(blue_events);
  // printf("\nred\n");
  // print_v(red_events);

  printf("%d\n", count());

  return 0;
}