### constant

MAX_B = 100

### main

loop do
  m, c, n = gets.split.map(&:to_i)
  break if (m | c | n) == 0

  q = []
  
  n.times do
    ki = gets.to_i
    q << gets.split.map(&:to_i)
  end
  
  books = MAX_B.times.map{m}
  desks = m.times.map{[]} + [(1...MAX_B).to_a]

  csum = 0

  while ! q.empty?
    reqs = q.shift
    next if reqs.empty?

    req = reqs.shift
    q << reqs if ! reqs.empty?

    pos = books[req]
    desks[pos].delete(req)
    csum += pos + 1

    if desks[0].length < c
      desks[0] << req
      books[req] = 0
      csum += 1
    else
      rpos = (1...m).find{|i| desks[i].length < c} || m
      desks[rpos] << req
      books[req] = rpos
      csum += rpos + 1
      
      tbook = desks[0].shift
      csum += 1

      tpos = (1...m).find{|i| desks[i].length < c} || m
      desks[tpos] << tbook
      books[tbook] = tpos
      csum += tpos + 1
      
      desks[rpos].delete(req)
      csum += rpos + 1

      desks[0] << req
      books[req] = 0
      csum += 1
    end
  end

  p csum
end