def solve(n, m, guestdata): schedulenote = {(d,h,m) : 0 for d in range(2, 9) for h in range(0, 24) for m in range(0, 60)} schedulelist = [(d,h,m) for d in range(2, 9) for h in range(0, 24) for m in range(0, 60)] totalcount = 0 guestdata.sort(key=lambda x: x.split(" ")[2:]) for guest in guestdata: d = guest.split(" ") checkin_day = int(d[0]) checkin_h = int(d[1][:2]) checkin_m = int(d[1][3:]) checkout_day = int(d[2]) checkout_h = int(d[3][:2]) checkout_m = int(d[3][3:]) checkin = (checkin_day, checkin_h, checkin_m) checkout = (checkout_day, checkout_h, checkout_m) available = True target_schedule = schedulelist[schedulelist.index(checkin):schedulelist.index(checkout) + 1] for schedule in target_schedule: if schedulenote[schedule] >= n: available = False break if available: totalcount += 1 for schedule in target_schedule: schedulenote[schedule] += 1 return totalcount if __name__ == "__main__": l = input().split(" ") n, m = int(l[0]), int(l[1]) data = [input() for _ in range(0, m)] print(solve(n, m, data))