#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
const int N = 10000;
int n, m;
bitset<N> c[N];
int cc = 1, used1[N], used2[N], pa1[N], pa2[N]; // used[i] == cc
/** used[1], pa2[2] */
void read() {
cin >> n >> m;
forn(i, n) {
int x;
while ((cin >> x) && x)
c[i][x - 1] = 1;
}
}
int dfs( int v ) {
used1[v] = cc;
forn(i, m)
if (c[v][i]) {
used2[i] = cc;
if (pa2[i] == -1 || (used1[pa2[i]] != cc && dfs(pa2[i]))) {
pa2[i] = v;
pa1[v] = i;
return 1;
}
}
return 0;
}
int main() {
read();
fill(pa2, pa2 + m, -1);
fill(pa1, pa1 + n, -1);
forn(i, n)
if (dfs(i))
cc++;
cc++;
forn(i, n)
if (pa1[i] == -1)
dfs(i);
forn(i, n)
if (used1[i] != cc)
printf("%d ", i + 1);
puts("");
forn(i, m)
if (used2[i] == cc)
printf("%d ", i + 1);
puts("");
}