### subroutines

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

def intersection(ts0, ts1)
  i0 = i1 = 0
  nts0 = ts0.length
  nts1 = ts1.length

  tsi = []

  loop do
    while i0 < nts0 && ts0[i0][1] <= ts1[i1][0]
      i0 += 1
    end
    break if i0 >= nts0

    while i1 < nts1 && ts1[i1][1] <= ts0[i0][0]
      i1 += 1
    end
    break if i1 >= nts1

    if ts0[i0][0] < ts1[i1][1] && ts1[i1][0] < ts0[i0][1]
      p0 = [ts0[i0][0], ts1[i1][0]].max
      p1 = [ts0[i0][1], ts1[i1][1]].min
      tsi << [p0, p1] if p0 < p1
      if ts0[i0][1] < ts1[i1][1]
        i0 += 1
        break if i0 >= nts0
      else
        i1 += 1
        break if i1 >= nts1
      end
    end
  end

  tsi
end

### main

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

  cmks = []
  cms = []

  n.times do
    k = gets.strip.to_i
    cmks << k

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

  its = [[hm0, hm1]]

  for i in (0...n)
    pits = its
    its = intersection(pits, cms[i])
  end

  pgs = [hm0] + its.flatten + [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