Source Code
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
#define F first
#define S second
#define all(a) a.begin(),a.end()

using namespace std;

const ll Mod=1e9+7 ;

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

void Max(ll& x,ll y){
x=max(x,y);
}
void Min(ll& x,ll y){
x=min(x,y);
}
void OK(bool yes){
    cout<<(yes?"Yes\n":"No\n");
}

const ll N=500500,M=2010,LN=(1<<24),K=17,Mx=4e6+50,inf=3e18,Mod1=1e9+7,Mod2=999997457;
const ld pi=acos(-1),eps=1e-12;

ll Fact[N];
void init(){
Fact[0]=1ll;
for(int i=1;i<N;i++)Fact[i]=(1ll*Fact[i-1]*i)%Mod;
}
int Cnk(ll x,ll y){
if(y > x)return 0;
return (  ((Fact[x]*poww(Fact[y],-1,Mod))%Mod)  *poww(Fact[x-y],-1,Mod))%Mod;
}


void Add(ll& x,ll y,int j=0){
ll mod=Mod;
if(j==1)mod=Mod1;
if(j==2)mod=Mod2;
x%=mod;
y%=mod;
if(x<0)x+=mod;
if(y<0)y+=mod;
x=(x+y>=mod?x+y-mod:x+y);
}
/*
void init(){
Fact[0]=iFact[0]=1ll;
for(int i=1;i<N;i++)Fact[i]=(Fact[i-1]*i)%Mod;
for(int i=1;i<N;i++)iFact[i]=(iFact[i-1]*poww(i,-1,Mod))%Mod;
}
*/

bool ok=1;

ll vis[N];

vector<ll> v1[N],Ans;
vector<pll> v[N];

void dfs(ll x){
if(vis[x])return;
vis[x]=1;
for(auto p:v1[x])Ans.pb(p);
if(v[x].size()<=2){
    for(auto p:v[x]){
        if(vis[p.F])continue;
        Ans.pb(p.S);
        dfs(p.F);
    }
}
else{
    ll cnt=0;
    for(auto p:v[x]){
        if(vis[p.F])continue;
        if(v[p.F].size()>1)cnt++;
        if(cnt>1)break;
    }
    if(cnt>1){
        ok=0;
        return;
    }
    ll later=0,later_Ans=0;
    for(auto p:v[x]){
        if(vis[p.F])continue;
        if(v[p.F].size()>1){
            later=p.F;
            later_Ans=p.S;
        }
        else{
            Ans.pb(p.S);
            dfs(p.F);
        }
    }
    if(later){
        Ans.pb(later_Ans);
        dfs(later);
    }
}

}


int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

    //freopen("heinto.in","r",stdin);


    int T=1;
    int Case=1;
    //init();
    //cin>>T;
    while(T--){
        ll n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            ll k;
            cin>>k;
            if(k==1){
                ll x;
                cin>>x;
                v1[x].pb(i);
            }
            else{
                ll x,y;
                cin>>x>>y;
                v[x].pb({y,i});
                v[y].pb({x,i});
            }
        }
        for(int i=1;i<=m;i++){
            if(vis[i])continue;
            if(v[i].size()<=1){
                dfs(i);
            }
        }
        if(Ans.size()!=n)ok=0;
        if(ok){
            for(auto p:Ans)cout<<p<<' ';
            cout<<'\n';
        }
        else{
            cout<<-1<<'\n';
        }
        //OK(ok);
    }

    return 0;
}
/*

6 7
2 1 2
2 2 7
2 4 7
2 5 4
2 5 6
2 3 7

*/

Copy
Gym Wise-ard
GNU G++17
13 ms
24.2 MB
Wrong Answer
Test Case #1
13 ms
23.9 MB
Accepted
Input
3 3
2 2 3
1 1
2 2 1
Output
2 3 1
Judge Output
2 3 1
Checker Message
ok
Test Case #2
13 ms
23.9 MB
Accepted
Input
4 10
1 4
1 9
1 4
1 9
Output
1 3 2 4
Judge Output
1 3 2 4
Checker Message
ok
Test Case #3
13 ms
24.0 MB
Accepted
Input
3 3
2 1 2
2 2 3
2 3 1
Output
-1
Judge Output
-1
Checker Message
ok
Test Case #4
13 ms
24.2 MB
Accepted
Input
1 1
1 1
Output
1
Judge Output
1
Checker Message
ok
Test Case #5
13 ms
24.1 MB
Wrong Answer
Input
20 15
2 2 13
2 3 13
2 7 14
2 8 14
2 8 14
2 13 3
1 3
2 10 3
1 15
2 14 15
2 3 12
2 15 11
2 14 12
2 12 14
2 13 2
2 14 15
2 15 14
1 13
2 12 3
2 12 3
Output
-1
Judge Output
18 1 15 2 6 7 8 11 19 20 13 14 3 4 5 10 16 17 9 12
Checker Message
participant coudln't find answer but jury did