#include <bits/stdc++.h>
using namespace std;
int n, m, s[1300], e[1300];
vector<int> edg[55][55];
bool f[55][55];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b, k; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &k);
    s[i] = a;
    e[i] = b;
    edg[a][b].resize(k);
    for (int j = 0; j < k; j++) scanf("%d", &edg[a][b][j]);
    f[a][b] = 1;
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j < edg[s[i]][e[i]].size(); j++) {
      if (edg[s[i]][e[i]][j - 1] == s[i] && edg[s[i]][e[i]][j] == e[i]) {
        vector<int> cur = edg[s[i]][e[i]], tmp;
        while (1) {
          if (cur.size() > 2 * n) break;
          bool flag = 0;
          tmp.clear();
          for (int t = 0; t + 1 < cur.size(); t++) {
            if (!f[cur[t]][cur[t + 1]]) {
              flag = 1;
              break;
            }
            tmp.insert(tmp.end(), edg[cur[t]][cur[t + 1]].begin(),
                       edg[cur[t]][cur[t + 1]].end());
          }
          if (flag) break;
          if (tmp == cur) {
            printf("%d\n", cur.size());
            for (auto &x : cur) printf("%d ", x);
            return 0;
          }
          if (tmp.size() > 2 * n) break;
          cur = tmp;
        }
        break;
      }
    }
  }
  puts("0");
  return 0;
}
