#include <bits/stdc++.h>
using namespace std;
const int MAXN = 55;
int N, M;
vector<int> flashes[MAXN][MAXN];
vector<int> forward_flash_chain, backward_flash_chain;
int main() {
  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < MAXN; j++) flashes[i][j].push_back(-1);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, k;
    cin >> x >> y >> k;
    x--, y--;
    flashes[x][y].clear();
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      t--;
      flashes[x][y].push_back(t);
    }
  }
  bool found = false;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (found) break;
      for (int k = 0; k < (int)flashes[i][j].size() - 1; k++) {
        if (flashes[i][j][k] == i && flashes[i][j][k + 1] == j) {
          bool bad = false;
          forward_flash_chain.clear();
          for (int l = k + 1; l < flashes[i][j].size(); l++)
            forward_flash_chain.push_back(flashes[i][j][l]);
          for (int l = 0; l < forward_flash_chain.size() - 1; l++) {
            if (forward_flash_chain.size() > 2 * N) break;
            if (flashes[forward_flash_chain[l]][forward_flash_chain[l + 1]]
                        .size() == 1 &&
                flashes[forward_flash_chain[l]][forward_flash_chain[l + 1]]
                       [0] == -1) {
              bad = true;
              break;
            }
            for (int m = 0;
                 m < flashes[forward_flash_chain[l]][forward_flash_chain[l + 1]]
                         .size();
                 m++)
              forward_flash_chain.push_back(
                  flashes[forward_flash_chain[l]][forward_flash_chain[l + 1]]
                         [m]);
          }
          backward_flash_chain.clear();
          for (int l = k; l >= 0; l--)
            backward_flash_chain.push_back(flashes[i][j][l]);
          for (int l = 0; l < backward_flash_chain.size() - 1; l++) {
            if (backward_flash_chain.size() > 2 * N) break;
            if (flashes[backward_flash_chain[l + 1]][backward_flash_chain[l]]
                        .size() == 1 &&
                flashes[backward_flash_chain[l + 1]][backward_flash_chain[l]]
                       [0] == -1) {
              bad = true;
              break;
            }
            for (int m = flashes[backward_flash_chain[l + 1]]
                                [backward_flash_chain[l]]
                                    .size() -
                         1;
                 m >= 0; m--)
              backward_flash_chain.push_back(
                  flashes[backward_flash_chain[l + 1]][backward_flash_chain[l]]
                         [m]);
          }
          if (!bad &&
              backward_flash_chain.size() + forward_flash_chain.size() <=
                  2 * N) {
            found = true;
            cout << backward_flash_chain.size() + forward_flash_chain.size()
                 << "\n";
            for (int l = backward_flash_chain.size() - 1; l >= 0; l--)
              cout << backward_flash_chain[l] + 1 << " ";
            for (int l = 0; l < forward_flash_chain.size(); l++)
              cout << forward_flash_chain[l] + 1 << " ";
            cout << "\n";
          }
          break;
        }
      }
    }
  if (!found) cout << "0\n";
  return 0;
}
