#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N, M; cin >> N >> M; vector L(M), R(M); REP(i, M) { int d[2], h[2], m[2]; scanf("%d %d:%d %d %d:%d", d, h, m, d + 1, h + 1, m + 1); auto conv = [](int d, int h, int m) { return (d - 2) * 24 * 60 + h * 60 + m; }; L[i] = conv(d[0], h[0], m[0]); R[i] = conv(d[1], h[1], m[1]) + 1; } vector ord(M); iota(ALL(ord), 0); sort(ALL(ord), [&](int i, int j) { return L[i] < L[j]; }); multiset ms; // 最小の R REP(i, N) ms.insert(INF); int ans = 0; for (int i : ord) { while (!ms.empty() && *ms.begin() <= L[i]) { ms.erase(ms.begin()); ms.insert(INF); ++ans; } if (*--ms.end() > R[i]) { ms.erase(--ms.end()); ms.insert(R[i]); } } while (!ms.empty() && *ms.begin() != INF) { ms.erase(ms.begin()); ++ans; } cout << ans << endl; }