#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àÁÆà·¢ÔNGXg³êÄ¢È¢{ð©Â¯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;
}