from functools import total_ordering @total_ordering class OrderTime: def __init__(self, day: int, hour: int, minute: int) -> None: self.hour = hour self.minute = minute self.day = day @property def time_tuple(self) -> tuple[int, int, int]: return self.day, self.hour, self.minute def __eq__(self, __o: object) -> bool: if not isinstance(__o, OrderTime): raise ValueError return self.time_tuple == __o.time_tuple def __lt__(self, __o: object) -> bool: if not isinstance(__o, OrderTime): raise ValueError return self.time_tuple < __o.time_tuple def __str__(self) -> str: return f"{self.day} {self.hour}:{self.minute}" class Order: def __init__(self, enter_day, enter_hour, enter_minute, out_day, out_hour, out_minute) -> None: self.enter_time = OrderTime(enter_day, enter_hour, enter_minute) self.out_time = OrderTime(out_day, out_hour, out_minute) def can_stay(self, _o: object) -> bool: if not isinstance(_o, Order): raise ValueError return self.out_time < _o.enter_time def main(): n, m = map(int, input().split()) orders: list[Order] = [] for _ in range(m): enter_day, enter_time, out_day, out_time = input().split() enter_day = int(enter_day) out_day = int(out_day) enter_hour, enter_minute = map(int, enter_time.split(":")) out_hour, out_minute = map(int, out_time.split(":")) orders.append(Order(enter_day, enter_hour, enter_minute, out_day, out_hour, out_minute)) orders.sort(key=lambda order: order.out_time) ctr = 0 rooms = [Order(1, 23, 50, 1, 23, 59) for _ in range(n)] for order in orders: next_order = [-1, (-1, -1, -1)] for room_idx, room in enumerate(rooms): if room.can_stay(order) and next_order[1] < room.out_time.time_tuple: next_order = [room_idx, room.out_time.time_tuple] if next_order[0] == -1: continue rooms[next_order[0]] = order ctr += 1 print(ctr) if __name__ == "__main__": main()