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
#include "bits/stdc++.h"
using namespace std;
template <class A, class B> ostream& operator<<(ostream &os, pair<A, B> p) {
  return os << '{' << p.first << ", " << p.second << '}';
}
template <class T> ostream& operator<<(ostream &os, vector<T> v) {
  os << '{';
  for (T i : v)
    os << i << ", ";
  return os << '}';
}
#ifdef DEBUG
#define D(x...) x
#else
#define D(x...)
#endif
#define LN(x) D(cerr << #x << ": " << x << ' ')
#define LOG(x) D(cerr << #x << ": " << x << '\n')
#define MES(x) D(cerr << x << ' ')
#define MESL(x) D(cerr << x << '\n')
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
#define EB emplace_back
#define PB pop_back
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)

// Ignacy Boehlke

vvi rep;

int find (int x, vi& rep2) {
    if (x == rep2[x]) return x;
    return rep2[x] = find(rep2[x], rep2);
}

void unite(int a, int b, vi& rep2) {
    a = find(a, rep2);
    b = find(b, rep2);
    rep2[a] = b;
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);

    rep.resize(3, vi(n + 1));
    REP(i, 3) iota(rep[i].begin(), rep[i].end(), 0);
    vi col(n);

    int l, r, c;
    REP(i, m) {
        scanf("%d%d%d", &l, &r, &c); --l; --r; --c;
        l = find(l, rep[c]);
        for (; l <= r; l = find(l, rep[c])) {
            col[l] |= (1 << c);
            unite(l, l + 1, rep[c]);
        }
    }
    LOG(col);
    int res = 0;
    REP(i, n) res += (col[i] == 3);
    printf("%d\n", res);
}