#include using namespace std; using ll = long long; using ul = unsigned long; using ull = unsigned long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector > v(m); for (auto&& it : v) { string e[4]; for (auto&& it2 : e) cin >> it2; it.first = stoi(e[0]) * 24 * 60 + stoi(e[1].substr(0, 2)) * 60 + stoi(e[1].substr(3, 2)); it.second = stoi(e[2]) * 24 * 60 + stoi(e[3].substr(0, 2)) * 60 + stoi(e[3].substr(3, 2)); } sort(v.begin(), v.end(), [](auto const& lhs, auto const& rhs) { return lhs.first < rhs.first || ((lhs.first == rhs.first) && (lhs.second < rhs.second)); }); int res{ 0 }; multiset co; for (const auto& it : v) { for (auto itco = co.begin(); itco != co.end();) { if (*itco < it.first) itco = co.erase(itco); else if (*itco >= it.first) break; } if ((int)co.size() == n) { auto itco = --co.end(); if (*itco > it.second) { co.erase(itco); co.insert(it.second); } } else if ((int)co.size() < n) { co.insert(it.second); ++res; } } cout << res << "\n"; return 0; }