#include <bits/stdc++.h>
long long n, m, get;
long long a[105][105], seq[105][105][105];
long long pl[1300], pr[1300], rd[1300];
long long flag[105][105];
void find() {
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++)
      if (flag[i][j]) {
        long long pos, x, y;
        for (long long k = 1; k <= seq[i][j][0] - 1; k++)
          if (seq[i][j][k] == i && seq[i][j][k + 1] == j) {
            pos = k;
            break;
          }
        pl[pl[0] = 1] = i, pr[pr[0] = 1] = j;
        rd[0] = 0;
        for (long long k = pos + 1; k <= seq[i][j][0]; k++)
          rd[++rd[0]] = seq[i][j][k];
        while (pl[0] + pr[0] <= 2 * n + 1 && rd[0] > pr[0] &&
               a[rd[pr[0]]][rd[pr[0] + 1]]) {
          x = rd[pr[0]], y = rd[pr[0] + 1];
          pr[++pr[0]] = y;
          for (long long k = 1; k <= seq[x][y][0]; k++)
            rd[++rd[0]] = seq[x][y][k];
        }
        if (rd[0] > pr[0]) continue;
        rd[0] = 0;
        for (long long k = pos; k >= 1; k--) rd[++rd[0]] = seq[i][j][k];
        while (pl[0] + pr[0] <= 2 * n + 1 && rd[0] > pl[0] &&
               a[rd[pl[0] + 1]][rd[pl[0]]]) {
          x = rd[pl[0] + 1], y = rd[pl[0]];
          pl[++pl[0]] = x;
          for (long long k = seq[x][y][0]; k >= 1; k--)
            rd[++rd[0]] = seq[x][y][k];
        }
        if (rd[0] > pl[0]) continue;
        if (!get) {
          get = 1;
          printf("%d\n", pl[0] + pr[0]);
          for (long long k = pl[0]; k >= 1; k--) printf("%d ", pl[k]);
          for (long long k = 1; k <= pr[0]; k++) printf("%d ", pr[k]);
          printf("\n");
        }
      }
}
int main() {
  scanf("%lld", &n), scanf("%lld", &m);
  for (long long i = 1; i <= m; i++) {
    long long x, y;
    scanf("%lld", &x), scanf("%lld", &y);
    a[x][y] = 1;
    scanf("%lld", &seq[x][y][0]);
    for (long long j = 1; j <= seq[x][y][0]; j++)
      scanf("%lld", &seq[x][y][j]),
          flag[x][y] |= j > 1 && seq[x][y][j - 1] == x && seq[x][y][j] == y;
  }
  find();
  if (!get) printf("0\n");
}
