#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

typedef pair<int,int> pii;

int main() {
  int m, c, n;
  while(cin>>m>>c>>n, m||c||n) {
    queue<int> b[n];
    queue<pii> Q;
    REP(i, n) {
      int k;
      cin >> k;
      REP(j, k) {
        int a;
        cin >> a;
        if (j==0)
          Q.push(pii(i, a));
        else
          b[i].push(a);
      }
    }
    int desk[m+1];              // desk[m]ÍgíÈ¢ªAªòð­È­·é½ßìé
    memset(desk, 0, sizeof(desk));
    int pos[100];
    REP(i, 100) pos[i] = m;
    int ans = 0;
    map<int, int> D1;
    int time = 0;
    
    while(!Q.empty()) {
      pii p = Q.front(); Q.pop();
      int book = p.second;
      int man = p.first;
      
      if (!b[man].empty()) {
        Q.push(pii(man, b[man].front()));
        b[man].pop();
      }

      // ¡ éê©çæèo·
      if (pos[book] != m)
        desk[pos[book]]--;
      ans += pos[book] + 1;
      // printf("%d©ç%dðæèo· : %d\n", pos[book], book, ans);
      
      if (pos[book]) {
        int nearest = m;
        REP(i, m)
          if (desk[i] < c) {
            nearest = i;
            break;
          }
        if (nearest == 0) {
          // D1Éu­
          desk[0]++;
          
          D1[book] = time;
          pos[book] = 0;
          ans += 1;
          // printf("0Éu­ : %d \n", ans);
        } else {
          // êIÉêÔß¢êÉu­
          int tmp = nearest;
          desk[tmp]++;
          ans += tmp + 1;
          // printf("êIÉ%dÉu­ : %d\n", tmp,ans);
          
          // D1©çêÔgíêÄÈ¢{ðIÑAÊÌêÉu­
          map<int,int>::iterator change;
          int mi = INF;
          FOR(it, D1) {
            if (mi > it->second) {
              mi = it->second;
              change = it;
            }
          }
          int changebook = change->first;
          D1.erase(change);
          desk[0]--;
          ans += 1;
          nearest = m;
          for (int i=1; i<m; ++i) 
            if (desk[i] < c) {
              nearest = i;
              break;
            }
          pos[changebook] = nearest;
          desk[nearest]++;
          ans += nearest+1;
          //printf("D1É é%dð%dÉu­ : %d\n", changebook, nearest, ans);
          
          // v³ê½{ðêIÉu¢½ê©çD1Éu­
          desk[tmp]--;
          ans += tmp + 1;
          D1[book] = time;
          desk[0]++;
          pos[book] = 0;
          ans += 1;
          //printf("%dÉ é%dð0Éu­ : %d\n", tmp, book, ans);
        }
      } else {
        ans += 1;
        //printf("0Éß· : %d\n", ans);
        desk[0]++;
        D1[book] = time;
      }
      //cout << ans << endl;
      time++;
    }
    cout << ans << endl;
  }
}

Measured memory_bytes: 0