### main

loop do
  gn, dur = gets.split.map(&:to_i)
  break if (gn | dur) == 0

  nts = []
  tsis = []

  gn.times do
    tsi = []
    loop do
      ds = gets.split.map(&:to_i)
      tsi += ds
      if tsi.last == 0
        tsi.pop
        break
      end
    end
    nts << tsi.length
    tsis << tsi
  end
  gets
  #p nts
  #p tsis

  st = gn.times.map{:consuming}
  rem = gn.times.map{|i| tsis[i].first}
  ti = gn.times.map{0}
  
  chgr = -1
  q = []
  wt = 0

  dur.times do
    for i in (0...gn)
      case st[i]
      when :consuming
        rem[i] -= 1
        if rem[i] == 0
          st[i] = :queued
          q << i
        end
      when :queued
        wt += 1
      when :charging
        rem[i] -= 1
        if rem[i] == 0
          st[i] = :consuming
          ti[i] = (ti[i] + 1) % nts[i]
          rem[i] = tsis[i][ti[i]]
          chgr = -1
        end
      end
    end

    if chgr < 0 && ! q.empty?
      i = q.shift
      st[i] = :charging
      ti[i] = (ti[i] + 1) % nts[i]
      rem[i] = tsis[i][ti[i]]
      chgr = i
    end
  end

  puts wt
end