#include <bits/stdc++.h>
using namespace std;
const int MaxN = 55;
bool G[MaxN][MaxN];
vector<int> edge[MaxN][MaxN];
int n, m;
void work(int u, int v) {
  vector<int> vb, vf;
  bool flag;
  int sz = 0;
  flag = 0;
  for (unsigned int k = 1; k < edge[u][v].size(); ++k) {
    if (edge[u][v][k - 1] == u && edge[u][v][k] == v) flag = 1;
    if (flag) vb.push_back(edge[u][v][k]);
  }
  for (unsigned int i = 0; i + 1 < vb.size(); ++i) {
    int x = vb[i], y = vb[i + 1];
    if (!G[x][y]) return;
    for (auto j : edge[x][y]) vb.push_back(j);
    if ((int)vb.size() + sz > 2 * n) return;
  }
  sz = vb.size();
  for (unsigned int k = 0; k + 1 < edge[u][v].size(); ++k) {
    vf.push_back(edge[u][v][k]);
    if (edge[u][v][k] == u && edge[u][v][k + 1] == v) break;
  }
  reverse(vf.begin(), vf.end());
  for (unsigned int i = 0; i + 1 < vf.size(); ++i) {
    int x = vf[i], y = vf[i + 1];
    if (!G[y][x]) return;
    for (int k = edge[y][x].size() - 1; k >= 0; --k)
      vf.push_back(edge[y][x][k]);
    if ((int)vf.size() + sz > 2 * n) return;
  }
  sz += vf.size();
  reverse(vf.begin(), vf.end());
  printf("%d\n", sz);
  for (auto i : vf) printf("%d ", i);
  for (auto i : vb) printf("%d ", i);
  puts("");
  exit(0);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v, k;
    scanf("%d%d%d", &u, &v, &k);
    G[u][v] = 1;
    vector<int> &vec = edge[u][v];
    vec.resize(k);
    for (int j = 0; j < k; ++j) scanf("%d", &vec[j]);
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (G[i][j]) {
        int cnt = 0;
        for (unsigned int k = 1; k < edge[i][j].size(); ++k)
          if (edge[i][j][k - 1] == i && edge[i][j][k] == j) ++cnt;
        if (cnt == 1) work(i, j);
      }
  puts("0");
  return 0;
}
