def gcd(m, n)
    if m < n 
        gcd(n, m)
    elsif n == 0
        m   
    else
        gcd(n, m % n)
    end 
end

def lcm(m, n)
    m * n / gcd(m, n)
end

require 'prime'

CYCLE = (1..24).reject {|x| Prime.prime?(x)}.inject {|r, x| lcm(r, x)} 

loop do
    n = gets.to_i
    break if n == 0
    cycles = {CYCLE => [0] * CYCLE}
    n.times do
        d, t, *qs = gets.split.map(&:to_i)
        qs.rotate!(-d)
        if Prime.prime?(d) && d * 2 > 24
            if cycles[d]
                cycles[d] = cycles[d].zip(qs).map {|x, y| x + y}
            else
                cycles[d] = qs
            end 
        else
            qs *= CYCLE / qs.size
            cycles[CYCLE] = cycles[CYCLE].zip(qs).map {|x, y| x + y}
        end 
    end 
    p cycles.values.map(&:max).inject(:+)
end