#include <bits/stdc++.h>
using namespace std;
const int maxN = 50 + 5;
const int maxM = 400 * 400 + 300;
const long long mod = 1000 * 1000 * 1000 + 7;
vector<int> a[maxN][maxN], ar[maxN][maxN];
int n;
int mp[maxN][maxN], mpr[maxN][maxN];
void check(int v, int u) {
  for (int i = 0; i + 1 < a[v][u].size(); i++)
    if (a[v][u][i] == v && a[v][u][i + 1] == u) {
      vector<int> v1, v2;
      for (int j = 0; j + 1 < ar[u][v].size(); j++)
        if (ar[u][v][j] == u && ar[u][v][j + 1] == v)
          for (int k = j; k < ar[u][v].size(); k++) v1.push_back(ar[u][v][k]);
      for (int j = i; j < a[v][u].size(); j++) v2.push_back(a[v][u][j]);
      int ps = 1;
      while (ps + 1 < v1.size()) {
        if (v1.size() > 2 * n) return;
        int xv = v1[ps], xu = v1[ps + 1];
        if (mpr[xv][xu] == 0) return;
        for (int j = 0; j < ar[xv][xu].size(); j++) v1.push_back(ar[xv][xu][j]);
        ps++;
      }
      ps = 1;
      while (ps + 1 < v2.size()) {
        if (v1.size() + v2.size() - 2 > 2 * n) return;
        int xv = v2[ps], xu = v2[ps + 1];
        if (mp[xv][xu] == 0) return;
        for (int j = 0; j < a[xv][xu].size(); j++) v2.push_back(a[xv][xu][j]);
        ps++;
      }
      reverse(v1.begin(), v1.end());
      v1.pop_back();
      v1.pop_back();
      cout << v1.size() + v2.size() << endl;
      for (int j = 0; j < v1.size(); j++) cout << v1[j] + 1 << ' ';
      for (int j = 0; j < v2.size(); j++) cout << v2[j] + 1 << ' ';
      cout << endl;
      exit(0);
    }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int m;
  cin >> n >> m;
  vector<pair<int, int> > ed;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    v--;
    u--;
    mp[v][u] = mpr[u][v] = 1;
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      x--;
      a[v][u].push_back(x);
      ar[u][v].push_back(x);
    }
    reverse(ar[u][v].begin(), ar[u][v].end());
    ed.push_back({v, u});
  }
  for (int i = 0; i < m; i++) check(ed[i].first, ed[i].second);
  cout << 0 << endl;
  return 0;
}
