from collections import deque
n, m=[int(k) for k in input().split()]
w=[]
for j in range(m):
w.append([int(k) for k in input().split()])
q={}
for j in w:
if j[0] in q:
q[j[0]].append(j[1])
else:
q[j[0]]=[j[1]]
if j[1] in q:
q[j[1]].append(j[0])
else:
q[j[1]]=[j[0]]
res=[]
eta=[]
rho=[]
iota=list(q.keys())
epsilon=set(iota)
for j in range(1, n+1):
if j not in epsilon:
rho.append(j)
check=set()
for j in iota:
if j not in check:
new=deque([j])
check.add(j)
eta.append([j])
zeta=1
while new:
x=new.pop()
for k in q[x]:
if k not in check:
new.appendleft(k)
check.add(k)
zeta+=1
eta[-1].append(k)
res.append(zeta)
if zeta>3:
break
if zeta==2:
if rho:
eta[-1].append(rho.pop())
else:
res.append(4)
break
for j in res:
if j>3:
print(-1)
break
else:
for j in eta:
print(" ".join([str(k) for k in j]))
while rho:
print(rho.pop(), rho.pop(), rho.pop())
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653
#define N 100005
using namespace std;
int dx[9]={1, -1, 0, 0, 1, 1, -1, -1, 0};
int dy[9]={0, 0, 1, -1, 1, -1, 1, -1, 0};
bool vis[N/2000];
vector<int> cc[N/2000];
int cnt;
void dfs (vector<int> g[], int t)
{
vis[t]=true;
cc[cnt].pb(t);
for(int i=0; i<g[t].size(); i++)
{
if(vis[g[t][i]]==false) dfs(g, g[t][i]);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m, i, j;
cnt=0;
cin>>n>>m;
vector<int> g[N/2000];
vector< vector<int> > ans;
for(i=1; i<=m; i++)
{
int u, v;
cin>>u>>v;
g[u].pb(v); g[v].pb(u);
}
for(i=0; i<=n; i++) vis[i]=false;
for(i=1; i<=n; i++)
{
if(vis[i]==false)
{
dfs(g, i);
cnt++;
}
}
for(i=0; i<cnt; i++)
{
if(cc[i].size()>3) break;
}
if(i<cnt) cout<<-1;
else{
int c1=0, c2=0;
for(i=0; i<cnt; i++)
{
if(cc[i].size()==1) c1++;
else if(cc[i].size()==2) c2++;
}
vector<int> one;
for(i=0; i<cnt; i++)
{
if(cc[i].size()==1) one.pb(cc[i][0]);
}
if(c1<c2) cout<<-1;
else{
for(i=0; i<cnt; i++)
{
if(cc[i].size()==2) ans.pb(cc[i]);
}
for(i=0; i<ans.size(); i++) ans[i].pb(one[i]);
for(i=ans.size(); i<one.size(); i=i+3) ans.pb({one[i], one[i+1], one[i+2]});
for(i=0; i<cnt; i++)
{
if(cc[i].size()==3) ans.pb(cc[i]);
}
for(i=0; i<ans.size(); i++)
{
for(j=0; j<ans[i].size(); j++)
cout<<ans[i][j]<<" ";
cout<<"\n";
}
}
}
return 0;
}
1002. Find Common Characters | 1602A - Two Subsequences |
1555A - PizzaForces | 1607B - Odd Grasshopper |
1084A - The Fair Nut and Elevator | 1440B - Sum of Medians |
1032A - Kitchen Utensils | 1501B - Napoleon Cake |
1584B - Coloring Rectangles | 1562B - Scenes From a Memory |
1521A - Nastia and Nearly Good Numbers | 208. Implement Trie |
1605B - Reverse Sort | 1607C - Minimum Extraction |
1604B - XOR Specia-LIS-t | 1606B - Update Files |
1598B - Groups | 1602B - Divine Array |
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |
2090. K Radius Subarray Averages | 2091. Removing Minimum and Maximum From Array |
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |