/*Shovkoplyas Grigory in the house*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<deque>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define name "gods"
int n;
vector<vector<int> > g;
vector<int> was;
vector<int> color;
bool bad;
deque<int> up, down;
vector<int> au, ad;
void dfs(int v, int c)
{
was[v] = 1;
color[v] = c;
for(int i = 0; i < (int)g[v].size(); i++)
{
int u = g[v][i];
if(color[u] == c)
bad = true;
else if(!was[u])
dfs(u, c ^ 1);
}
}
void deal(int v, int dir, int p)
{
if(g[v].size() == 1)
return;
int n = g[v].size();
if(g[v][0] != p && g[v][n - 1] != p)
{
if(!color[v])
{
assert((int)down.size() == 1);
down.resize(0);
for(int i = 0; i < n; i++)
{
int u = g[v][i];
if(u != p && was[u])
bad = true;
was[u] = 1;
down.push_back(u);
}
}
else
{
assert((int)up.size() == 1);
up.resize(0);
for(int i = 0; i < n; i++)
{
int u = g[v][i];
if(u != p && was[u])
bad = true;
was[u] = 1;
up.push_back(u);
}
}
if(!bad)
deal(g[v][0], -1, v), deal(g[v][n - 1], 1, v);
return;
}
if(g[v][0] != p)
reverse(g[v].begin(), g[v].end());
for(int i = 1; i < n; i++)
{
int u = g[v][i];
if(was[u])
bad = true;
was[u] = 1;
if(dir == 1)
{
if(color[v])
up.push_back(u);
else
down.push_back(u);
}
else
{
if(color[v])
up.push_front(u);
else
down.push_front(u);
}
}
if(!bad)
deal(g[v][n - 1], dir, v);
}
void make_graph(int v)
{
was[v] = 1;
if((int)g[v].size() == 0)
{
if(!color[v])
au.push_back(v);
else
ad.push_back(v);
return;
}
if(!color[v])
up.push_back(v);
else
down.push_back(v);
for(int i = 0; i < (int)g[v].size(); i++)
{
int u = g[v][i];
if(was[u])
bad = true;
was[u] = 1;
if(color[v])
up.push_back(u);
else
down.push_back(u);
}
deal(g[v][0], -1, v);
if((int)g[v].size() > 1)
deal(g[v][g[v].size() - 1], 1, v);
for(int i = 0; i < (int)up.size(); i++)
au.push_back(up[i]);
for(int i = 0; i < (int)down.size(); i++)
ad.push_back(down[i]);
up.resize(0);
down.resize(0);
}
int main()
{
freopen(name".in", "r", stdin);
freopen(name".out", "w", stdout);
cin >> n;
color.assign(n, -1);
was.assign(n, 0);
g.resize(n);
for(int i = 0; i < n; i++)
{
int k;
cin >> k;
set<int> wwas;
for(int j = 0; j < k; j++)
{
int a;
cin >> a;
a--;
if (wwas.count(a) == 0)
g[i].push_back(a);
wwas.insert(a);
}
}
for(int i = 0; i < n; i++)
if(!was[i])
dfs(i, 0);
//cerr << bad << endl;
for(int i = 0; i < n; i++)
{
if(g[i].size() < 3)
continue;
int k = 0;
for(int j = 0; j < (int)g[i].size(); j++)
{
int u = g[i][j];
if(g[u].size() > 1)
k++;
}
if(k > 2)
{
bad = true;
break;
}
}
if(bad)
{
cout << "-1";
return 0;
}
for(int i = 0; i < n; i++)
{
bool e = false;
if(g[i].size() < 3)
continue;
for(int j = 0; j < (int)g[i].size(); j++)
{
if(g[g[i][j]].size() > 1)
{
if(!e)
swap(g[i][j], g[i][0]), e = true;
else
swap(g[i][j], g[i][g[i].size() - 1]);
}
}
}
was.assign(n, 0);
for(int i = 0; i < n; i++)
if(!was[i])
make_graph(i);
if(bad)
{
cout << "-1";
return 0;
}
cout << au.size() << ' ' << ad.size() << endl;
for(int i = 0; i < (int)au.size(); i++)
cout << au[i] + 1 << ' ';
cout << endl;
for(int i = 0; i < (int)ad.size(); i++)
cout << ad[i] + 1 << ' ';
cout << endl;
return 0;
}
LypTaG92a29wbHlhcyBHcmlnb3J5IGluIHRoZSBob3VzZSovCiNpbmNsdWRlPGNzdGRpbz4KI2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGFsZ29yaXRobT4KI2luY2x1ZGU8Y3RpbWU+CiNpbmNsdWRlPGRlcXVlPgojaW5jbHVkZTxjYXNzZXJ0PgojaW5jbHVkZTxjbWF0aD4KI2luY2x1ZGU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dV0gPSAxOwogICAgICAgIGRvd24ucHVzaF9iYWNrKHUpOwogICAgICB9CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgIGFzc2VydCgoaW50KXVwLnNpemUoKSA9PSAxKTsKICAgICAgdXAucmVzaXplKDApOwogICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICB7CiAgICAgICAgaW50IHUgPSBnW3ZdW2ldOwogICAgICAgIGlmKHUgIT0gcCAmJiB3YXNbdV0pCiAgICAgICAgICBiYWQgPSB0cnVlOwogICAgICAgIHdhc1t1XSA9IDE7CiAgICAgICAgdXAucHVzaF9iYWNrKHUpOwogICAgICB9CiAgICB9CiAgICBpZighYmFkKQogICAgICBkZWFsKGdbdl1bMF0sIC0xLCB2KSwgZGVhbChnW3ZdW24gLSAxXSwgMSwgdik7CiAgICByZXR1cm47IAogIH0KICBpZihnW3ZdWzBdICE9IHApCiAgICByZXZlcnNlKGdbdl0uYmVnaW4oKSwgZ1t2XS5lbmQoKSk7CiAgZm9yKGludCBpID0gMTsgaSA8IG47IGkrKykKICB7CiAgICBpbnQgdSA9IGdbdl1baV07CiAgICBpZih3YXNbdV0pCiAgICAgIGJhZCA9IHRydWU7CiAgICAgICAgd2FzW3VdID0gMTsKICAgIGlmKGRpciA9PSAxKQogICAgewogICAgICBpZihjb2xvclt2XSkKICAgICAgICB1cC5wdXNoX2JhY2sodSk7CiAgICAgIGVsc2UKICAgICAgICBkb3duLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgaWYoY29sb3Jbdl0pCiAgICAgICAgdXAucHVzaF9mcm9udCh1KTsKICAgICAgZWxzZQogICAgICAgIGRvd24ucHVzaF9mcm9udCh1KTsKICAgIH0gCiAgfQogIGlmKCFiYWQpCiAgICBkZWFsKGdbdl1bbiAtIDFdLCBkaXIsIHYpOwp9Cgp2b2lkIG1ha2VfZ3JhcGgoaW50IHYpCnsKICB3YXNbdl0gPSAxOwogIGlmKChpbnQpZ1t2XS5zaXplKCkgPT0gMCkKICB7CiAgICBpZighY29sb3Jbdl0pCiAgICAgIGF1LnB1c2hfYmFjayh2KTsKICAgIGVsc2UKICAgICAgYWQucHVzaF9iYWNrKHYpOwogICAgcmV0dXJuOwogIH0KICBpZighY29sb3Jbdl0pCiAgICB1cC5wdXNoX2JhY2sodik7CiAgZWxzZQogICAgZG93bi5wdXNoX2JhY2sodik7CiAgZm9yKGludCBpID0gMDsgaSA8IChpbnQpZ1t2XS5zaXplKCk7IGkrKykgIAogIHsKICAgICAgICBpbnQgdSA9IGdbdl1baV07CiAgICBpZih3YXNbdV0pCiAgICAgIGJhZCA9IHRydWU7CiAgICB3YXNbdV0gPSAxOwogICAgaWYoY29sb3Jbdl0pCiAgICAgIHVwLnB1c2hfYmFjayh1KTsKICAgIGVsc2UKICAgICAgZG93bi5wdXNoX2JhY2sodSk7CiAgfQogIGRlYWwoZ1t2XVswXSwgLTEsIHYpOwogIGlmKChpbnQpZ1t2XS5zaXplKCkgPiAxKQogICAgZGVhbChnW3ZdW2dbdl0uc2l6ZSgpIC0gMV0sIDEsIHYpOwogIGZvcihpbnQgaSA9IDA7IGkgPCAoaW50KXVwLnNpemUoKTsgaSsrKQogICAgYXUucHVzaF9iYWNrKHVwW2ldKTsKICBmb3IoaW50IGkgPSAwOyBpIDwgKGludClkb3duLnNpemUoKTsgaSsrKQogICAgYWQucHVzaF9iYWNrKGRvd25baV0pOwogICAgdXAucmVzaXplKDApOwogICAgZG93bi5yZXNpemUoMCk7Cn0KCmludCBtYWluKCkKewogIGZyZW9wZW4obmFtZSIuaW4iLCAiciIsIHN0ZGluKTsKICBmcmVvcGVuKG5hbWUiLm91dCIsICJ3Iiwgc3Rkb3V0KTsKICBjaW4gPj4gbjsKICBjb2xvci5hc3NpZ24obiwgLTEpOwogIHdhcy5hc3NpZ24obiwgMCk7CiAgZy5yZXNpemUobik7CiAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykKICB7CiAgICBpbnQgazsKICAgIGNpbiA+PiBrOwogICAgc2V0PGludD4gd3dhczsKICAgIGZvcihpbnQgaiA9IDA7IGogPCBrOyBqKyspCiAgICB7CiAgICAgIGludCBhOwogICAgICBjaW4gPj4gYTsKICAgICAgYS0tOwogICAgICBpZiAod3dhcy5jb3VudChhKSA9PSAwKQogICAgICAgICAgZ1tpXS5wdXNoX2JhY2soYSk7CiAgICAgIHd3YXMuaW5zZXJ0KGEpOwogICAgfQogIH0KICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgaWYoIXdhc1tpXSkKICAgICAgZGZzKGksIDApOwogIC8vY2VyciA8PCBiYWQgPDwgZW5kbDsKICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogIHsKICAgIGlmKGdbaV0uc2l6ZSgpIDwgMykKICAgICAgY29udGludWU7CiAgICBpbnQgayA9IDA7CiAgICBmb3IoaW50IGogPSAwOyBqIDwgKGludClnW2ldLnNpemUoKTsgaisrKQogICAgewogICAgICBpbnQgdSA9IGdbaV1bal07CiAgICAgIGlmKGdbdV0uc2l6ZSgpID4gMSkKICAgICAgICBrKys7CiAgICB9CiAgICBpZihrID4gMikKICAgIHsKICAgICAgYmFkID0gdHJ1ZTsKICAgICAgYnJlYWs7CiAgICB9CiAgfQogIGlmKGJhZCkKICB7CiAgICBjb3V0IDw8ICItMSI7CiAgICByZXR1cm4gMDsKICB9CiAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykKICB7CiAgICBib29sIGUgPSBmYWxzZTsKICAgIGlmKGdbaV0uc2l6ZSgpIDwgMykKICAgICAgY29udGludWU7CiAgICBmb3IoaW50IGogPSAwOyBqIDwgKGludClnW2ldLnNpemUoKTsgaisrKQogICAgewogICAgICBpZihnW2dbaV1bal1dLnNpemUoKSA+IDEpCiAgICAgIHsKICAgICAgICBpZighZSkKICAgICAgICAgIHN3YXAoZ1tpXVtqXSwgZ1tpXVswXSksIGUgPSB0cnVlOwogICAgICAgIGVsc2UKICAgICAgICAgIHN3YXAoZ1tpXVtqXSwgZ1tpXVtnW2ldLnNpemUoKSAtIDFdKTsKICAgICAgfQogICAgfQoKICB9CiAgd2FzLmFzc2lnbihuLCAwKTsKICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgaWYoIXdhc1tpXSkKICAgICAgbWFrZV9ncmFwaChpKTsKICBpZihiYWQpCiAgewogICAgY291dCA8PCAiLTEiOwogICAgcmV0dXJuIDA7CiAgfQogIGNvdXQgPDwgYXUuc2l6ZSgpIDw8ICcgJyA8PCBhZC5zaXplKCkgPDwgZW5kbDsKICBmb3IoaW50IGkgPSAwOyBpIDwgKGludClhdS5zaXplKCk7IGkrKykKICAgIGNvdXQgPDwgYXVbaV0gKyAxIDw8ICcgJzsKICBjb3V0IDw8IGVuZGw7CiAgZm9yKGludCBpID0gMDsgaSA8IChpbnQpYWQuc2l6ZSgpOyBpKyspCiAgICBjb3V0IDw8IGFkW2ldICsgMSA8PCAnICc7CiAgY291dCA8PCBlbmRsOwogIHJldHVybiAwOwp9CgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg