## 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

  cmks = []
  cms = []

  n.times do
    cmks << gets.strip.to_i
    cms << gets.strip.split(' ').map(&:to_i)
  end

  its = [hm0, hm1]

  for i in (0...n)
    break if its.empty?

    pits = its
    its = []

    pit0 = pits.shift
    pit1 = pits.shift

    for j in (0...cmks[i])
      cm0, cm1 = cms[i][2 * j, 2]

      while pit1 <= cm0
        pit0 = pits.shift
        pit1 = pits.shift
        break if pit0.nil?
      end
      next if pit0.nil?

      p0 = [cm0, pit0].max
      p1 = [cm1, pit1].min
      its += [p0, p1] if p0 < p1
    end
  end

  pgs = [hm0] + its + [hm1]
  max_min = 0

  while ! pgs.empty?
    m0 = hm2m(pgs.shift)
    m1 = hm2m(pgs.shift)

    min = m1 - m0
    max_min = min if max_min < min
  end

  puts min
end