#pragma  warning (disable:4996)
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int m, c, n;
int k[102];
int b[102][52];
vector<int> desk[12];//&#27599;&#24352;&#26700;子上放的&#20070;&#32534;号
int amount[12];
int que[5005];//拿&#20070;&#39034;序
int maxk;
int max(int a, int b){
	if (a > b)return a;
	else return b;
}
int main(){
	while (cin >> m >> c >> n){
		if (!m&&!n&&!c) break;
		int i, j,p;
		
		memset(b, 0, sizeof(b));
		memset(desk, 0, sizeof(desk));
		memset(amount, 0, sizeof(amount));
		memset(que, 0, sizeof(que));
		memset(k, 0, sizeof(k));
		maxk = -10;

		for ( i = 1; i <= n; i++){
			cin >> k[i];
			maxk = max(maxk, k[i]);
			for ( j = 1; j <= k[i]; j++)
				cin >> b[i][j];
		}
		

		int ans = 0;
		int num = 0;

		for (j = 1; j <= maxk; j++){
			for (i = 1; i <= n; i++){
				if (b[i][j] != 0)
				{
					que[num++] = b[i][j];
					
				}
			}
		}
		
		int id;
		int mm = 0;
		
		
		while (mm < num){
			id = que[mm];
			int flag = 0;
			//找&#20070;
			/*for (j = 1; j <= m; j++){
				cout << "desk["<<j<<"]:";
				for (i = 0; i < desk[j].size(); i++)
					cout << desk[j][i] << " ";
				cout << endl;
			}*/
				
			for (i = 1; i <= m; i++){
				for (j = 0; j < desk[i].size(); j++){
					if (desk[i][j] == id) {
						flag = 1;
						ans += i;
						//cout << "第"<<mm<<"cinashu"<<i << endl;
						amount[i]--;
						desk[i][j] = 0;//拿走了
						break;
					}
				}
				if (flag)break;
			}
			if (!flag){
				ans += (m + 1); //cout << "第" << mm << "cinashu" << m+1 << endl;
			}

			//放&#20070;
			int temp;
			if (amount[1] < c){
				desk[1].push_back(id);
				ans += 1;
				amount[1]++;
				
			}
			else{
				for (i = 2; i <= m + 1; i++){
					if (amount[i] < c || (i == m + 1))
					{

						temp = i;//&#26242;&#26102;放的位置
						amount[i]++;
						
						break;
					}
				}
				int longestid;//很久没用的&#20070;

				for (i = 0; i < desk[1].size(); i++){
					if (desk[1][i])
					{
						longestid = desk[1][i];
						desk[1][i] = 0;
					
						break;
					}
				}
				desk[1].push_back(id);
				

				int temp2;	
				for (p = temp ; p <= m + 1; p++){
					if (amount[p] < c || p == m + 1){

						temp2 = p;
						desk[p].push_back(longestid);
						amount[p]++;
						break;
					}
				}
				amount[temp]--;
				ans += (2 * temp + temp2 + 2);
				//cout << temp <<" "<< temp2 <<" "<<2 * temp + temp2 + 2<< endl;


			}mm++;
		}
				
		cout << ans << endl;

		
	}return 0;
}