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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;

struct Color {
  int cnt[3] = {};

  void operator+=(const Color &other) {
    REP(i,3) cnt[i] += other.cnt[i];
  }

  bool is_green() const {
    return cnt[0] > 0 && cnt[1] > 0 && cnt[2] == 0;
  }
};

class Cans {
public:
  void read();
  int count_green() const;
private:
  int num_cans = 0;
  vector<Color> deltas;
};

void Cans::read() {
  int num_ops;
  cin >> num_cans >> num_ops;
  deltas.resize(num_cans+1);
  REP(i, num_ops) {
    int a, b, k;
    cin >> a >> b >> k;
    --a; --k;
    deltas[a].cnt[k] += 1;
    deltas[b].cnt[k] -= 1;
  }
}

int Cans::count_green() const {
  Color color;
  int res = 0;
  REP(i, num_cans) {
    color += deltas[i];
    if (color.is_green()) res++;
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  Cans cans;
  cans.read();
  int res = cans.count_green();
  cout << res << "\n";
}