#include <stdio.h>
#include <string.h>
const int N = 1005*1005;
int L[N], R[N], U[N], D[N], C[N], Sum[N];
int Row[N], Col[N];
int ans[N], anslen;
int n, m, id;
bool OK;
void pre() {
for(int i = 0; i <= m; i ++) {
L[i] = i - 1;
R[i] = i + 1;
U[i] = D[i] = C[i] = i;
Sum[i] = 0;
}
L[0] = m; R[m] = 0;
id = m + 1;
}
void build() {
int num, x;
for(int i = 1; i <= n; i ++) {
scanf("%d", &num);
for(int j = 0; j < num; j ++, id ++) {
scanf("%d", &x);
Row[id] = i;
Col[id] = x;
Sum[x] ++;
U[id] = x;
D[id] = D[x];
U[D[x]] = id;
D[x] = id;
if( j == 0 ) {
L[id] = R[id] = id;
} else {
L[id] = id - 1;
R[id] = id - j;
R[id-1] = id;
L[id-j] = id;
}
}
}
}
inline void remove(int c) {
R[L[c]] = R[c];
L[R[c]] = L[c];
for(int id = D[c]; id != c; id = D[id]) {
for(int i = R[id]; i != id; i = R[i] ) {
D[U[i]] = D[i];
U[D[i]] = U[i];
Sum[Col[i]] --;
}
}
}
inline void resume(int c) {
L[R[c]] = c;
R[L[c]] = c;
for(int id = D[c]; id != c; id = D[id]) {
for(int i = R[id]; i != id; i = R[i] ) {
U[D[i]] = i;
D[U[i]] = i;
Sum[Col[i]] ++;
}
}
}
void dfs(int deep) {
if( R[0] == 0 ) {
OK = true;
anslen = deep;
return ;
}
int column = R[0];
for(int i = D[0]; i != 0; i = D[i]) {
if( Sum[i] <= Sum[column] ) column = i;
}
remove(column);
for(int id = D[column]; id != column; id = D[id]) {
ans[deep] = Row[id];
for(int i = R[id]; i != id; i = R[i]) remove(Col[i]);
dfs( deep + 1);
if(OK) return;
for(int i = R[id]; i != id; i = R[i]) resume(Col[i]);
}
resume(column);
}
int main() {
while( ~scanf("%d %d", &n, &m) ) {
pre();
build();
OK = false;
dfs(0);
if( OK ) {
printf("%d", anslen);
for(int i = 0; i < anslen; i ++) printf(" %d", ans[i]);
puts("");
} else {
puts("NO");
}
}
}
http://acm.hust.edu.cn/thanks/problem.php?id=1017