### subroutines

def hm2m(hm)
  (hm / 100) * 60 + (hm % 100)
end

### main

loop do
  n, hm0, hm1 = gets.strip.split(' ').map(&:to_i)
  break if (n | hm0 | hm1) == 0

  m0 = hm2m(hm0)
  m1 = hm2m(hm1)

  nts = m1 - m0
  ts = nts.times.map{false}

  n.times do
    k = gets.strip.to_i

    tmps = gets.strip.split(' ').map{|s| hm2m(s.to_i)}
    cms = (0...k).map{|i| [tmps[2 * i], tmps[2 * i + 1]]}.sort

    st = 0
    for tm in (cms + [[m1, m1]])
      et = tm[0] - m0
      for t in (st...et)
        ts[t] = true
      end
      st = tm[1] - m0
    end
  end
  #p ts

  max_m = 0
  prvi = -1

  for i in (0...nts)
    if ts[i]
      prvi = i if prvi < 0
    else
      if prvi >= 0
        m = i - prvi
        max_m = m if max_m < m
        prvi = -1
      end
    end
  end

  if prvi >= 0
    m = nts - prvi
    max_m = m if max_m < m
  end

  puts max_m
end