#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int main(){

    int n,m,c;
    while(cin>>m>>c>>n&&!(m==0&&c==0&&n==0)){
        vector<vector<int> > orders;
        vector<int> idxs;
        for(int i = 0; i < n; i++) idxs.push_back(0);
        queue<int> waiter;
        for(int i = 0; i < n; i++) waiter.push(i);
        for(int i = 0; i < n; i++){
            int k;
            cin>>k;
            vector<int> v;
            for(int j = 0; j < k; j++){
                int b;
                cin>>b;
                v.push_back(b);
            }
            orders.push_back(v);
        }
        // tables
        vector<vector<int> > tableAndShelf;
        for(int i = 0; i < m; i++)tableAndShelf.push_back(vector<int>());
        vector<int> shelf;
        for(int i = 0; i < 101; i++)
            shelf.push_back(i);
        tableAndShelf.push_back(shelf);
        int sumCost=0;
        while(waiter.size()){
            int member=waiter.front();
            int bookid=orders[member][idxs[member]];
            waiter.pop();
            // {ªÇ±É é©©Â¯é
            for(int i = 0; i < tableAndShelf.size(); i++){
                vector<int>::iterator it=find(tableAndShelf[i].begin(),tableAndShelf[i].end(),bookid);
                if(it!=tableAndShelf[i].end()){
                    tableAndShelf[i].erase(it);
                    sumCost+=i+1;
                    break;
                }
            }
            // u«êÉ·®u¯é©Åêí¯
            if(tableAndShelf[0].size()<c){
                tableAndShelf[0].push_back(bookid);
                sumCost+=1;
            }
            else{
                pair<int,int> tmp;
                // óÌIðTµÄêIÉ¨­
                for(int i = 0; i < tableAndShelf.size(); i++){
                    // ó©I
                    if(tableAndShelf[i].size()<c||i==tableAndShelf.size()-1){
                        tableAndShelf[i].push_back(bookid);
                        sumCost+=i+1;
                        tmp.first=i;
                        tmp.second=tableAndShelf[i].size()-1;
                        break;
                    }
                }
                // D1ÌãÌ{ÅAàÁÆà·¢ÔNGXg³êÄ¢È¢{ð©Â¯Aæè­
                int old=tableAndShelf[0][0];
                tableAndShelf[0].erase(tableAndShelf[0].begin());
                sumCost++;
                // D2È~ÅtÅÈ¢{IÉ¨­
                for(int i = 1; i < tableAndShelf.size(); i++){
                    // ó©I
                    if(tableAndShelf[i].size()<c||i==tableAndShelf.size()-1){
                        tableAndShelf[i].push_back(old);
                        sumCost+=i+1;
                        break;
                    }
                }
                // temp©çæèoµÄ«½{ðD1É¨­
                tableAndShelf[tmp.first].erase(tableAndShelf[tmp.first].begin()+tmp.second);
                sumCost+=tmp.first+1;
                tableAndShelf[0].push_back(bookid);
                sumCost++;
            }
            idxs[member]++;
            if(idxs[member]!=orders[member].size())
                waiter.push(member);
        }
        cout<<sumCost<<endl;
    }

    return 0;
}

Measured memory_bytes: 0