import re import heapq N, M = map(int, input().split()) R = [] for _ in range(M): d_in, h_in, m_in, d_out, h_out, m_out = map(int, re.split('[ :]', input())) R.append([d_in * 1440 + h_in * 60 + m_in, d_out * 1440 + h_out * 60 + m_out]) R.sort(key=lambda x: x[1]) R.sort(key=lambda x: x[0]) pendings = [] count = 0 for r in R: heapq.heappush(pendings, (r[1], r[0])) while pendings[0][0] < r[0]: heapq.heappop(pendings) count += 1 if len(pendings) > N: pendings.sort() pendings.pop() heapq.heapify(pendings) count += len(pendings) print(count)