N, M = gets.split.map(&:to_i) DAY = 24 * 60 def to_time(str) h, m = str.split(':').map(&:to_i) h * 60 + m end reserve_list = [] M.times do d, t1, o, t2 = gets.chomp.split from = d.to_i * DAY + to_time(t1) to = o.to_i * DAY + to_time(t2) reserve_list << [from, to] end reserve_list.sort_by! { |f, t| t } rooms = Array.new(N, -1) ans = 0 reserve_list.each do |from, to| best_id = -1 max_time = -Float::INFINITY N.times do |i| if rooms[i] < from && max_time < rooms[i] best_id = i max_time = rooms[i] end end if best_id != -1 rooms[best_id] = to ans += 1 end end puts ans