#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int f[105][55], b[15][35], pos[105];

main(){
	int m, c, n, i, j, k, p, max, s;
	while (scanf("%d%d%d",&m,&c,&n)){
		if (m==0 && c==0 && n==0) break;
		memset(f,0,sizeof(f));
		memset(b,0,sizeof(b));
		for (i=1;i<=100;++i) pos[i]=m+1;
		max=0;
		for (i=1;i<=n;++i){
			scanf("%d",&k);
			if (k>max) max=k;
			for (j=1;j<=k;++j) {
				scanf("%d",&f[i][j]);
			}
		}
		s=0;
		for (j=1;j<=max;++j)
		  for (i=1;i<=n;++i)
		    if (f[i][j]>0){
				//printf("%d %d\n",f[i][j],pos[f[i][j]]);
				//printf("%d\n",b[1][0]);
				s+=pos[f[i][j]];                                    //蜴溷ァ狗噪諡ソ荵ヲ 
				if (pos[f[i][j]]==1){                               //譛ャ譚・蟆ア蝨ィ隨ャ荳?クェ荵ヲ譟?
					s+=1;
					for (k=1;k<=b[1][0];++k)
					  if (b[1][k]==f[i][j]) break;
					for (;k<b[1][0];++k) b[1][k]=b[1][k+1];
					b[1][b[1][0]]=f[i][j];                                 
				}
				else {                                              //荳榊惠隨ャ荳?クェ荵ヲ譟?
					if (b[1][0]<c){                                 //隨ャ荳?クェ荵ヲ譟懈イ。譛画サ。 
						s+=1;
						b[1][0]++;
						pos[f[i][j]]=1;
						b[1][b[1][0]]=f[i][j];
					}
					else {                                          //隨ャ荳?クェ荵ヲ譟懈サ。莠?
						for (k=2;k<=m;++k){                         //謇セ遨コ荵ヲ譟?
							if (b[k][0]<c) break;
					    }
					    s+=k; 
					    if (pos[f[i][j]]<=m) b[pos[f[i][j]]][0]--;
					    pos[f[i][j]]=k;                              
					    if (k<=m) b[k][0]++;                        //荳肴弍荵ヲ譫カ 
					    s+=1;                                       //謚顔ャャ荳?クェ荵ヲ譟懈怙髟ソ荳咲畑逧?ケヲ諡ソ襍ー(隨ャ荳?悽)
					    for (k=2;k<=m;++k){                         //謇セ蝨ー譁ケ謾セ荳?
							if (b[k][0]<c) break;
						}
						s+=k;
						pos[b[1][1]]=k;                        
						if (k<=m) {                                 //謾セ逧?ク肴弍荵ヲ譫カ 
							b[k][0]++;                                 
							b[k][b[k][0]]=b[1][1];                     
						}
						if (pos[f[i][j]]<=m) b[pos[f[i][j]]][0]--;
						for (p=2;p<c;++p) b[1][p]=b[1][p+1];        //譖エ譁ー隨ャ荳?クェ荵ヲ譟懃噪荵ヲ菴ソ逕ィ鬚醍紫 
						s+=pos[f[i][j]]+1;                          //霑呎悽荵ヲ諡ソ蝗樊擂謾セ蛻ー隨ャ荳?クェ荵ヲ譟懈怙蜷朱擇 
						b[1][c]=f[i][j];
						pos[f[i][j]]=1;                             
					}
				}
				//printf("%d\n\n",s); 
			}
		printf("%d\n",s);
	}
	return 0;
}