from bisect import bisect_left n, m = map(int, input().split()) T = [] for _ in range(m): fd, ft, td, tt = input().split() fd = int(fd) - 1 fh, fm = map(int, ft.split(':')) td = int(td) - 1 th, tm = map(int, tt.split(':')) f = fd * 24 * 60 + fh * 60 + fm t = td * 24 * 60 + th * 60 + tm T.append((f, t)) T.sort(key=lambda x: (x[1], x[0])) ans = 0 end_times = [] for left, right in T: idx = bisect_left(end_times, left) if idx > 0: del end_times[idx - 1] end_times.append(right) ans += 1 elif len(end_times) < n: end_times.append(right) ans += 1 print(ans)