#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int Maxn = 55, Maxm = 300005, Mo = 10007,
          sp[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const long long oo = LLONG_MAX >> 2;
typedef int IArr[Maxn];
typedef long long LArr[Maxn];
typedef double DArr[Maxn];
struct Tree {
  int v, mk;
};
struct point {
  int x, y;
};
struct edge {
  int v;
  vector<int> vr;
};
vector<int> e[Maxn][Maxn];
IArr et[Maxn];
int i, j, n, m;
int is_inti(int u, int v, vector<int>& vr) {
  for (int i = 0; i + 1 < vr.size(); i++)
    if (vr[i] == u && vr[i + 1] == v) return 1;
  return 0;
}
void extend_f(deque<int>& ans, deque<int>& vr, int v, int& len) {
  while (len >= 0) {
    if (vr.empty()) return;
    int u = vr.front();
    if (!et[u][v]) len = -1;
    vr.pop_front();
    ans.push_front(u);
    for (int i = e[u][v].size() - 1; i >= 0; i--)
      vr.push_back((e[u][v][i])), len--;
    v = u;
  }
}
void extend_b(deque<int>& ans, deque<int>& vr, int u, int& len) {
  while (len >= 0) {
    if (vr.empty()) return;
    int v = vr.front();
    if (!et[u][v]) len = -1;
    vr.pop_front();
    ans.push_back((v));
    for (int i = 0; i < e[u][v].size(); i++) vr.push_back((e[u][v][i])), len--;
    u = v;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    int u, v, k, g;
    cin >> u >> v >> k;
    et[u][v] = 1;
    while (k--) cin >> g, e[u][v].push_back((g));
  }
  for (int u = 1; u <= n; u++)
    for (int v = 1; v <= n; v++)
      if (et[u][v] && is_inti(u, v, e[u][v])) {
        deque<int> ans, Q_f, Q_b;
        ans.push_back((u));
        ans.push_back((v));
        int p = 0, len = 2 * n - 2;
        while (e[u][v][p] != u) Q_f.push_front(e[u][v][p++]);
        p += 2;
        while (p < e[u][v].size()) Q_b.push_back((e[u][v][p++]));
        extend_f(ans, Q_f, u, len);
        extend_b(ans, Q_b, v, len);
        if (len < 0) continue;
        cout << ans.size() << endl;
        while (ans.size()) {
          cout << ans.front() << " ";
          ans.pop_front();
        }
        return 0;
      }
  cout << 0 << endl;
}
