import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int m = Integer.parseInt(first[0]); int n = Integer.parseInt(first[1]); ArrayList list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(new Customer(i, br.readLine())); } Collections.sort(list, new Comparator() { public int compare(Customer c1, Customer c2) { if (c1.left == c2.left) { return c1.right - c2.right; } else { return c1.left - c2.left; } } }); TreeSet stays = new TreeSet<>(); int count = 0; for (Customer c : list) { while (stays.size() > 0 && stays.first().right < c.left) { stays.pollFirst(); } stays.add(c); if (stays.size() > m) { count++; stays.pollLast(); } } System.out.println(n - count); } static class Customer implements Comparable { int idx; int left; int right; public Customer(int idx, String line) { this.idx = idx; String[] args = line.split(" ", 4); left = Integer.parseInt(args[0]) * 24 * 60; String[] in = args[1].split(":", 2); left += Integer.parseInt(in[0]) * 60; left += Integer.parseInt(in[1]); right = Integer.parseInt(args[2]) * 24 * 60; String[] out = args[3].split(":", 2); right += Integer.parseInt(out[0]) * 60; right += Integer.parseInt(out[1]); } public int hashCode() { return idx; } public int compareTo(Customer another) { if (right == another.right) { return idx - another.idx; } else { return right - another.right; } } } }