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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

void readMix(int mixCount, vector<vector<pair<int, int>>>& colorSection) {
	for (int i = 0; i < mixCount; ++i) {
		int start, stop, color;
		cin >> start;
		cin >> stop;
		cin >> color;
		colorSection[color].push_back(pair<int, int>(start, stop));
	}
}


int redIntersection(int greenStart, int greenEnd, int redStart, int redEnd) {
	// cout << "green: " << greenStart << "->" << greenEnd << endl;

	int start = max(greenStart, redStart);
	int end = min(greenEnd, redEnd);

	int length = 0;
	if (start <= end) {
		length = end - start + 1;
	}
	// cout << "red intersection of length " << length << endl;
	return length;
}

bool moveIndexes(vector<vector<pair<int, int>>>& colorSection,
	int& yellow, int& blue, int& red, bool stillRed) {

	//yellow finishes first
	if (stillRed) {
		if (colorSection[1][yellow].second <= colorSection[2][blue].second
			&& colorSection[1][yellow].second <= colorSection[3][red].second) {
			yellow++;
			return false;
		}
	} else {
		if (colorSection[1][yellow].second <= colorSection[2][blue].second) {
			yellow++;
			return false;
		}		
	}

	//blue finishes first
	if (stillRed) {
		if (colorSection[2][blue].second <= colorSection[1][yellow].second
			&& colorSection[2][blue].second <= colorSection[3][red].second) {
			blue++;
			return false;
		}
	} else {
		if (colorSection[2][blue].second <= colorSection[1][yellow].second) {
			blue++;
			return false;
		}
	}

	if (stillRed) {
		//red finishes first
		if (colorSection[3][red].second <= colorSection[1][yellow].second
			&& colorSection[3][red].second <= colorSection[2][blue].second) {
			red++;
			return true;
		}
	}

	return false;
}

void mergeSection(vector<vector<pair<int, int>>>& colorSection, int color,
				  int canCount) {
	sort(colorSection[color].begin(), colorSection[color].end());

	int lastValidSection = 0;
	for (int j = 1; j < colorSection[color].size(); ++j) {
		if (colorSection[color][lastValidSection].second >= colorSection[color][j].first
			|| colorSection[color][lastValidSection].first == colorSection[color][j].first) {
			colorSection[color][lastValidSection].second = colorSection[color][j].second;
			colorSection[color][j].first = canCount + 1;
			colorSection[color][j].second = canCount + 1;
		} else {
			lastValidSection = j;
		}
	}

	sort(colorSection[color].begin(), colorSection[color].end());
	
	while (colorSection[color].size() > 0 && prev(colorSection[color].end())->first == canCount + 1) {
		colorSection[color].erase(prev(colorSection[color].end()));
	}
}

int countGreen(vector<vector<pair<int, int>>>& colorSection, int canCount) {
	int count = 0;
	for (int i = 1; i < colorSection.size(); ++i) {
		mergeSection(colorSection, i, canCount);
	}

	int yellow, blue, red;
	yellow = 0;
	blue = 0;
	red = 0;

	bool stillRed = (colorSection[3].size() > 0);
	bool redMoved = false;

	while (yellow < colorSection[1].size() && blue < colorSection[2].size()) {
		// cout << endl;
		// cout << "Considering:" << endl;
		// cout << "yellow: " << colorSection[1][yellow].first << "->"
			//			   << colorSection[1][yellow].second << endl;

		// cout << "blue: " << colorSection[2][blue].first << "->"
//				   << colorSection[2][blue].second << endl;

		// cout << "red: " << colorSection[3][red].first << "->"
//				   << colorSection[3][red].second << endl;				   

		int greenPotentialStart = max(colorSection[1][yellow].first,
									  colorSection[2][blue].first);
		int greenPotentialEnd = min(colorSection[1][yellow].second,
									colorSection[2][blue].second);
		
		int greenLength = 0;
		if (!redMoved) {
			greenLength = greenPotentialEnd - greenPotentialStart + 1;
			if (greenLength < 0)
				greenLength = 0;
		}
		else
			redMoved = false;

		// cout << "blue and yellow give green of length " << greenLength << endl;
		if (stillRed) {
			if (greenLength > 0)
				greenLength -= redIntersection(greenPotentialStart, greenPotentialEnd,
							colorSection[3][red].first, colorSection[3][red].second);
		}

		count += greenLength;
		// cout << "Adding " << greenLength << endl;
		redMoved = moveIndexes(colorSection, yellow, blue, red, stillRed);

		if (red >= colorSection[3].size()) {
			stillRed = false;
		}
	}


	return count;
}

int main() {
	int canCount, mixCount;
	cin >> canCount;
	cin >> mixCount;
	vector<vector<pair<int, int>>> colorSection(4);

	readMix(mixCount, colorSection);
	cout << countGreen(colorSection, canCount) << endl;
	return 0;
}