#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	int table[50],index,num_book;
};

struct LOC{
	int desk,index;
};

struct Data{
	int book_id,put_time;
};

int num_desk,LIMIT,num_student,have_num[11],tmp_pos;
Data D_1[30];
LOC loc[101];

void func(){

	for(int i = 1; i <= 100; i++)loc[i].desk = num_desk+1;

	for(int i = 1; i <= num_desk; i++)have_num[i] = 0;

	for(int i = 0; i < LIMIT; i++)D_1[i].book_id = -1;

	queue<Info> Q;
	for(int loop = 0; loop < num_student; loop++){
		Info new_info;
		new_info.index = 0;
		scanf("%d",&new_info.num_book);
		for(int i = 0; i < new_info.num_book; i++)scanf("%d",&new_info.table[i]);
		Q.push(new_info);
	}

	int ans = 0,time = 0,current,next,book_id;
	int can_put_desk,space_D1,min_time,min_index,move_id;

	Info tmp;

	while(!Q.empty()){

		tmp = Q.front();
		Q.pop();

		book_id = tmp.table[tmp.index];

		current = loc[book_id].desk;

		if(current <= num_desk){
			have_num[current]--;
			if(current == 1){
				D_1[loc[book_id].index].book_id = -1;
			}
		}
		ans += current;
		time++;

		for(can_put_desk = 1; can_put_desk <= num_desk; can_put_desk++){
			if(have_num[can_put_desk] < LIMIT)break;
		}
		time++;

		if(can_put_desk == 1){

			ans += 1;

			for(int i = 0; i < LIMIT; i++){
				if(D_1[i].book_id == -1){
					space_D1 = i;
					break;
				}
			}

			loc[book_id].desk = 1;
			loc[book_id].index = space_D1;

			D_1[space_D1].book_id = book_id;
			D_1[space_D1].put_time = time;

			have_num[1]++;

		}else{

			ans += can_put_desk;
			tmp_pos = can_put_desk;

			if(can_put_desk <= num_desk){
				have_num[can_put_desk]++;
			}

			loc[book_id].desk = can_put_desk;

			min_time = BIG_NUM;

			for(int i = 0; i < LIMIT; i++){
				if(D_1[i].book_id != -1){
					if(min_time > D_1[i].put_time){
						min_time = D_1[i].put_time;
						min_index = i;
					}
				}
			}

			move_id = D_1[min_index].book_id;
			D_1[min_index].book_id = -1;
			have_num[1]--;

			ans += 1;

			for(can_put_desk = 2; can_put_desk <= num_desk; can_put_desk++){
				if(have_num[can_put_desk] < LIMIT)break;
			}
			time++;

			ans += can_put_desk;

			loc[move_id].desk = can_put_desk;

			if(can_put_desk <= num_desk){
				have_num[can_put_desk]++;
			}

			time++;

			ans += (tmp_pos+1);
			have_num[1]++;
			have_num[tmp_pos]--;
			for(int i = 0; i < LIMIT; i++){
				if(D_1[i].book_id == -1){
					space_D1 = i;
					break;
				}
			}

			loc[book_id].desk = 1;
			loc[book_id].index = space_D1;

			D_1[space_D1].book_id = book_id;
			D_1[space_D1].put_time = time;
		}

		if(tmp.index == tmp.num_book-1)continue;

		Info next_info;
		for(int i = 0; i < tmp.num_book; i++)next_info.table[i] = tmp.table[i];
		next_info.index = tmp.index+1;
		next_info.num_book = tmp.num_book;

		Q.push(next_info);
	}

	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d %d %d",&num_desk,&LIMIT,&num_student);
		if(num_desk == 0 && LIMIT == 0 && num_student == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 3156