#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>

using namespace std;

int m, c, n;
int cost[20];
int dc[20], srn[20], sqr[20][100];
queue<int> req;
deque<int> sto[20];

void init()
{
	for (int i = 1; i <= 20; i++)
		sto[i].clear();
	while (req.size() > 0)
		req.pop();
}

int main()
{
	while (scanf("%d%d%d", &m, &c, &n) != EOF)
	{
		init();
		if (m == c && c == n && n == 0)
			break;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &srn[i]);
			for (int j = 0; j < srn[i]; j++)
				scanf("%d", &sqr[i][j]);
		}
		int tnn[20];
		memset(tnn, 0, sizeof(tnn));
		int ttt = 0;
		for (int i = 1; i <= n; i++)
			ttt += srn[i];
		//printf("tttt = %d\n", ttt);
		while (ttt--)
		{
			for (int i = 1; i <= n; i++)
			{
				if (tnn[i] < srn[i])
					req.push(sqr[i][tnn[i]]), tnn[i]++;
			}
		}
		/*
		while (req.size() > 0)
			printf("%d\n", req.front()), req.pop();
			*/
		memset(dc, 0, sizeof(dc));
		long long ans = 0;
		while (req.size() > 0)
		{
			int ttrep = req.front();
			req.pop();
			int flag = 0;
			for (int i = 1; i <= m; i++)
				for (int j = 0; j < sto[i].size(); j++)
					if (ttrep == sto[i][j])
					{
						ans += i;
						flag = 1;
						sto[i].erase(sto[i].begin() + j , sto[i].begin() + j + 1);
						break;
					}
			if (flag == 0)
				ans += (m + 1);

			//拿回storeroom
			//not full
			if (sto[1].size() < c)
			{
				sto[1].push_back(ttrep);
				ans += 1;
				//printf("ans = %d\n", ans);
			}
			else
			{
				//printf("ans = %d\n", ans);
				int trrr, trrrj;
				flag = 0;
				for (int i = 2; i <= m; i++)
					if (sto[i].size() < c)
					{
						trrr = i, trrrj = sto[i].size();
						//printf("trrrr = %d\n", trrr);
						sto[i].push_back(ttrep);
						flag = 1;
						ans += i;
						break;
					}
				//printf("trrr  =%d , trrrj =%d\n", trrr, trrrj);
				if (flag == 0)
				{
					trrr = m + 1;
					ans += (m + 1);
				}
				int loUn = sto[1].front();
				sto[1].pop_front();
				//printf("sto.size() = %d\n", sto[1].size());
				ans += 1;
				flag = 0;
				for (int i = 2; i <= m; i++)
					if (sto[i].size() < c)
					{
						sto[i].push_back(loUn);
						flag = 1;
						ans += i;
						break;
					}
				if (flag == 0)
					ans += (m + 1);
				if (trrr == m  + 1)
				{
					ans += (trrr + 1);
					sto[1].push_back(ttrep);
				}
				else
				{
					ans += (trrr + 1);
					sto[1].push_back(ttrep);
					sto[trrr].erase(sto[trrr].begin() + trrrj, sto[trrr].begin() + trrrj + 1);
				}
			}
			//printf("lllans  = %d\n", ans);
		}
		//printf("ans = %d\n" , ans);
		printf("%d\n", ans);
	}
	return 0;
}