A programming coach has n students to teach. We know that n is divisible by 3. Let's assume that all students are numbered from 1 to n, inclusive.

Before the university programming championship the coach wants to split all students into groups of three. For some pairs of students we know that they want to be on the same team. Besides, if the i-th student wants to be on the same team with the j-th one, then the j-th student wants to be on the same team with the i-th one. The coach wants the teams to show good results, so he wants the following condition to hold: if the i-th student wants to be on the same team with the j-th, then the i-th and the j-th students must be on the same team. Also, it is obvious that each student must be on exactly one team.

Help the coach and divide the teams the way he wants.

Input

The first line of the input contains integers n and m (3 ≤ n ≤ 48, <image>. Then follow m lines, each contains a pair of integers ai, bi (1 ≤ ai < bi ≤ n) — the pair ai, bi means that students with numbers ai and bi want to be on the same team.

It is guaranteed that n is divisible by 3. It is guaranteed that each pair ai, bi occurs in the input at most once.

Output

If the required division into teams doesn't exist, print number -1. Otherwise, print <image> lines. In each line print three integers xi, yi, zi (1 ≤ xi, yi, zi ≤ n) — the i-th team. 

If there are multiple answers, you are allowed to print any of them.

Examples

Input

3 0


Output

3 2 1 


Input

6 4
1 2
2 3
3 4
5 6


Output

-1


Input

3 3
1 2
2 3
1 3


Output

3 2 1

Difficulty: MEDIUM
Tags: Graph algorithms

Graph traversal

Complete search
Skill types: Complete search

Starter code:

Solutions:
(n, m) = map(int, input().split())
arr = [i for i in range(n + 1)]
mem = {}
for i in range(m):
	(a, b) = map(int, input().split())
	if arr[a] in mem:
		mem[arr[a]].append(arr[b])
		arr[b] = arr[a]
	elif arr[b] in mem:
		mem[arr[b]].append(arr[a])
		arr[a] = arr[b]
	else:
		mem[arr[a]] = [arr[a], arr[b]]
		arr[b] = arr[a]
for i in mem.keys():
	arr[i] = 0
brr = []
for i in range(1, n + 1):
	if arr[i] == i:
		brr.append(i)
l1 = len(brr)
crr = list(mem.values())
l2 = len(crr)
for i in range(l2):
	crr[i] = list(set(crr[i]))
flag = 0
for i in crr:
	if len(i) > 3:
		flag = 1
		break
	elif len(i) == 3:
		continue
	else:
		try:
			i.append(brr[0])
			brr = brr[1:]
		except:
			flag = 1
			break
if flag != 1:
	for i in crr:
		print(*i)
	l1 = len(brr)
	for j in range(0, l1, 3):
		print(brr[j], brr[j + 1], brr[j + 2])
else:
	print(-1)


N = 64
rd = lambda : list(map(int, input().split()))

def dfs(u):
	x = [u]
	c.add(u)
	for v in I:
		if g[u][v] and v not in c:
			x += dfs(v)
	return x

def fail():
	print(-1)
	exit()
(n, m) = rd()
g = [[0] * N for _ in range(N)]
for _ in range(m):
	(u, v) = rd()
	g[u][v] = g[v][u] = True
v = [[], [], [], []]
c = set()
I = range(1, n + 1)
for i in I:
	if i not in c:
		x = dfs(i)
		l = len(x)
		if l > 3:
			fail()
		v[l] += [x]
for x in v[2]:
	if v[1]:
		v[3] += [x + v[1].pop()]
	else:
		fail()
if len(v[1]) % 3:
	fail()
while v[1]:
	v[3] += [v[1].pop() + v[1].pop() + v[1].pop()]
for x in v[3]:
	print(' '.join(map(str, x)))


from collections import defaultdict
(n, m) = map(int, input().split())
d = defaultdict(list)
f = [False] * (n + 1)
v = []

def dfs(i, s):
	s.add(i)
	f[i] = True
	for k in d[i]:
		if not f[k]:
			dfs(k, s)
for j in range(m):
	(x, y) = map(int, input().split())
	d[x].append(y)
	d[y].append(x)
for i in range(1, n + 1):
	if d[i] and (not f[i]):
		s = set()
		dfs(i, s)
		if len(s) > 3:
			print(-1)
			exit()
		v.append(list(s))
if len(v) > n // 3:
	print(-1)
	exit()
while len(v) < n // 3:
	v.append([])
j = 0
for i in range(1, n + 1):
	if not f[i]:
		while len(v[j]) == 3:
			j += 1
		v[j].append(i)
for i in v:
	print(i[0], i[1], i[2])


(n, m) = input().split(' ')
n = int(n)
m = int(m)
c = []
s = 1
hh = []
cc = [i for i in range(1, n + 1)]
for i in range(0, m):
	(a, b) = input().split(' ')
	a = int(a)
	b = int(b)
	c.append([a, b])
for i in range(0, m):
	e = c[i]
	for k in range(0, i):
		if c[k][0] in e and c[k][1] not in e:
			e.append(c[k][1])
		elif c[k][1] in e and c[k][0] not in e:
			e.append(c[k][0])
	for k in range(i + 1, m):
		if c[k][0] in e and c[k][1] not in e:
			e.append(c[k][1])
		elif c[k][1] in e and c[k][0] not in e:
			e.append(c[k][0])
	e.sort()
	if len(e) > 3:
		s = 0
		break
	elif e not in hh:
		hh.append(e)
		for i in e:
			cc.remove(i)
kkk = int(n / 3)
hhh = int(kkk - len(hh))
if hhh < 0:
	s = 0
for i in range(len(hh)):
	if len(hh[i]) == 2 and hhh >= 0:
		hh[i].append(cc[0])
		del cc[0]
for i in range(hhh):
	hh.append([cc[0], cc[1], cc[2]])
	del cc[0]
	del cc[0]
	del cc[0]
if s == 0:
	print(-1)
else:
	for i in range(0, kkk):
		print(hh[i][0], hh[i][1], hh[i][2])


def recursive_dfs(graph, start, path=[]):
	path = path + [start]
	for node in graph[start]:
		if not node in path:
			path = recursive_dfs(graph, node, path)
	return path
(a, b) = map(int, input().split(' '))
graph = {}
for i in range(a):
	graph[i] = []
for i in range(b):
	(x, y) = map(int, input().split(' '))
	graph[x - 1].append(y - 1)
	graph[y - 1].append(x - 1)
marked = [False] * a
ones = []
twos = []
threes = []
bad = []
for i in range(a):
	if not marked[i]:
		a = recursive_dfs(graph, i)
		if len(a) == 1:
			ones.append(a)
		elif len(a) == 2:
			twos.append(a)
		elif len(a) == 3:
			threes.append(a)
		else:
			print(-1)
			quit()
		for j in recursive_dfs(graph, i):
			marked[j] = True
if len(twos) > len(ones):
	print(-1)
	quit()
else:
	for a in threes:
		print(' '.join([str(i + 1) for i in a]))
	for a in twos:
		print(str(ones[0][0] + 1) + ' ' + ' '.join([str(i + 1) for i in a]))
		ones.remove([ones[0][0]])
	while len(ones) > 0:
		a = ones[0][0]
		b = ones[1][0]
		c = ones[2][0]
		print(' '.join([str(a + 1), str(b + 1), str(c + 1)]))
		ones = ones[3:]


def dfs(start, color):
	m[start] = True
	if len(mas[start]) == 0:
		colors[0].append(start + 1)
	else:
		colors[color].append(start + 1)
		for i in mas[start]:
			if not m[i]:
				dfs(i, color)
(n, m) = map(int, input().split())
mas = []
for i in range(n):
	mas.append([])
for i in range(m):
	(a, b) = map(int, input().split())
	mas[a - 1].append(b - 1)
	mas[b - 1].append(a - 1)
m = [False] * n
colors = []
for i in range(n):
	colors.append([])
color = 1
for i in range(n):
	if not m[i]:
		dfs(i, color)
		color += 1
for i in range(1, n):
	if len(colors[i]) > 3:
		print(-1)
		exit()
	elif len(colors[i]) == 3 or len(colors[i]) == 0:
		continue
	elif len(colors[0]) == 0:
		print(-1)
		exit()
	elif len(colors[i]) == 2 and len(colors[0]) > 0:
		colors[i].append(colors[0][0])
		del colors[0][0]
	elif len(colors[i]) == 1 and len(colors[0]) > 1:
		colors[i].append(colors[0][0])
		colors[i].append(colors[0][1])
		del colors[0][0]
		del colors[0][1]
	else:
		print(-1)
		exit()
if len(colors[0]) % 3 == 0:
	for i in range(0, len(colors[0]), 3):
		print(' '.join(map(str, colors[0][i:i + 3])))
	for i in range(1, n):
		if colors[i] != []:
			print(' '.join(map(str, colors[i])))
else:
	print(-1)


from collections import defaultdict
(n, m) = map(int, input().split())
if m == 0:
	for i in range(1, n + 1, 3):
		print(i, i + 1, i + 2)
	exit()
parents = [i for i in range(n + 1)]
ranks = [1 for i in range(n + 1)]

def find(x):
	if parents[x] != x:
		parents[x] = find(parents[x])
	return parents[x]

def union(x, y):
	xs = find(x)
	ys = find(y)
	if xs == ys:
		return
	if ranks[xs] > ranks[ys]:
		parents[ys] = xs
	elif ranks[ys] > ranks[xs]:
		parents[xs] = ys
	else:
		parents[xs] = ys
		ranks[ys] += 1
for _ in range(m):
	(a, b) = map(int, input().split())
	union(a, b)
ps = {}
for i in range(1, n + 1):
	p = find(i)
	if p in ps:
		ps[p].append(i)
	else:
		ps[p] = [i]
check = True
vals = sorted(list(ps.values()), key=lambda x: -len(x))
for i in vals:
	if len(i) > 3:
		check = False
		break
if not check:
	print(-1)
else:
	(cnt1, cnt2, cnt3) = ([], [], [])
	for i in ps:
		if len(ps[i]) == 1:
			cnt1.append(ps[i])
		elif len(ps[i]) == 2:
			cnt2.append(ps[i])
		else:
			cnt3.append(ps[i])
	if (len(cnt1) - len(cnt2)) % 3 != 0 or len(cnt1) < len(cnt2):
		print(-1)
	else:
		for i in cnt3:
			print(*i)
		for i in range(len(cnt2)):
			x = cnt1[i] + cnt2[i]
			print(*x)
		for i in range(len(cnt2), len(cnt1), 3):
			x = cnt1[i] + cnt1[i + 1] + cnt1[i + 2]
			print(*x)


import sys
(n, m) = (int(x) for x in sys.stdin.readline().strip().split(' '))
adj = dict()
for i in range(0, m):
	(a, b) = (int(x) for x in sys.stdin.readline().strip().split(' '))
	if a in adj:
		adj[a].append(b)
	else:
		adj[a] = [b]
visited = set()
groups = []
for i in adj:
	if i in visited:
		continue
	visited.add(i)
	to_visit = adj[i]
	cur_group = [i]
	while len(to_visit) != 0:
		cur = to_visit[-1]
		del to_visit[-1]
		if cur in visited:
			continue
		visited.add(cur)
		cur_group.append(cur)
		if cur not in adj:
			continue
		for x in adj[cur]:
			if x not in visited:
				to_visit.append(x)
	groups.append(cur_group)
need_team = []
for i in range(1, n + 1):
	if i not in visited:
		need_team.append(i)
for g in groups:
	if len(g) > 3:
		print('-1')
		sys.exit()
	while len(g) < 3:
		if len(need_team) == 0:
			print(-1)
			sys.exit()
		g.append(need_team[-1])
		del need_team[-1]
while len(need_team) != 0:
	groups.append([need_team[-1], need_team[-2], need_team[-3]])
	del need_team[-1]
	del need_team[-1]
	del need_team[-1]
for x in groups:
	print(' '.join([str(a) for a in x]))


from sys import stdin as sin

def aint():
	return int(sin.readline())

def amap():
	return map(int, sin.readline().split())

def alist():
	return list(map(int, sin.readline().split()))

def astr():
	return str(sin.readline().split())
from collections import defaultdict as dd
(n, m) = amap()
d = {}
for i in range(1, n + 1):
	d[i] = []
l = []
for i in range(m):
	(a, b) = amap()
	d[a].append(b)
	d[b].append(a)
	l.append(a)
	l.append(b)
l = list(set(l))
p = []
for i in range(1, n + 1):
	if i not in l:
		p.append(i)
p.sort()
f = True
ans = []
while True:
	if l == []:
		break
	q = [l.pop()]
	z = []
	while q:
		b = q.pop()
		z.append(b)
		for i in d[b]:
			if i in l:
				l.remove(i)
				q.append(i)
	if len(z) > 3:
		f = False
		break
	else:
		ans.append(z)
if f:
	if ans == []:
		for i in range(1, n + 1):
			print(i, end=' ')
			if i % 3 == 0:
				print()
	else:
		g = True
		h = []
		for i in ans:
			while len(i) < 3:
				if p == []:
					g = False
					break
				i.append(p.pop())
			if g:
				h.append(i)
		if g == False:
			print(-1)
		else:
			for i in range(0, len(p), 3):
				h.append(p[i:i + 3])
			for i in h:
				print(*i, sep=' ')
else:
	print(-1)


from collections import defaultdict
from itertools import islice

def finding_connected_component(graph, current_student):
	stack = [current_student]
	result = []
	while stack:
		student = stack.pop()
		result.append(student)
		for connected_student in graph[student]:
			if not visited[connected_student]:
				visited[connected_student] = True
				stack.append(connected_student)
	return result
(number_of_students, number_of_pairs) = map(int, input().rstrip().split(' '))
graph = defaultdict(list)
group = defaultdict(list)
visited = [False] * (number_of_students + 1)
isHavingSolution = True
for i in range(number_of_pairs):
	(studentA, studentB) = map(int, input().rstrip().split(' '))
	graph[studentA].append(studentB)
	graph[studentB].append(studentA)
for index in range(1, len(visited)):
	if not visited[index]:
		visited[index] = True
		list_of_connected_students = finding_connected_component(graph, index)
		if len(list_of_connected_students) > 3:
			isHavingSolution = False
			break
		else:
			group[len(list_of_connected_students)].append(list_of_connected_students)
if len(group[2]) > len(group[1]) or not isHavingSolution:
	print(-1)
else:
	while len(group[2]) > 0:
		current_group_2 = group[2].pop()
		current_group_1 = group[1].pop()
		group[3].append(current_group_2 + current_group_1)
	if len(group[1]) % 3 != 0:
		print(-1)
	else:
		index = 0
		group_whateverleft = []
		for element in group[1]:
			group_whateverleft.append(element[0])
		if len(group_whateverleft) % 3 != 0:
			print(-1)
		else:
			last_group_3 = [group_whateverleft[i:i + 3] for i in range(0, len(group_whateverleft), 3)]
			for element in last_group_3:
				group[3].append(element)
			for element in group[3]:
				element = sorted(element, reverse=True)
				print(' '.join(map(str, element)))


(n, m) = map(int, input().split())
ans = []
temp = set()
flag = False
tuples = []
unused = [x for x in range(1, n + 1)]
if m == 0:
	for x in range(n // 3):
		print(n, n - 1, n - 2)
		n -= 3
else:
	for x in range(m):
		(z, y) = map(int, input().split())
		tuples.append((z, y))
	for x in tuples:
		for z in x:
			if z in unused:
				unused[z - 1] = 0
	unused = [x for x in unused if x != 0]
	for x in tuples:
		for i in ans:
			if x[0] in i and x[1] in i:
				flag = True
				break
			if temp.issubset(i):
				temp = set()
		if flag:
			flag = False
			continue
		if len(temp) < 3:
			temp.add(x[0])
			temp.add(x[1])
			if len(temp) > 3:
				if unused:
					temp.remove(x[0])
					temp.remove(x[1])
					temp.add(unused.pop())
					if len(temp) == 3:
						ans.append(temp)
						temp = {x[0], x[1]}
				else:
					print(-1)
					exit()
		elif x[0] in temp and x[1] not in temp or (x[0] not in temp and x[1] in temp):
			print(-1)
			exit()
		else:
			ans.append(temp)
			temp = {x[0], x[1]}
	if 0 < len(temp) < 3:
		while len(temp) < 3:
			if unused:
				temp.add(unused.pop())
			else:
				break
		else:
			ans.append(temp)
	temp = set()
	for x in unused:
		temp.add(x)
		if len(temp) == 3:
			ans.append(temp)
			temp = set()
	for x in ans:
		print(*x)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
int_r = lambda : int(sys.stdin.readline())
str_r = lambda : sys.stdin.readline().strip()
intList_r = lambda : list(map(int, sys.stdin.readline().strip().split()))
strList_r = lambda : list(sys.stdin.readline().strip())
jn = lambda x, l: x.join(map(str, l))
mul = lambda : map(int, sys.stdin.readline().strip().split())
mulf = lambda : map(float, sys.stdin.readline().strip().split())
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
outStr = lambda x: stdout.write(str(x))
mod = 1000000007

def main():
	(n, m) = intList_r()
	sets = []
	for i in range(m):
		(a, b) = intList_r()
		flag = 0
		for se in sets:
			if a in se or b in se:
				se.add(a)
				se.add(b)
				flag = 1
		if not flag:
			newset = set()
			newset.add(a)
			newset.add(b)
			sets.append(newset)
	set4 = [i for i in sets if len(i) > 3]
	set3 = [i for i in sets if len(i) == 3]
	set2 = [i for i in sets if len(i) == 2]
	len4 = sum([len(i) for i in set4])
	len3 = sum([len(i) for i in set3])
	len2 = sum([len(i) for i in set2])
	len1 = n - len2 - len3 - len4
	if int(len4 / 4) > 0 or int(len2 / 2) > len1 or int(len1 - len2 / 2) % 3 != 0:
		outStr(-1)
	else:
		allset = set()
		for i in set3:
			allset = allset.union(i)
		for i in set2:
			allset = allset.union(i)
		for i in set4:
			allset = allset.uniosn(i)
		set1 = set([i + 1 for i in range(n)]) - allset
		set1 = list(set1)
		set2 = list(set2)
		for i in set3:
			outStr(' '.join([str(j) for j in i]))
			outStr('\n')
		i = 0
		for it in set2:
			lissto = [str(j) for j in it]
			lissto.append(str(set1[i]))
			outStr(' '.join(lissto))
			outStr('\n')
			i += 1
		while i < len(set1):
			outStr(' '.join([str(set1[i]), str(set1[i + 1]), str(set1[i + 2])]))
			outStr('\n')
			i += 3
main()


(n, m) = map(int, input().split())
d = [[0 if i != j else 1 for i in range(n)] for j in range(n)]
for i in range(m):
	(u, v) = (int(x) - 1 for x in input().split())
	d[u][v] = d[v][u] = 1
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] |= d[i][k] & d[k][j]
s = set(range(n))
L = [[], [], [], []]
for i in range(n):
	if i in s:
		x = sum(d[i])
		if x > 3:
			print(-1)
			exit()
		L[x].append([j for j in range(n) if d[i][j]])
		for y in L[x][-1]:
			s.remove(y)
if len(L[1]) < len(L[2]) or (len(L[2]) - len(L[1])) % 3:
	print(-1)
	exit()
for x in L[3]:
	print(x[0] + 1, x[1] + 1, x[2] + 1)
for (x, y) in zip(L[1], L[2]):
	print(x[0] + 1, y[0] + 1, y[1] + 1)
for x in range(len(L[2]), len(L[1]), 3):
	print(L[1][x][0] + 1, L[1][x + 1][0] + 1, L[1][x + 2][0] + 1)


def dfs(node):
	global cnt, i
	seen[node] = 1
	cnt += 1
	teams[i].append(node)
	for each in arr[node]:
		if seen[each] == 0:
			dfs(each)
(n, m) = map(int, input().split())
arr = [list() for x in range(49)]
seen = [0] * 49
teams = [list() for k in range(49)]
teams1 = list()
teams2 = list()
for i in range(m):
	(a, b) = map(int, input().split())
	arr[a].append(b)
	arr[b].append(a)
for i in range(1, n + 1):
	if seen[i] == 0:
		cnt = 0
		dfs(i)
		if cnt > 3:
			print(-1)
			break
		if cnt == 2:
			teams2.append(teams[i])
			teams[i] = []
		elif cnt == 1:
			teams1.append(teams[i])
			teams[i] = []
else:
	if len(teams2) > len(teams1):
		print(-1)
	else:
		for each in teams:
			if each:
				(x, y, z) = each
				print(x, y, z)
		for i in range(min(len(teams2), len(teams1))):
			(x, y) = teams2.pop()
			z = teams1.pop()
			print(x, y, z[0])
		cnt = 0
		for i in teams1:
			cnt += 1
			if i:
				print(i[0], end=' ')
			if cnt % 3 == 0:
				print()


(n, m) = list(input().split())
n = int(n)
m = int(m)
l = [{i} for i in range(1, n + 1)]
for i in range(m):
	(a, b) = list(map(int, input().split()))
	fa = False
	fb = False
	for j in l:
		if a in j:
			fa = True
			x = l.index(j)
			l[l.index(j)] |= {a, b}
		if b in j:
			y = l.index(j)
			fb = True
			l[l.index(j)] |= {a, b}
	if fb and fa and (y != x):
		l[x] |= l[y]
		l.remove(l[y])
y = {tuple(i) for i in l}
l = [set(i) for i in y]
ll = [len(i) for i in l]
f = True
for i in ll:
	if i > 3:
		print(-1)
		f = False
		break
if f:
	p = []
	if ll.count(1) < ll.count(2) or (ll.count(1) - ll.count(2)) % 3 != 0:
		print(-1)
	else:
		while len(l) > 0:
			for i in l:
				if len(i) == 3:
					print(' '.join(map(str, i)))
					l.remove(i)
				elif len(i) == 1:
					if len(p) == 2:
						print(' '.join(map(str, p + list(i))))
						p = []
						l.remove(i)
				elif len(i) == 2:
					if len(p) == 0:
						p += list(i)
						l.remove(i)
			if all((len(i) == 1 for i in l)) and len(l) % 3 == 0:
				c = 0
				for i in range(len(l)):
					if c in {0, 1}:
						print(' '.join(map(str, l[i])), end=' ')
						c += 1
					else:
						print(' '.join(map(str, l[i])))
						c = 0
				break


(n, m) = map(int, input().split())
teams = []
adj = {}
for i in range(1, n + 1):
	adj[i] = []
for i in range(m):
	(a, b) = map(int, input().split())
	adj[a].append(b)
	adj[b].append(a)
done = []
ans = 0
for i in sorted([key for key in adj.keys()], key=lambda x: len(adj[x]), reverse=True):
	this_adj = adj[i]
	if i not in done:
		if len(this_adj) > 2:
			ans = -1
			break
		elif len(this_adj) == 0:
			has_team = False
			for team in teams:
				if len(team) < 3:
					team.append(i)
					done.append(i)
					has_team = True
					break
			if not has_team:
				teams.append([])
				teams[len(teams) - 1].append(i)
		else:
			new_team = [i]
			for j in this_adj:
				if j not in done:
					new_team.append(j)
				else:
					ans = -1
					break
			done.extend(new_team)
			teams.append(new_team)
for team in teams:
	if len(team) != 3:
		ans = -1
		break
if ans == -1:
	print(ans)
else:
	for team in teams:
		print(' '.join([str(i) for i in team]))


__author__ = 'Alex'

def union_find(lis):
	lis = map(set, lis)
	unions = []
	for item in lis:
		temp = []
		for s in unions:
			if not s.isdisjoint(item):
				item = s.union(item)
			else:
				temp.append(s)
		temp.append(item)
		unions = temp
	return unions

def run():
	(n, m) = map(int, input().split())
	l = list()
	for i in range(m):
		l.append(list(map(int, input().split())))
	res = union_find(l)
	for i in range(len(res)):
		res[i] = list(res[i])
	for val in res:
		if len(val) > 3:
			print(-1)
			return
	allnums = list()
	for i in range(1, n + 1):
		allnums.append(i)
	for val in res:
		for i in val:
			allnums.remove(i)
	if len(res) > n // 3:
		print(-1)
		return
	while len(res) < n // 3:
		res.append(list())
	for val in res:
		while len(val) < 3:
			val.append(allnums.pop())
	for val in res:
		temp = list(val)
		for i in temp:
			print(i, end=' ')
run()


import sys
sys.setrecursionlimit(100000)
(n, m) = [int(x) for x in sys.stdin.readline().strip().split()]
d = {i: [] for i in range(1, n + 1)}
for j in range(m):
	(a, b) = [int(x) for x in sys.stdin.readline().strip().split()]
	d[a].append(b)
	d[b].append(a)
trip = []
visited = [False] * (n + 1)
flag = False
for i in range(1, n + 1):
	if not visited[i]:
		stack = [i]
		visited[i] = True
		dis = 0
		pair = []
		while stack:
			k = stack.pop()
			if len(d[k]) > 2:
				flag = True
				break
			pair.append(k)
			for j in d[k]:
				if not visited[j]:
					dis += 1
					if dis > 2:
						flag = True
						break
					visited[j] = True
					stack.append(j)
		trip.append(pair)
		if flag:
			break
	if flag:
		break
if flag:
	print(-1)
else:
	pairs = []
	ones = []
	triplets = []
	for i in trip:
		l = len(i)
		if l == 3:
			triplets.append(i)
		elif l == 2:
			pairs.append(i)
		else:
			ones.append(i[0])
	if len(ones) < len(pairs):
		print(-1)
	else:
		for i in triplets:
			print(i[0], i[1], i[2])
		for i in range(len(pairs)):
			print(ones.pop(0), pairs[i][0], pairs[i][1])
		for i in range(0, len(ones), 3):
			print(ones[i], ones[i + 1], ones[i + 2])


(n, m) = map(int, input().split())
(e, f, v) = ([[] for i in range(n + 1)], [False] * (n + 1), [])

def dfs(i, s):
	s.add(i)
	f[i] = True
	for k in e[i]:
		if not f[k]:
			dfs(k, s)
for j in range(m):
	(a, b) = map(int, input().split())
	e[a].append(b)
	e[b].append(a)
for i in range(1, n + 1):
	if e[i] and (not f[i]):
		s = set()
		dfs(i, s)
		if len(s) > 3:
			print(-1)
			exit()
		v.append(list(s))
if len(v) > n // 3:
	print(-1)
	exit()
while len(v) < n // 3:
	v.append([])
vi = 0
for i in range(1, n + 1):
	if not f[i]:
		while len(v[vi]) == 3:
			vi += 1
		v[vi].append(i)
for vi in v:
	print(vi[0], vi[1], vi[2])


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
dict1 = {}
visited = [False] * 50

def dfs(n, lis):
	if visited[n]:
		return
	visited[n] = True
	lis.append(n)
	if n in dict1:
		for x in dict1[n]:
			dfs(x, lis)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	(n, m) = list(map(int, input.readline().split()))
	for i in range(m):
		(a, b) = list(map(int, input.readline().split()))
		if a in dict1:
			dict1[a].append(b)
		else:
			dict1[a] = [b]
		if b in dict1:
			dict1[b].append(a)
		else:
			dict1[b] = [a]
	total = []
	for i in range(1, n + 1):
		lis = []
		dfs(i, lis)
		if len(lis) > 0:
			total.append(lis)
	cantbe = False
	for (i, lis) in enumerate(total):
		if len(lis) > 3:
			cantbe = True
			break
	for (i, lis) in enumerate(total):
		if len(lis) == 2:
			for (j, lis2) in enumerate(total):
				if len(lis2) == 1 and lis2[0] != -1:
					total[i].append(lis2[0])
					total[j][0] = -1
					break
	for (i, lis) in enumerate(total):
		if len(lis) == 1 and lis[0] != -1:
			for (j, lis2) in enumerate(total):
				if i == j:
					continue
				if len(lis2) == 1 and lis2[0] != -1:
					total[i].append(lis2[0])
					total[j][0] = -1
				if len(total[i]) == 3:
					break
	for i in range(len(total) - 1, -1, -1):
		if total[i][0] == -1:
			del total[i]
	if len(total) != n / 3:
		cantbe = True
	if cantbe:
		output = -1
	else:
		output = ''
		for (i, lis) in enumerate(total):
			for (j, x) in enumerate(lis):
				if j + 1 == len(lis):
					output += str(x)
				else:
					output += str(x) + ' '
			if i + 1 != len(total):
				output += '\n'
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


import sys
nm = list(map(int, input().split()))
done = []
visited = []
for i in range(nm[1]):
	pair = input().split()
	if pair[0] not in visited and pair[1] not in visited:
		done.append([pair[0], pair[1]])
		visited.append(pair[0])
		visited.append('-')
		visited.append(pair[1])
	elif pair[0] not in visited:
		temp = visited.index(pair[1])
		if temp < len(visited) - 1 and visited[temp + 1] != '-':
			if temp < len(visited) - 1 and visited[temp + 1] == '|':
				print('-1')
				sys.exit()
			visited.insert(temp + 1, '-')
			visited.insert(temp + 2, pair[0])
		else:
			if temp < len(visited) - 3 and visited[temp + 3] == '|':
				print('-1')
				sys.exit()
			visited.insert(temp + 4, '-')
			visited.insert(temp + 5, pair[0])
	elif pair[1] not in visited:
		temp = visited.index(pair[0])
		if temp < len(visited) - 1 and visited[temp + 1] != '-':
			if temp < len(visited) - 1 and visited[temp + 1] == '|':
				print('-1')
				sys.exit()
			visited.insert(temp + 1, '-')
			visited.insert(temp + 2, pair[1])
		else:
			if temp < len(visited) - 3 and visited[temp + 3] == '|':
				print('-1')
				sys.exit()
			visited.insert(temp + 4, '-')
			visited.insert(temp + 5, pair[1])
k = 0
lis = [[] for i in range(int(nm[0]) // 3 * 2)]
i = 0
while i < len(visited) - 1:
	if visited[i + 1] == '-':
		lis[k].append(visited[i])
		i += 2
	else:
		lis[k].append(visited[i])
		k += 1
		i += 1
i = 0
while i < len(lis):
	if lis[i] == []:
		lis.pop()
		i -= 1
	i += 1
if len(visited) > 2:
	lis[-1].append(visited[-1])
for i in range(nm[0]):
	flag = False
	if str(i + 1) not in visited:
		for e in range(len(lis)):
			if len(lis[e]) < 3:
				lis[e].append(str(i + 1))
				flag = True
				break
		if not flag:
			lis.append([str(i + 1)])
for i in lis:
	if len(i) != 3:
		print(-1)
		sys.exit()
for i in lis:
	print(i[0], i[1], i[2])


(n, m) = [int(x) for x in input().split()]
mat = []
a = []
c = [0] * (n + 1)
for i in range(m):
	mat.append(list(map(int, input().split())))
for i in range(n // 3):
	a.append([])
mat.sort(key=lambda x: x[0])
for i in range(m):
	l = mat[i][0]
	r = mat[i][1]
	c[l] = -1
	c[r] = -1
	f = 0
	for j in range(n // 3):
		for w in range(len(a[j])):
			if a[j][w] == l:
				a[j].append(r)
				c[r] = -1
				f = 1
				break
			if r == a[j][w]:
				a[j].append(l)
				f = 1
				c[l] = -1
				break
		if f:
			break
	if f == 0:
		for j in range(n // 3):
			L = len(a[j])
			if L <= 1:
				a[j].append(l)
				a[j].append(r)
				c[l] = -1
				c[r] = -1
				break
for i in range(n // 3):
	a[i] = list(set(a[i]))
for i in range(1, n + 1):
	if c[i] == 0:
		for j in range(n // 3):
			if len(a[j]) < 3:
				a[j].append(i)
				break
flag = 0
for i in range(n // 3):
	if len(a[i]) != 3:
		flag = 1
		break
if flag:
	print(-1)
else:
	for i in range(n // 3):
		print(*a[i])


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]

def dfs(node):
	stack = [node]
	team = []
	while stack:
		s = stack.pop()
		visited[s] = True
		team.append(s)
		for i in graph[s]:
			if not visited[i]:
				visited[i] = True
				stack.append(i)
	if len(team) > 3:
		return False
	else:
		return team
(n, m) = MAP()
graph = defaultdict(list)
for i in range(m):
	(a, b) = MAP()
	graph[a].append(b)
	graph[b].append(a)
visited = [False] * (n + 1)
ans = []
one = []
two = []
for i in range(1, n + 1):
	if not visited[i]:
		team = dfs(i)
		if not team:
			print('-1')
			exit(0)
		elif len(team) == 3:
			ans.append(team)
		elif len(team) == 2:
			two.append(team)
		else:
			one.extend(team)
while two:
	if one:
		ele = two.pop()
		ele.append(one.pop())
		ans.append(ele)
	else:
		print('-1')
		exit(0)
while one:
	ele = []
	for i in range(3):
		ele.append(one.pop())
	ans.append(ele)
for i in ans:
	print(*i)


(n, m) = map(int, input().split())
s = [-1] * (n + 1)
s[0] = -99
p = 0
for i in range(m):
	(a, b) = map(int, input().split())
	if s[a] == -1:
		if s[b] == -1:
			s[b] = p
			p += 1
		s[a] = s[b]
	elif s[b] == -1:
		s[b] = s[a]
	elif s[b] != s[a]:
		print(-1)
		exit()
if p > n // 3:
	print(-1)
	exit()
t = [[] for i in range(n // 3)]
for i in range(1, len(s)):
	if s[i] != -1:
		t[s[i]].append(i)
	if len(t[s[i]]) > 3:
		print(-1)
		exit()
z = 0
for i in range(len(t)):
	while len(t[i]) < 3:
		while z < len(s) and s[z] != -1:
			z += 1
		if z >= len(s):
			print(-1)
			exit()
		t[i].append(z)
		s[z] = -99
[print(' '.join(map(str, x))) for x in t]


(n, m) = map(int, input().split())
a = list(range(n))
visited = [-1] * n
graph = [[] for _ in ' ' * n]
for _ in range(m):
	(i, j) = map(int, input().split())
	visited[i - 1] = 0
	visited[j - 1] = 0
	graph[i - 1].append(j - 1)
	graph[j - 1].append(i - 1)

def dfs(v, c, s):
	visited[v] = 1
	c[0] += 1
	a.remove(v)
	s.append(v + 1)
	if c[0] > 3:
		print(-1)
		exit(0)
	for i in graph[v]:
		if visited[i] == 0:
			dfs(i, c, s)
ans = []
for i in range(n):
	if visited[i] == 1 or visited[i] == -1:
		continue
	c = [0]
	s = []
	if visited[i] == 0:
		dfs(i, c, s)
	ans.append(s)
ans_ = []
for _ in range(n // 3):
	if ans:
		x = ans.pop(0)
		while len(x) < 3:
			if a:
				x.append(a.pop(0) + 1)
			else:
				print(-1)
				exit(0)
		ans_.append(x)
	elif len(a) >= 3:
		h = []
		for i in range(3):
			h.append(a.pop(0) + 1)
		ans_.append(h)
	else:
		print(-1)
		exit(0)
for i in ans_:
	print(*i)


(n, m) = map(int, input().split())
(p, r) = (list(range(n + 1)), list(({i} for i in range(n + 1))))
(ans, p[0], t) = ([], 1, [[] for i in range(4)])
for i in range(m):
	(a, b) = map(int, input().split())
	(x, y) = (p[a], p[b])
	if x != y:
		for j in r[y]:
			p[j] = x
		r[x] |= r[y]
		if len(r[x]) > 3:
			ans = -1
			break
if not ans:
	for i in set(p):
		t[len(r[i])].append(i)
	if len(t[2]) > len(t[1]):
		ans = -1
	if not ans:
		for i in t[3]:
			ans.append(' '.join((str(j) for j in r[i])))
		for i in range(len(t[2])):
			ans.append(' '.join((str(j) for j in r[t[2][i]] | r[t[1][i]])))
		for i in range(len(t[2]), len(t[1]), 3):
			ans.append(' '.join((str(j) for j in (t[1][k] for k in range(i, i + 3)))))
		ans = '\n'.join(ans)
print(ans)


from collections import defaultdict
par = []
size = []
scc = 0

def build(n):
	global par, size
	for i in range(n):
		par.append(i)
		size.append(1)
	return par

def find(i):
	global par
	if par[i] == i:
		return i
	par[i] = find(par[i])
	return par[i]

def union(a, b):
	global par, scc, size
	p = find(a)
	q = find(b)
	if p == q:
		return
	if size[q] > size[p]:
		temp = q
		q = p
		p = temp
	size[p] += size[q]
	size[q] = 0
	par[q] = par[p]
	scc += 1

def main():
	(n, m) = map(int, input().split(' '))
	par = build(n)
	for i in range(m):
		(a, b) = map(lambda i: int(i) - 1, input().split(' '))
		union(a, b)
	for x in range(5):
		for i in range(n):
			for j in range(i + 1, n):
				if size[i] % 3 == 1 and size[j] % 3 == 2:
					union(i, j)
				elif size[j] % 3 == 1 and size[i] % 3 == 2:
					union(i, j)
	for i in range(n):
		for j in range(i + 1, n):
			for k in range(j + 1, n):
				if size[i] % 3 == 1 and size[j] % 3 == 1 and (size[k] % 3 == 1):
					union(i, j)
					union(i, k)
	m = defaultdict(list)
	for (i, p) in enumerate(par):
		m[p].append(i + 1)
	for k in m:
		if len(m[k]) % 3 != 0 or len(m[k]) > 3:
			print(-1)
			return
	for k in m:
		print(' '.join(map(str, m[k])))
main()


(n, m) = map(int, input().split())
a = [[0 for i in range(n + 1)] for i in range(n + 1)]
for i in range(m):
	(u, v) = map(int, input().split())
	a[u][v] = a[v][u] = 1
s = set()
ans = []

def dfs(u):
	s.add(u)
	ans[-1].append(u)
	for v in range(1, n + 1):
		if a[u][v] and v not in s:
			dfs(v)
for i in range(1, n + 1):
	if i not in s:
		ans.append([])
		dfs(i)
x = list(filter(lambda x: len(x) == 1, ans))
y = list(filter(lambda x: len(x) == 2, ans))
z = list(filter(lambda x: len(x) == 3, ans))
if any(map(lambda x: len(x) > 3, ans)) or len(x) < len(y):
	print(-1)
else:
	for i in z:
		print(i[0], i[1], i[2])
	for i in range(len(y)):
		print(y[i][0], y[i][1], x[i][0])
	for i in range(len(y), len(x), 3):
		print(x[i][0], x[i + 1][0], x[i + 2][0])


from sys import stdin
from collections import defaultdict

def c(d):
	for i in d:
		if len(d[i]) > 2:
			return True
	return False

def t(d):
	res = []
	seen = set()
	for i in d:
		(seen, g) = dfs(i, d, seen, [])
		if g:
			res.append(g)
	return (seen, res)

def dfs(k, d, seen, g):
	if k not in seen:
		g.append(k)
	seen.add(k)
	for i in d[k]:
		if i not in seen:
			dfs(i, d, seen, g)
	return (seen, g)
(n, m) = [int(i) for i in stdin.readline().split()]
d = defaultdict(list)
for i in range(m):
	(a, b) = [int(i) for i in stdin.readline().split()]
	d[a].append(b)
	d[b].append(a)
if c(d):
	print(-1)
else:
	st = {i for i in range(1, n + 1)}
	(seen, res) = t(d)
	v = True
	for i in res:
		if len(i) > 3:
			print(-1)
			v = False
			break
	if v:
		if len(res) > n // 3:
			print(-1)
			v = False
	if v:
		if m == 0:
			for i in range(n // 3):
				res.append([])
		if len(res) < n // 3:
			for i in range(n // 3 - len(res)):
				res.append([])
		lft = list(st - seen)
		g = []
		for i in res:
			while len(i) < 3 and lft:
				i.append(lft.pop())
		for (x, y, z) in res:
			print(x, y, z)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
import heapq
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'
mod = 10 ** 9 + 7

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def ispali(s):
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	(n, m) = mi()
	d = defaultdict(list)
	for i in range(m):
		(u, v) = mi()
		d[u].append(v)
		d[v].append(u)
	vis = [-1] * (n + 1)
	cnt = 1
	groups = defaultdict(list)

	def dfs(i):
		vis[i] = cnt
		con = 1
		for child in d[i]:
			if vis[child] == -1:
				con += dfs(child)
		return con
	f = 0
	for i in range(1, n + 1):
		if vis[i] == -1:
			grup = dfs(i)
			tmp = []
			for j in range(1, n + 1):
				if vis[j] == cnt:
					tmp.append(j)
			if grup == 3:
				groups[3].append(tmp)
			elif grup == 2:
				groups[2].append(tmp)
			elif grup == 1:
				groups[1].append(tmp)
			else:
				f = 1
				break
			cnt += 1
	if len(groups[2]) > len(groups[1]):
		f = 1
	else:
		for i in range(len(groups[2])):
			groups[3].append(groups[2].pop() + groups[1].pop())
	for i in range(len(groups[1]) // 3):
		tmp = []
		for _ in range(3):
			tmp += groups[1].pop()
		groups[3].append(tmp)
	if f:
		print(-1)
	else:
		for i in groups[3]:
			print(*i)
main()


def get(x):
	for i in range(len(a)):
		if x in a[i]:
			return i

def join(x, y):
	if x == y:
		return
	for i in a[y]:
		a[x].add(i)
	a.pop(y)
(n, m) = map(int, input().split())
a = [{i} for i in range(n)]
for i in range(m):
	(k, l) = map(int, input().split())
	k1 = get(k - 1)
	l1 = get(l - 1)
	join(k1, l1)
a1 = []
a2 = []
a3 = []
for i in range(len(a)):
	if len(a[i]) > 3:
		print(-1)
		exit()
	elif len(a[i]) == 3:
		a3.append(a[i])
	elif len(a[i]) == 2:
		a2.append(a[i])
	elif len(a[i]) == 1:
		a1.append(a[i])
	elif len(a[i]) == 0:
		print('What the hell is wrong with you?!')
if len(a2) > len(a1):
	print(-1)
	exit()
else:
	if (len(a1) - len(a2)) % 3 != 0:
		print(-1)
		exit()
	else:
		for i in range(len(a2), len(a1), 3):
			a3.append({a1[i].pop(), a1[i + 1].pop(), a1[i + 2].pop()})
	for i in range(len(a2)):
		a3.append({a1[i].pop(), a2[i].pop(), a2[i].pop()})
	for i in range(len(a3)):
		a3[i] = {a3[i].pop() + 1, a3[i].pop() + 1, a3[i].pop() + 1}
	for i in range(len(a3)):
		print(' '.join(map(str, a3[i])))


(n, m) = [int(i) for i in input().split()]
every = []
for i in range(n):
	every.append(i + 1)
if m == 0:
	for i in range(n // 3):
		print(*[every[0], every[1], every[2]])
		every.pop(0)
		every.pop(0)
		every.pop(0)
w = []
wished = []
for i in range(m):
	s = [int(i) for i in input().split()]
	w.append(s)
	wished += s
trio = []
dub = w
al = []
res = []
to_del = []
for i in range(m):
	for coup in range(m):
		if coup != i:
			if dub[i][0] in dub[coup] or dub[i][1] in dub[coup]:
				trio.append(dub[coup] + dub[i])
				to_del.append(dub[coup])
				to_del.append(dub[i])
dub = [i for i in dub if i not in to_del]
for i in dub:
	for j in trio:
		if i[0] in j and i[1] in j:
			dub.remove(i)
for i in trio:
	for j in trio:
		if set(i) != set(j):
			if len(set(i).intersection(set(j))) != 0:
				print(-1)
				exit(0)
al = list(set(every).difference(wished))
if (len(al) - len(dub)) % 3 != 0 or len(dub) > len(al):
	print(-1)
	exit(0)
else:
	for i in trio:
		a = sorted(list(set(i)))
		if a not in res:
			res.append(a)
	while len(dub) > 0:
		res.append(dub[0] + [al[0]])
		al.pop(0)
		dub.pop(0)
	while len(al) > 0:
		res.append([al[0], al[1], al[2]])
		al.pop(0)
		al.pop(0)
		al.pop(0)
for i in res:
	print(*i)


(n, m) = map(int, input().split())
x = [0] * (n + 1)
t = [0] * (n // 3 + 1)
for i in range(m):
	(a, b) = map(int, input().split())
	if x[a] == 0 and x[b] == 0:
		if t[1:].count(1) > 0 or t[1:].count(0) > 0:
			for i in range(1, n // 3 + 1):
				if t[i] < 2:
					t[i] += 2
					x[a] = x[b] = i
					break
			else:
				print(-1)
				exit()
		else:
			print(-1)
			exit()
	elif x[a] != 0 and x[b] != 0:
		if x[a] != x[b]:
			print(-1)
			exit()
	else:
		if x[a] != 0:
			v = a
			p = b
		else:
			v = b
			p = a
		if t[x[v]] < 3:
			t[x[v]] += 1
			x[p] = x[v]
		else:
			print(-1)
			exit()
for i in range(1, n + 1):
	if x[i] == 0:
		for v in range(1, n // 3 + 1):
			if t[v] < 3:
				t[v] += 1
				x[i] = v
				break
for i in range(1, n // 3 + 1):
	for s in range(1, n + 1):
		if x[s] == i:
			print(s, end=' ')
	print()


from collections import defaultdict
(n, m) = list(map(int, input().split()))
if m == 0:
	for i in range(1, n + 1, 3):
		print(i, i + 1, i + 2)
else:
	graph = defaultdict(list)
	for i in range(m):
		(u, v) = list(map(int, input().split()))
		graph[u].append(v)
		graph[v].append(u)
	visited = [False for i in range(n)]
	d = {}
	for i in range(n):
		if visited[i] == False:
			cur = i + 1
			q = []
			q.append(cur)
			d[cur] = [cur]
			while q != []:
				u = q.pop(0)
				for k in graph[u]:
					if visited[k - 1] == False:
						visited[k - 1] = True
						q.append(k)
						d[cur].append(k)
	for i in d:
		d[i] = list(set(d[i]))
	f = 0
	for i in d:
		if len(d[i]) > 3:
			f = 1
			break
	if f == 1:
		print(-1)
	else:
		(cnt1, cnt2, cnt3) = ([], [], [])
		for i in d:
			if len(d[i]) == 1:
				cnt1.append(d[i])
			elif len(d[i]) == 2:
				cnt2.append(d[i])
			elif len(d[i]) == 3:
				cnt3.append(d[i])
		if (len(cnt1) - len(cnt2)) % 3 != 0 or len(cnt1) < len(cnt2):
			print(-1)
		else:
			for i in cnt3:
				print(*i)
			for i in range(len(cnt2)):
				x = cnt1[i] + cnt2[i]
				print(*x)
			for i in range(len(cnt2), len(cnt1), 3):
				x = cnt1[i] + cnt1[i + 1] + cnt1[i + 2]
				print(*x)


def dfs(node):
	if len(a[node]) > 3:
		print(-1)
		exit()
	temp.append(node)
	vis[node] = 1
	for i in range(len(a[node])):
		if vis[a[node][i]] != 1:
			dfs(a[node][i])
(n, m) = map(int, input().split())
(a, vis) = ([], [0] * 50)
for i in range(50):
	a.append([])
for i in range(m):
	(x, y) = map(int, input().split())
	a[x].append(y)
	a[y].append(x)
ans = []
for i in range(1, n + 1):
	temp = []
	if vis[i] != 1:
		dfs(i)
	if len(temp) > 3:
		print(-1)
		exit()
	else:
		ans.append(temp)
one = []
two = []
three = []
for i in ans:
	if len(i) == 1:
		one.append(i)
	elif len(i) == 2:
		two.append(i)
	elif len(i) == 3:
		three.append(i)
if len(two) > len(one):
	print(-1)
elif (len(one) - len(two)) % 3 != 0:
	print(-1)
else:
	for i in range(len(two)):
		two[i].append(one[i][0])
	for i in three:
		print(*i)
	for i in two:
		print(*i)
	for i in range(len(two), len(one), 3):
		print(one[i][0], one[i + 1][0], one[i + 2][0])


from collections import deque

def check(node, graph, visited):
	arr = []
	q = deque()
	q.append(node)
	visited[node] = True
	arr.append(node)
	while q != deque([]):
		a = q.popleft()
		for i in graph[a]:
			if not visited[i]:
				visited[i] = True
				q.append(i)
				arr.append(i)
	return arr
(n, m) = map(int, input().split())
boo = True
dic = {}
alone = []
graph = [[] for i in range(n + 1)]
for i in range(m):
	(a, b) = map(int, input().split())
	graph[a].append(b)
	graph[b].append(a)
	if a not in dic:
		dic[a] = 1
	if b not in dic:
		dic[b] = 1
for i in range(1, n + 1):
	if i not in dic:
		alone.append(i)
visited = [False for i in range(n + 1)]
ans = []
boo = True
for i in range(1, n + 1):
	if not visited[i]:
		temp = check(i, graph, visited)
		if len(temp) == 1:
			continue
		if len(temp) > 3 or (len(temp) == 2 and len(alone) == 0):
			print(-1)
			boo = False
			break
		elif len(temp) == 3:
			ans.append(tuple(temp))
		else:
			temp.append(alone[-1])
			ans.append(tuple(temp))
			del alone[-1]
if boo:
	if len(alone) % 3 != 0:
		print(-1)
	else:
		for i in ans:
			for j in i:
				print(j, end=' ')
			print()
		while len(alone) > 0:
			for i in range(3):
				print(alone[-1], end=' ')
				del alone[-1]
			print()


(n, m) = map(int, input().split())
d = {i: [] for i in range(1, n // 3 + 1)}
idx = [-1 for i in range(n + 1)]
arr = []
key = 1
for i in range(m):
	(a, b) = map(int, input().split())
	if idx[a] == idx[b] and idx[a] != -1:
		continue
	elif idx[a] == -1 and idx[b] == -1:
		idx[a] = key
		idx[b] = key
		if key > n // 3:
			print(-1)
			exit()
		d[key].append(a)
		d[key].append(b)
		key += 1
	elif idx[a] != -1 and idx[b] == -1:
		idx[b] = idx[a]
		if len(d[idx[a]]) < 3:
			d[idx[a]].append(b)
		else:
			print(-1)
			exit()
	elif idx[a] == -1 and idx[b] != -1:
		idx[a] = idx[b]
		if len(d[idx[b]]) < 3:
			d[idx[b]].append(a)
		else:
			print(-1)
			exit()
	else:
		print(-1)
		exit()
remain = []
for i in range(1, n + 1):
	if idx[i] == -1:
		remain.append(i)
ptr = 0
for i in d:
	if len(d[i]) < 3:
		for j in range(3 - len(d[i])):
			d[i].append(remain[ptr])
			ptr += 1
	print(*d[i])


def dfs_helper(li, n, i, visited):
	count = 1
	l = [i]
	visited[i] = True
	for ele in li[i]:
		if visited[ele] == False:
			(cnt, ele) = dfs_helper(li, n, ele, visited)
			count += cnt
			l.extend(ele)
	return (count, l)

def dfs(li, avail, n):
	visited = [False] * (n + 1)
	big = []
	for i in range(1, n + 1):
		if visited[i] == False:
			(cnt, l) = dfs_helper(li, n, i, visited)
			if cnt > 3:
				print(-1)
				return
			elif cnt == 3:
				big.append(l)
			elif cnt == 2:
				if avail:
					x = avail.pop()
					l.append(x)
					big.append(l)
				else:
					print(-1)
					return
	big.append(avail)
	for ele in big:
		print(*ele)
	return
(n, m) = map(int, input().split())
li = [set() for i in range(n + 1)]
unused = [True] * (n + 1)
if m == 0:
	li = [i for i in range(1, n + 1)]
	print(*li)
else:
	for i in range(m):
		(u, v) = map(int, input().split())
		unused[u] = False
		unused[v] = False
		li[u].add(v)
		li[v].add(u)
	avail = []
	for i in range(1, n + 1):
		if unused[i] == True:
			avail.append(i)
	dfs(li, avail, n)


import sys
sys.setrecursionlimit(9000)
komsu = [0] * 48

def bul(x):
	if komsu[x] == x:
		return x
	komsu[x] = bul(komsu[x])
	return komsu[x]

def yerlestir(x, y):
	x = bul(x)
	y = bul(y)
	if x == y:
		return
	komsu[x] = y

def main():
	(n, m) = map(int, sys.stdin.readline().strip().split(' '))
	(x, y, z, p, r) = (0, 0, 0, 0, 0)
	a = [0] * 48
	b = [[0 for _ in range(3)] for __ in range(16)]
	c = [0] * 48
	for i in range(n):
		komsu[i] = i
	for i in range(m):
		(l, o) = map(int, sys.stdin.readline().strip().split(' '))
		yerlestir(l - 1, o - 1)
	for i in range(n):
		c[bul(i)] += 1
	for i in range(n):
		if c[i] > 3:
			print(-1)
			exit()
		if c[i] == 1:
			x += 1
		elif c[i] == 2:
			y += 1
		elif c[i] == 3:
			z += 1
	if y + z > n / 3:
		print(-1)
		exit()
	for i in range(n):
		if c[i] == 3:
			q = 0
			for j in range(n):
				if bul(j) == i:
					a[j] = 1
					b[p][q] = j + 1
					q += 1
			p += 1
	for i in range(n):
		if c[i] == 2:
			q = 0
			for j in range(n):
				if bul(j) == i:
					a[j] = 1
					b[p][q] = j + 1
					q += 1
			for j in range(n):
				if a[j] == 0 and c[bul(j)] == 1:
					a[j] = 1
					b[p][q] = j + 1
					q += 1
					break
			p += 1
	for i in range(n):
		if a[i] == 0:
			b[p][r] = i + 1
			r += 1
			if r == 3:
				p += 1
				r = 0
	for i in range(n // 3):
		print(b[i][0], b[i][1], b[i][2])
main()


(n, m) = [int(i) for i in input().split()]
students = {i: -1 for i in range(1, n + 1)}
teams = []
have_solution = True
for i in range(m):
	(a, b) = [int(i) for i in input().split()]
	if students[a] == -1 and students[b] == -1:
		teams.append(set([a, b]))
		students[a] = students[b] = len(teams) - 1
	elif students[a] != -1 and students[b] != -1 and (students[a] != students[b]):
		have_solution = False
		break
	else:
		students[a] = students[b] = max(students[a], students[b])
		teams[students[a]].add(a)
		teams[students[a]].add(b)
		if len(teams[students[a]]) > 3:
			have_solution = False
			break
if not have_solution:
	print(-1)
else:
	result = ''
	not_teams = []
	for (key, value) in students.items():
		if value == -1:
			not_teams.append(key)
	for i in teams:
		tmp = list(i)
		if len(i) != 3:
			if not_teams:
				tmp.append(not_teams.pop())
			else:
				have_solution = False
				break
		tmp.sort()
		result += ' '.join((str(j) for j in tmp))
		result += '\n'
	while not_teams:
		result += ' '.join((str(not_teams.pop()) for j in range(3)))
		result += '\n'
	if have_solution:
		print(result)
	else:
		print(-1)


import sys
from collections import defaultdict

def make_graph():
	return defaultdict(make_graph)

def return_pair_sets(person, graph, used):
	if person in used:
		return []
	used[person] = True
	if person not in graph:
		return [person]
	sets = [person]
	for friend in graph[person]:
		sets += return_pair_sets(friend, graph, used)
	return sets
values = [int(x) for x in sys.stdin.readline().split()]
(n_kids, n_pairings) = (values[0], values[1])
graph = make_graph()
for i in range(n_pairings):
	pair_values = [int(x) for x in sys.stdin.readline().split()]
	(first, second) = (pair_values[0], pair_values[1])
	if first not in graph:
		graph[first] = []
	if second not in graph:
		graph[second] = []
	graph[first].append(second)
	graph[second].append(first)
used = {}
sets_of_three = []
for i in range(1, n_kids + 1):
	if i not in used:
		group = return_pair_sets(i, graph, used)
		if len(group) < 3:
			did_insert = False
			for j in sets_of_three:
				if len(j) + len(group) == 3:
					j += group
					did_insert = True
					break
			if not did_insert:
				sets_of_three.append(group)
		else:
			sets_of_three.append(group)
ones = [x[0] for x in sets_of_three if len(x) == 1]
if len(ones) % 3 != 0:
	print('-1')
	sys.exit(0)
for i in range(len(ones) // 3):
	cur_set = []
	index = i * 3
	cur_set.append(ones[index])
	cur_set.append(ones[index + 1])
	cur_set.append(ones[index + 2])
	sets_of_three.append(cur_set)
for i in sets_of_three:
	if len(i) != 3 and len(i) != 1:
		print('-1')
		sys.exit(0)
for i in sets_of_three:
	if len(i) == 3:
		sys.stdout.write(str(i[0]) + ' ' + str(i[1]) + ' ' + str(i[2]) + '\n')


(n, m) = list(map(int, input().split()))
if m == 0:
	for nnnn in range(1, n + 1, 3):
		print(nnnn, nnnn + 1, nnnn + 2)
else:
	vis = [False] * n
	vec = []
	arr = [k for k in range(1, n + 1)]
	arr2 = {}
	for _ in range(m):
		(a, b) = list(map(int, input().split()))
		if a in arr2:
			arr2[a].append(b)
		else:
			arr2[a] = [b]
		if b in arr2:
			arr2[b].append(a)
		else:
			arr2[b] = [a]
		if a in arr:
			arr.remove(a)
		if b in arr:
			arr.remove(b)
	flag = 0
	curr = 0
	ex = 0
	for kkkkk in arr2:
		if len(arr2[kkkkk]) > 2:
			print(-1)
			exit()
	while ex < n // 3:
		ddg = []
		ex += 1
		for kkk in arr2:
			for fuc in arr2[kkk]:
				ddg.append(fuc)
			ddg.append(kkk)
			if len(ddg) > 3:
				print(-1)
				exit()
			elif len(ddg) == 2 and len(arr) != 0:
				ddg.append(arr[0])
				arr.pop(0)
			elif len(ddg) == 2 and len(arr) == 0:
				print(-1)
				exit()
			for jj in ddg:
				if jj in arr2:
					arr2.pop(jj)
			flag += 1
			vec.append(ddg)
			break
	for km in vec:
		print(*km)
	if len(arr) > 0:
		if len(arr) % 3 == 0:
			for nmnm in range(0, len(arr), 3):
				print(arr[nmnm], arr[nmnm + 1], arr[nmnm + 2])
		else:
			print(-1)
			exit()


(n, m) = list(map(int, input().split()))
la = []
sa = 0
na = 0
for x in range(m):
	(s, j) = list(map(int, input().split()))
	for item in la:
		if s in item and j in item:
			sa = 1
			pass
		elif len(item) == 3 and s in item or j in item:
			na = 1
			break
		elif s not in item and j not in item:
			pass
		else:
			sa = 1
			if s in item:
				item.append(j)
			else:
				item.append(s)
			break
	if na == 1:
		print(-1)
		break
	elif sa == 0:
		t = [s, j]
		la.append(t)
	sa = 0
zim = []
dim = list(range(1, n + 1))
sima = []
nol = 0
for item in la:
	for x in item:
		zim.append(x)
if m == 0:
	kam = list(range(1, n + 1))
	y = 0
	for x in range(len(kam) // 3):
		print(*kam[y:y + 3])
		y += 3
elif na != 1:
	for item in la:
		nid = 1
		if len(item) == 3:
			pass
		else:
			for x in dim:
				if x not in zim:
					item.append(x)
					nid = 0
					zim.append(x)
					break
			if nid == 1:
				print(-1)
				nol = 1
				break
	if nol != 1:
		for x in dim:
			if x not in zim:
				sima.append(x)
		y = 0
		for x in range(len(sima) // 3):
			print(*sima[y:y + 3])
			y += 3
		for ite in la:
			print(*ite)


import sys
from math import sqrt, gcd, ceil, log
from collections import defaultdict, Counter, deque
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def main():
	(n, m) = read()
	adj = [[] for i in range(n + 1)]
	visited = [0] * (n + 1)
	for i in range(m):
		(u, v) = read()
		adj[u].append(v)
		adj[v].append(u)

	def dfs(node):
		stack = [node]
		team = []
		while stack:
			par = stack.pop()
			visited[par] = 1
			team.append(par)
			for child in adj[par]:
				if not visited[child]:
					visited[child] = 1
					stack.append(child)
		if len(team) > 3:
			return
		else:
			return team
	ans = []
	two = []
	one = []
	for i in range(1, n + 1):
		if not visited[i]:
			team = dfs(i)
			if not team:
				print(-1)
				exit()
			elif len(team) == 3:
				ans.append(team)
			elif len(team) == 2:
				two.append(team)
			else:
				one.extend(team)
	while two:
		if one:
			elem = two.pop()
			elem.append(one.pop())
			ans.append(elem)
		else:
			print(-1)
			exit()
	while one:
		elem = []
		for i in range(3):
			elem.append(one.pop())
		ans.append(elem)
	for i in ans:
		print(*i)
main()


N = 64
c = set()

def dfs(u):
	x = [u]
	c.add(u)
	for vertex in range(1, n + 1):
		if g[u][vertex] and vertex not in c:
			x += dfs(vertex)
	return x
(n, m) = map(int, input().split())
g = [[0] * N for __ in range(N)]
for __ in range(m):
	(u, vertex) = map(int, input().split())
	g[u][vertex] = g[vertex][u] = True
vertex = [[], [], [], []]
for i in range(1, n + 1):
	if i not in c:
		x = dfs(i)
		l = len(x)
		if l > 3:
			print(-1)
			exit()
		vertex[l] += [x]
for x in vertex[2]:
	if vertex[1]:
		vertex[3] += [x + vertex[1].pop()]
	else:
		print(-1)
		exit()
if len(vertex[1]) % 3:
	print(-1)
	exit()
while vertex[1]:
	vertex[3] += [vertex[1].pop() + vertex[1].pop() + vertex[1].pop()]
for x in vertex[3]:
	print(' '.join(map(str, x)))


(n, m) = map(int, input().split())
edges = [[*map(int, input().split())] for _ in range(m)]
parent = [*range(n)]
for (i, j) in edges:
	if parent[i - 1] != parent[j - 1]:
		(old, new) = (parent[i - 1], parent[j - 1])
		for j in range(n):
			if parent[j] == old:
				parent[j] = new
d = {}
for i in range(n):
	if parent[i] in d:
		d[parent[i]].append(i + 1)
	else:
		d[parent[i]] = [i + 1]
d = [i for i in d.values()]
d.sort(key=lambda x: -len(x))
pl = [len(i) for i in d]
(th, tw, o) = ([], [], [])
for i in d:
	if len(i) == 3:
		th.append(i)
	elif len(i) == 2:
		tw.append(i)
	elif len(i) == 1:
		o.append(i)
	elif len(i) > 3:
		print(-1)
		exit()
if len(tw) > len(o):
	print(-1)
	exit()
for i in th:
	print(*i)
for (i, j) in zip(tw, o):
	print(i[0], i[1], j[0])
for i in range(len(tw), len(o), 3):
	print(o[i][0], o[i + 1][0], o[i + 2][0])


def whichList(player, teams):
	for x in range(len(teams)):
		if player in teams[x]:
			return x
	return -1

def printTeam(team):
	team = sorted(team, reverse=True)
	if len(team) == 3:
		print('%d %d %d' % (team[0], team[1], team[2]))
	else:
		print(team)

def main():
	s = input().split()
	n = int(s[0])
	m = int(s[1])
	base = list(range(1, n + 1))
	teams = []
	for i in range(m):
		tmp = input().split()
		p1 = int(tmp[0])
		p2 = int(tmp[1])
		if p1 in base and p2 in base:
			teams.append([p1, p2])
			base.remove(p1)
			base.remove(p2)
		elif p1 in base:
			i = whichList(p2, teams)
			if i >= 0:
				teams[i].append(p1)
				base.remove(p1)
			else:
				print('-1')
				return
		elif p2 in base:
			i = whichList(p1, teams)
			if i >= 0:
				teams[i].append(p2)
				base.remove(p2)
			else:
				print('-1')
				return
	if m == 0:
		while len(base) >= 3:
			teams.append(base[:3])
			base = base[3:]
	if len(base) > 0:
		for p in base[:]:
			for t in teams:
				if len(t) > 3:
					print('-1')
					return
				if len(t) < 3:
					t.append(p)
					base.remove(p)
					break
	if len(base) % 3 != 0:
		print('-1')
		return
	while len(base) >= 3:
		teams.append(base[:3])
		base = base[3:]
	for t in teams:
		if len(t) != 3:
			print('-1')
			return
	for t in teams:
		printTeam(t)
main()


def dfs(student, req):
	global three
	three.add(student)
	visited.add(student)
	if req[student] != None:
		for x in req[student]:
			if x not in visited:
				dfs(x, req)
(n, m) = map(int, input().split(' '))
three = set()
(flag, used, visited, req, threes) = (True, set(), set(), [None] * (n + 1), [])
for x in range(m):
	(student1, student2) = map(int, input().split(' '))
	if req[student1] == None:
		req[student1] = [student2]
	else:
		req[student1].append(student2)
for x in range(1, n + 1):
	if req[x] != None and x not in visited:
		dfs(x, req)
		if len(three) > 3:
			flag = False
			break
		threes.append(list(three))
		three.clear()
if flag == False:
	print(-1)
else:
	it = 1
	for i in range(len(threes)):
		if len(threes[i]) < 3:
			for x in range(it, n + 1):
				if x not in visited:
					threes[i].append(x)
					visited.add(x)
					it = x + 1
					break
		if len(threes[i]) < 3:
			flag = False
			print(-1)
			break
	if flag:
		for group in threes:
			print(*group)
if len(visited) < n and flag == True:
	temp = []
	for x in range(1, n + 1):
		if x not in visited:
			temp.append(x)
		if len(temp) == 3:
			print(*temp)
			temp.clear()


(n, m) = map(int, input().split())
visited = [False for i in range(n)]
g = [[] for _ in range(n)]
for _ in range(m):
	(a, b) = map(int, input().split())
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)

def dfs(x, s):
	s.add(x)
	visited[x] = True
	for adj in g[x]:
		if not visited[adj]:
			dfs(adj, s)
groups = []
for i in range(n):
	if g[i] and (not visited[i]):
		s = set()
		dfs(i, s)
		if len(s) > 3:
			print(-1)
			exit()
		groups.append(list(s))
if len(groups) > n // 3:
	print(-1)
	exit()
while len(groups) < n // 3:
	groups.append([])
current_v = 0
for i in range(n):
	if not visited[i]:
		while len(groups[current_v]) == 3:
			current_v += 1
		groups[current_v].append(i)
for g in groups:
	print(' '.join(map(lambda x: str(x + 1), g)))


def f():
	(n, m) = map(int, input().split())
	(p, r) = (list(range(n + 1)), list(({i} for i in range(n + 1))))
	(q, p[0], t) = (True, 1, [[] for i in range(4)])
	for i in range(m):
		(a, b) = map(int, input().split())
		(x, y) = (p[a], p[b])
		if x != y:
			for j in r[y]:
				p[j] = x
			r[x] |= r[y]
			if len(r[x]) > 3:
				print(-1)
				return
	for i in set(p):
		t[len(r[i])].append(i)
	if len(t[2]) > len(t[1]):
		print(-1)
		return
	print('\n'.join([' '.join((str(j) for j in r[i])) for i in t[3]] + [' '.join((str(j) for j in r[t[2][i]] | r[t[1][i]])) for i in range(len(t[2]))] + [' '.join((str(j) for j in (t[1][k] for k in range(i, i + 3)))) for i in range(len(t[2]), len(t[1]), 3)]))
f()


s = input()
k = s.split()
n = int(k[0])
m = int(k[1])
t = []
tab = []
l = [0]
for i in range(m):
	s = input()
	f = s.split()
	t.append(int(f[0]))
	tab.append(int(f[1]))
p = 0
v = []
for i in range(1, n + 1):
	s = set()
	for j in range(len(tab)):
		if tab[j] == i or t[j] == i:
			s.add(tab[j])
			s.add(t[j])
	if list(s) == []:
		v.append(i)
	l.append(list(s))
verif = True
cont = 0
h = set()
g = set()
for i in range(1, len(l)):
	if len(l[i]) > 3:
		verif = False
	elif len(l[i]) == 3:
		verif = set(l[l[i][0]]) == set(l[l[i][1]]) and set(l[l[i][0]]) == set(l[l[i][2]])
		l[i] = sorted(l[i])
		g.add(tuple(l[i]))
	elif len(l[i]) == 2:
		verif = set(l[l[i][0]]) == set(l[l[i][1]])
		cont = cont + 1
		l[i] = sorted(l[i])
		h.add(tuple(l[i]))
	if verif == False:
		break
if verif == False or len(v) - len(h) < 0:
	print(-1)
elif (len(v) - len(h)) % 3 != 0:
	print(-1)
else:
	g = list(g)
	for i in range(len(g)):
		f = list(g[i])
		print(f[0], f[1], f[2])
	h = list(h)
	for i in range(len(h)):
		f = list(h[i])
		print(f[0], f[1], v[i])
	for i in range(len(h), len(v) - 2, 3):
		print(v[i], v[i + 1], v[i + 2])


import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def seive(n):
	primes = [True] * (n + 1)
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	return primes

def factors(n):
	factors = []
	x = 2
	while x * x <= n:
		while n % x == 0:
			factors.append(x)
			n //= x
		if n > 1:
			factors.append(x)
	return factors

def main():
	try:
		(n, m) = invr()
		a = list(range(n))
		visited = [-1] * n
		adj = [[] for _ in range(n)]
		for _ in range(m):
			(i, j) = invr()
			(i, j) = (i - 1, j - 1)
			(visited[i], visited[j]) = (0, 0)
			adj[j].append(i)
			adj[i].append(j)

		def dfs(v, c, s):
			visited[v] = 1
			c[0] += 1
			a.remove(v)
			s.append(v + 1)
			if c[0] > 3:
				print('-1')
				exit()
			for i in adj[v]:
				if visited[i] == 0:
					dfs(i, c, s)
		ans = []
		for i in range(n):
			if visited[i] == 1 or visited[i] == -1:
				continue
			c = [0]
			s = []
			if visited[i] == 0:
				dfs(i, c, s)
			ans.append(s)
		ans_ = []
		for i in range(n // 3):
			if ans:
				x = ans.pop()
				while len(x) < 3:
					if a:
						x.append(a.pop() + 1)
					else:
						print('-1')
						return
				ans_.append(x)
			elif len(a) >= 3:
				h = []
				for i in range(3):
					h.append(a.pop() + 1)
				ans_.append(h)
			else:
				print('-1')
				return
		for i in ans_:
			print(*i)
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from random import randint
sys.setrecursionlimit(2030)

def rec(pos, tri):
	global deep
	if type(g[pos]) is int and g[pos] < 0:
		if g[pos] == -tri:
			return True
		else:
			return False
	deep += 1
	if deep > 3:
		return False
	out[tri].append(pos)
	v = g[pos]
	g[pos] = -tri
	for i in v:
		if not rec(i, tri):
			return False
	return True
(n, m) = map(int, input().strip().split())
for k in range(1):
	impossible = False
	g = [[] for i in range(n)]
	out = {}
	for i in range(m):
		(a, b) = (randint(1, n), randint(1, n))
		(a, b) = map(int, input().strip().split())
		if a != b:
			a -= 1
			b -= 1
			if b not in g[a]:
				g[a].append(b)
			if a not in g[b]:
				g[b].append(a)
			if len(g[a]) > 2 or len(g[b]) > 2:
				impossible = True
	tri = 1
	alone = []
	for i in range(n):
		if type(g[i]) is list:
			if len(g[i]) == 0:
				alone.append(i)
				g[i] = -99999
			else:
				out[tri] = []
				deep = 0
				if not rec(i, tri):
					impossible = True
					break
				tri += 1
		if tri - 1 > n / 3:
			impossible = True
			break
	if impossible:
		print(-1)
	else:
		counter = 0
		for d in out.items():
			v = d[1]
			N = len(v)
			for i in range(N):
				if i == 2:
					print(v[i] + 1, end='\n')
				else:
					print(v[i] + 1, end=' ')
				counter += 1
			for i in range(3 - N):
				if i == 2 - N:
					print(alone.pop() + 1, end='\n')
				else:
					print(alone.pop() + 1, end=' ')
				counter += 1
		while counter < n:
			for i in range(3):
				if i == 2:
					print(alone.pop() + 1, end='\n')
				else:
					print(alone.pop() + 1, end=' ')
				counter += 1


(m, n) = list(map(int, input().split()))
nums = set((str(i) for i in range(1, m + 1)))
teams = {}
maps = {}
for _ in range(n):
	(base, target) = list(input().split())
	if base in maps:
		flag = False
	else:
		flag = True
	if flag:
		teams[base] = set([base, target])
		if base in nums:
			nums.remove(base)
		if target in nums:
			nums.remove(target)
		maps[base] = base
		while maps[base] != base:
			base = maps[base]
		maps[target] = base
	else:
		if target in nums:
			nums.remove(target)
		if not flag:
			teams[maps[base]].add(target)
			if len(teams[maps[base]]) > 3:
				print(-1)
				exit()
		else:
			teams[base].add(target)
			if len(teams[base]) > 3:
				print(-1)
				exit()
		while maps[base] != base:
			base = maps[base]
		maps[target] = base
queue = []
for team in teams.values():
	try:
		if len(team) != 3:
			queue.append(' '.join(list(team) + [nums.pop()]))
		else:
			queue.append(' '.join(team))
	except:
		print(-1)
		exit()
for i in range(len(nums) // 3):
	try:
		queue.append(str(nums.pop() + ' ' + nums.pop() + ' ' + nums.pop()))
	except:
		print(-1)
		exit()
for i in queue:
	print(i)


(n, m) = map(int, input().split())
l = []
c = [0] * (n // 3 + 1)
k = [[-1] * 3 for i in range(n // 3 + 1)]
a = [-1] * (n + 1)
for i in range(m):
	l += [list(map(int, input().split()))]
for i in l:
	if i[1] < i[0]:
		(i[0], i[1]) = (i[1], i[0])
l.sort()
k1 = 1
for i in range(m):
	if a[l[i][0]] + a[l[i][1]] == -2:
		if k1 > n // 3:
			print('-1')
			exit()
		k[k1][0] = l[i][0]
		k[k1][1] = l[i][1]
		a[l[i][0]] = k1
		a[l[i][1]] = k1
		c[k1] = 2
		k1 += 1
	elif a[l[i][0]] != -1 and a[l[i][1]] == -1:
		t = a[l[i][0]]
		if c[t] < 3:
			k[t][2] = l[i][1]
			a[l[i][1]] = t
			c[t] = 3
		else:
			print('-1')
			exit()
	elif a[l[i][0]] == -1 and a[l[i][1]] != -1:
		t = a[l[i][1]]
		if c[t] < 3:
			k[t][2] = l[i][0]
			a[l[i][0]] = t
			c[t] = 3
		else:
			print('-1')
			exit()
o = []
for i in range(1, n + 1):
	if a[i] == -1:
		o += [i]
o1 = len(o)
s1 = len(k)
u = 0
for i in range(1, s1):
	for j in range(3):
		if k[i][j] == -1:
			k[i][j] = o[u]
			u += 1
k = k[1:]
for i in k:
	print(i[0], i[1], i[2])