After a successful field test, Heidi is considering deploying a trap along some Corridor, possibly not the first one. She wants to avoid meeting the Daleks inside the Time Vortex, so for abundance of caution she considers placing the traps only along those Corridors that are not going to be used according to the current Daleks' plan – which is to use a minimum spanning tree of Corridors. Heidi knows that all energy requirements for different Corridors are now different, and that the Daleks have a single unique plan which they are intending to use.

Your task is to calculate the number E_{max}(c), which is defined in the same way as in the easy version – i.e., the largest e ≤ 10^9 such that if we changed the energy of corridor c to e, the Daleks might use it – but now for every corridor that Heidi considers. 

Input

The first line: number n of destinations, number m of Time Corridors (2 ≤ n ≤ 10^5, n - 1 ≤ m ≤ 10^6). The next m lines: destinations a, b and energy e (1 ≤ a, b ≤ n, a ≠ b, 0 ≤ e ≤ 10^9).

No pair \\{a, b\} will repeat. The graph is guaranteed to be connected. All energy requirements e are distinct.

Output

Output m-(n-1) lines, each containing one integer: E_{max}(c_i) for the i-th Corridor c_i from the input that is not part of the current Daleks' plan (minimum spanning tree).

Example

Input

3 3
1 2 8
2 3 3
3 1 4


Output

4

Note

If m = n-1, then you need not output anything.

Source: codeforces | Difficulty: HARD
Tags: Tree algorithms

Graph algorithms

Graph traversal

Shortest paths


Solutions:
def naiveSolve():
	return
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

class UnionFind:

	def __init__(self, n):
		self.parent = list(range(n))

	def find(self, a):
		acopy = a
		while a != self.parent[a]:
			a = self.parent[a]
		while acopy != a:
			(self.parent[acopy], acopy) = (a, self.parent[acopy])
		return a

	def union(self, a, b):
		self.parent[self.find(b)] = self.find(a)

def main():
	(n, m) = readIntArr()
	a = []
	b = []
	c = []
	for _ in range(m):
		(aa, bb, cc) = readIntArr()
		a.append(aa)
		b.append(bb)
		c.append(cc)
	idxes = list(range(m))
	idxes.sort(key=lambda i: c[i])
	uf = UnionFind(n + 1)
	inA = []
	inB = []
	inC = []
	outA = []
	outB = []
	outIdxes = []
	for i in range(m):
		aa = a[idxes[i]]
		bb = b[idxes[i]]
		cc = c[idxes[i]]
		if uf.find(aa) != uf.find(bb):
			uf.union(aa, bb)
			inA.append(aa)
			inB.append(bb)
			inC.append(cc)
		else:
			outIdxes.append(idxes[i])
	outIdxes.sort()
	for i in outIdxes:
		outA.append(a[i])
		outB.append(b[i])
	adj = [[] for _ in range(n + 1)]
	assert len(inA) == n - 1
	for i in range(n - 1):
		(u, v, c) = (inA[i], inB[i], inC[i])
		adj[u].append((v, c))
		adj[v].append((u, c))

	@bootstrap
	def dfs(node, p, cost):
		up[node][0] = p
		maxEdge[node][0] = cost
		tin[node] = time[0]
		time[0] += 1
		for (v, c) in adj[node]:
			if v != p:
				yield dfs(v, node, c)
		tout[node] = time[0]
		time[0] += 1
		yield None
	time = [0]
	tin = [-1] * (n + 1)
	tout = [-1] * (n + 1)
	maxPow = 0
	while pow(2, maxPow) < n:
		maxPow += 1
	maxPow += 1
	up = [[-1 for _ in range(maxPow)] for __ in range(n + 1)]
	maxEdge = [[-inf for _ in range(maxPow)] for __ in range(n + 1)]
	dfs(1, -1, -inf)
	for i in range(1, maxPow):
		for u in range(2, n + 1):
			if up[u][i - 1] == -1 or up[up[u][i - 1]][i - 1] == -1:
				continue
			up[u][i] = up[up[u][i - 1]][i - 1]
			maxEdge[u][i] = max(maxEdge[u][i - 1], maxEdge[up[u][i - 1]][i - 1])

	def isAncestor(u, v):
		return tin[u] <= tin[v] and tout[u] >= tout[v]

	def findMaxEdgeValue(u, v):
		res = 0
		if not isAncestor(u, v):
			u2 = u
			for i in range(maxPow - 1, -1, -1):
				if up[u2][i] != -1 and (not isAncestor(up[u2][i], v)):
					res = max(res, maxEdge[u2][i])
					u2 = up[u2][i]
			res = max(res, maxEdge[u2][0])
		if not isAncestor(v, u):
			v2 = v
			while not isAncestor(up[v2][0], u):
				for i in range(maxPow - 1, -1, -1):
					if up[v2][i] != -1 and (not isAncestor(up[v2][i], u)):
						res = max(res, maxEdge[v2][i])
						v2 = up[v2][i]
			res = max(res, maxEdge[v2][0])
		return res
	ans = []
	for i in range(len(outA)):
		(u, v) = (outA[i], outB[i])
		ans.append(findMaxEdgeValue(u, v))
	multiLineArrayPrint(ans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(r):
	print('? {}'.format(r))
	sys.stdout.flush()
	return readIntArr()

def answerInteractive(adj, n):
	print('!')
	for u in range(1, n + 1):
		for v in adj[u]:
			if v > u:
				print('{} {}'.format(u, v))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
from math import gcd, floor, ceil
for _abc in range(1):
	main()