
from collections import deque



def      __UpperCAmelCase    (  snake_case_							:		Dict				):



	'''simple docstring'''



	return input(snake_case_				).strip().split(" "				)



def      __UpperCAmelCase    (  snake_case_							:		int				,					snake_case_							:		int				):



	'''simple docstring'''



	UpperCAmelCase:					Dict 						=     {}
	for i in range(snake_case_				):
		UpperCAmelCase:					int 						=     []

	for e in range(snake_case_				):
		UpperCAmelCase						,   UpperCAmelCase:					Tuple 						=     (int(snake_case_				) for i in _input(F'Edge {e + 1}: <node1> <node2> '				))
		graph[x].append(snake_case_				)
	return graph



def      __UpperCAmelCase    (  snake_case_							:		int				,					snake_case_							:		int				):



	'''simple docstring'''



	UpperCAmelCase:					str 						=     {}
	for i in range(snake_case_				):
		UpperCAmelCase:					Any 						=     []

	for e in range(snake_case_				):
		UpperCAmelCase						,   UpperCAmelCase:					Optional[int] 						=     (int(snake_case_				) for i in _input(F'Edge {e + 1}: <node1> <node2> '				))
		graph[x].append(snake_case_				)
		graph[y].append(snake_case_				)
	return graph



def      __UpperCAmelCase    (  snake_case_							:		int				,					snake_case_							:		int				):



	'''simple docstring'''



	UpperCAmelCase:					str 						=     {}
	for i in range(snake_case_				):
		UpperCAmelCase:					Optional[int] 						=     []

	for e in range(snake_case_				):
		UpperCAmelCase						,   UpperCAmelCase						,   UpperCAmelCase:					Dict 						=     (int(snake_case_				) for i in _input(F'Edge {e + 1}: <node1> <node2> <weight> '				))
		graph[x].append((y, w)				)
		graph[y].append((x, w)				)
	return graph


if __name__ == "__main__":
	snake_case_      ,							snake_case_      :					Any				      =     (int(i) for i in _input('Number of nodes and edges: '))

	snake_case_      :					Tuple				      =     int(
	    _input(
	        'Press 1 or 2 or 3 \n'
	        '1. Unweighted directed \n'
	        '2. Unweighted undirected \n'
	        '3. Weighted undirected \n'
	    )[0]
	)

	snake_case_      :					str				      =     {
	    1: initialize_unweighted_directed_graph,
	    2: initialize_unweighted_undirected_graph,
	    3: initialize_weighted_undirected_graph,
	}[graph_choice](n, m)



def      __UpperCAmelCase    (  snake_case_							:		Tuple				,					snake_case_							:		Tuple				):



	'''simple docstring'''



	UpperCAmelCase						,   UpperCAmelCase:					Union[str, Any] 						=     {s}, [s]
	print(snake_case_				)
	while _s:
		UpperCAmelCase:					List[str] 						=     0
		for i in g[_s[-1]]:
			if i not in vis:
				_s.append(snake_case_				)
				vis.add(snake_case_				)
				UpperCAmelCase:					Any 						=     1
				print(snake_case_				)
				break
		if not flag:
			_s.pop()



def      __UpperCAmelCase    (  snake_case_							:		Tuple				,					snake_case_							:		int				):



	'''simple docstring'''



	UpperCAmelCase						,   UpperCAmelCase:					Union[str, Any] 						=     {s}, deque([s]				)
	print(snake_case_				)
	while q:
		UpperCAmelCase:					int 						=     q.popleft()
		for v in g[u]:
			if v not in vis:
				vis.add(snake_case_				)
				q.append(snake_case_				)
				print(snake_case_				)



def      __UpperCAmelCase    (  snake_case_							:		int				,					snake_case_							:		Any				):



	'''simple docstring'''



	UpperCAmelCase						,   UpperCAmelCase						,   UpperCAmelCase:					Tuple 						=     {s: 0}, set(), {s: 0}
	while True:
		if len(snake_case_				) == len(snake_case_				) - 1:
			break
		UpperCAmelCase:					Optional[Any] 						=     1_0_0_0_0_0
		for i in dist:
			if i not in known and dist[i] < mini:
				UpperCAmelCase:					Optional[Any] 						=     dist[i]
				UpperCAmelCase:					List[Any] 						=     i
		known.add(snake_case_				)
		for v in g[u]:
			if v[0] not in known and dist[u] + v[1] < dist.get(v[0]				,					1_0_0_0_0_0				):
				UpperCAmelCase:					Optional[int] 						=     dist[u] + v[1]
				UpperCAmelCase:					str 						=     u
	for i in dist:
		if i != s:
			print(dist[i]				)



def      __UpperCAmelCase    (  snake_case_							:		Dict				,					snake_case_							:		int=None				,					snake_case_							:		int=None				):



	'''simple docstring'''



	if q is None:
		UpperCAmelCase:					Dict 						=     [1]
	if ind is None:
		UpperCAmelCase:					Optional[Any] 						=     [0] * (len(snake_case_				) + 1)  # SInce oth Index is ignored
		for u in g:
			for v in g[u]:
				ind[v] += 1
		UpperCAmelCase:					List[str] 						=     deque()
		for i in g:
			if ind[i] == 0:
				q.append(snake_case_				)
	if len(snake_case_				) == 0:
		return
	UpperCAmelCase:					List[str] 						=     q.popleft()
	print(snake_case_				)
	for w in g[v]:
		ind[w] -= 1
		if ind[w] == 0:
			q.append(snake_case_				)
	topo(snake_case_				,					snake_case_				,					snake_case_				)



def      __UpperCAmelCase    (  ):



	'''simple docstring'''



	UpperCAmelCase:					Dict 						=     input().strip()
	UpperCAmelCase:					List[Any] 						=     []
	for _ in range(snake_case_				):
		a.append(map(snake_case_				,					input().strip().split()				)				)
	return a, n



def      __UpperCAmelCase    (  snake_case_							:		List[Any]				):



	'''simple docstring'''



	((UpperCAmelCase)						,   (UpperCAmelCase)):					Tuple 						=     a_and_n
	UpperCAmelCase:					str 						=     list(snake_case_				)
	UpperCAmelCase:					Tuple 						=     [[0] * n for i in range(snake_case_				)]
	for k in range(snake_case_				):
		for i in range(snake_case_				):
			for j in range(snake_case_				):
				if dist[i][j] > dist[i][k] + dist[k][j]:
					UpperCAmelCase:					int 						=     dist[i][k] + dist[k][j]
					UpperCAmelCase:					List[Any] 						=     k
	print(snake_case_				)



def      __UpperCAmelCase    (  snake_case_							:		Dict				,					snake_case_							:		List[Any]				):



	'''simple docstring'''



	UpperCAmelCase						,   UpperCAmelCase						,   UpperCAmelCase:					Tuple 						=     {s: 0}, set(), {s: 0}
	while True:
		if len(snake_case_				) == len(snake_case_				) - 1:
			break
		UpperCAmelCase:					str 						=     1_0_0_0_0_0
		for i in dist:
			if i not in known and dist[i] < mini:
				UpperCAmelCase:					str 						=     dist[i]
				UpperCAmelCase:					Any 						=     i
		known.add(snake_case_				)
		for v in g[u]:
			if v[0] not in known and v[1] < dist.get(v[0]				,					1_0_0_0_0_0				):
				UpperCAmelCase:					str 						=     v[1]
				UpperCAmelCase:					Optional[Any] 						=     u
	return dist



def      __UpperCAmelCase    (  ):



	'''simple docstring'''



	UpperCAmelCase						,   UpperCAmelCase:					Optional[int] 						=     map(snake_case_				,					input().split(" "				)				)
	UpperCAmelCase:					Optional[int] 						=     []
	for _ in range(snake_case_				):
		edges.append(map(snake_case_				,					input().split(" "				)				)				)
	return edges, n



def      __UpperCAmelCase    (  snake_case_							:		List[str]				):



	'''simple docstring'''



	((UpperCAmelCase)						,   (UpperCAmelCase)):					List[str] 						=     e_and_n
	e.sort(reverse=snake_case_				,					key=lambda snake_case_				: x[2]				)
	UpperCAmelCase:					str 						=     [{i} for i in range(1				,					n + 1				)]
	while True:
		if len(snake_case_				) == 1:
			break
		print(snake_case_				)
		UpperCAmelCase:					Union[str, Any] 						=     e.pop()
		for i in range(len(snake_case_				)				):
			if x[0] in s[i]:
				break
		for j in range(len(snake_case_				)				):
			if x[1] in s[j]:
				if i == j:
					break
				s[j].update(s[i]				)
				s.pop(snake_case_				)
				break



def      __UpperCAmelCase    (  snake_case_							:		str				):



	'''simple docstring'''



	UpperCAmelCase:					Optional[int] 						=     []
	for node in graph:
		if not graph[node]:
			isolated.append(snake_case_				)
	return isolated




from collections import deque

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Dict	)     ->	Tuple:



	"""simple docstring"""






	return input(UpperCamelCase	).strip().split(""" """	)

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      int     ,					UpperCamelCase		:      int	)     ->	dict[int, list[int]]:



	"""simple docstring"""






	a_     			=   {}
	for i in range(UpperCamelCase	):
		a_     			=   []

	for e in range(UpperCamelCase	):
		a_			, a_     			=   (int(UpperCamelCase	) for i in _input(F"""Edge {e + 1}: <node1> <node2> """	))
		graph[x].append(UpperCamelCase	)
	return graph

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      int     ,					UpperCamelCase		:      int	)     ->	dict[int, list[int]]:



	"""simple docstring"""






	a_     			=   {}
	for i in range(UpperCamelCase	):
		a_     			=   []

	for e in range(UpperCamelCase	):
		a_			, a_     			=   (int(UpperCamelCase	) for i in _input(F"""Edge {e + 1}: <node1> <node2> """	))
		graph[x].append(UpperCamelCase	)
		graph[y].append(UpperCamelCase	)
	return graph

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      int     ,					UpperCamelCase		:      int	)     ->	dict[int, list[tuple[int, int]]]:



	"""simple docstring"""






	a_     			=   {}
	for i in range(UpperCamelCase	):
		a_     			=   []

	for e in range(UpperCamelCase	):
		a_			, a_			, a_     			=   (int(UpperCamelCase	) for i in _input(F"""Edge {e + 1}: <node1> <node2> <weight> """	))
		graph[x].append((y, w)	)
		graph[y].append((x, w)	)
	return graph


if __name__ == "__main__":
			_A     ,				_A							     =       (int(i) for i in _input('Number of nodes and edges: '))

			_A							     =       int(
			    _input(
			        'Press 1 or 2 or 3 \n'
			        '1. Unweighted directed \n'
			        '2. Unweighted undirected \n'
			        '3. Weighted undirected \n'
			    )[0]
			)

			_A							     =       {
			    1: initialize_unweighted_directed_graph,
			    2: initialize_unweighted_undirected_graph,
			    3: initialize_weighted_undirected_graph,
			}[graph_choice](n, m)

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Tuple     ,					UpperCamelCase		:      Tuple	)     ->	List[Any]:



	"""simple docstring"""






	a_			, a_     			=   {s}, [s]
	print(UpperCamelCase	)
	while _s:
		a_     			=   0
		for i in g[_s[-1]]:
			if i not in vis:
				_s.append(UpperCamelCase	)
				vis.add(UpperCamelCase	)
				a_     			=   1
				print(UpperCamelCase	)
				break
		if not flag:
			_s.pop()

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Tuple     ,					UpperCamelCase		:      int	)     ->	int:



	"""simple docstring"""






	a_			, a_     			=   {s}, deque([s]	)
	print(UpperCamelCase	)
	while q:
		a_     			=   q.popleft()
		for v in g[u]:
			if v not in vis:
				vis.add(UpperCamelCase	)
				q.append(UpperCamelCase	)
				print(UpperCamelCase	)

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      int     ,					UpperCamelCase		:      Any	)     ->	Union[str, Any]:



	"""simple docstring"""






	a_			, a_			, a_     			=   {s: 0}, set(), {s: 0}
	while True:
		if len(UpperCamelCase	) == len(UpperCamelCase	) - 1:
			break
		a_     			=   10_0000
		for i in dist:
			if i not in known and dist[i] < mini:
				a_     			=   dist[i]
				a_     			=   i
		known.add(UpperCamelCase	)
		for v in g[u]:
			if v[0] not in known and dist[u] + v[1] < dist.get(v[0]     ,					10_0000	):
				a_     			=   dist[u] + v[1]
				a_     			=   u
	for i in dist:
		if i != s:
			print(dist[i]	)

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Dict     ,					UpperCamelCase		:      int=None     ,					UpperCamelCase		:      int=None	)     ->	Union[str, Any]:



	"""simple docstring"""






	if q is None:
		a_     			=   [1]
	if ind is None:
		a_     			=   [0] * (len(UpperCamelCase	) + 1)  # SInce oth Index is ignored
		for u in g:
			for v in g[u]:
				ind[v] += 1
		a_     			=   deque()
		for i in g:
			if ind[i] == 0:
				q.append(UpperCamelCase	)
	if len(UpperCamelCase	) == 0:
		return
	a_     			=   q.popleft()
	print(UpperCamelCase	)
	for w in g[v]:
		ind[w] -= 1
		if ind[w] == 0:
			q.append(UpperCamelCase	)
	topo(UpperCamelCase     ,					UpperCamelCase     ,					UpperCamelCase	)

def 			__SCREAMING_SNAKE_CASE		(		)     ->	List[Any]:



	"""simple docstring"""






	a_     			=   input().strip()
	a_     			=   []
	for _ in range(UpperCamelCase	):
		a.append(map(UpperCamelCase     ,					input().strip().split()	)	)
	return a, n

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      List[Any]	)     ->	List[str]:



	"""simple docstring"""






	((a_)			, (a_))     			=   a_and_n
	a_     			=   list(UpperCamelCase	)
	a_     			=   [[0] * n for i in range(UpperCamelCase	)]
	for k in range(UpperCamelCase	):
		for i in range(UpperCamelCase	):
			for j in range(UpperCamelCase	):
				if dist[i][j] > dist[i][k] + dist[k][j]:
					a_     			=   dist[i][k] + dist[k][j]
					a_     			=   k
	print(UpperCamelCase	)

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Dict     ,					UpperCamelCase		:      List[Any]	)     ->	str:



	"""simple docstring"""






	a_			, a_			, a_     			=   {s: 0}, set(), {s: 0}
	while True:
		if len(UpperCamelCase	) == len(UpperCamelCase	) - 1:
			break
		a_     			=   10_0000
		for i in dist:
			if i not in known and dist[i] < mini:
				a_     			=   dist[i]
				a_     			=   i
		known.add(UpperCamelCase	)
		for v in g[u]:
			if v[0] not in known and v[1] < dist.get(v[0]     ,					10_0000	):
				a_     			=   v[1]
				a_     			=   u
	return dist

def 			__SCREAMING_SNAKE_CASE		(		)     ->	int:



	"""simple docstring"""






	a_			, a_     			=   map(UpperCamelCase     ,					input().split(""" """	)	)
	a_     			=   []
	for _ in range(UpperCamelCase	):
		edges.append(map(UpperCamelCase     ,					input().split(""" """	)	)	)
	return edges, n

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      List[str]	)     ->	List[str]:



	"""simple docstring"""






	((a_)			, (a_))     			=   e_and_n
	e.sort(reverse=UpperCamelCase     ,					key=lambda UpperCamelCase	: x[2]	)
	a_     			=   [{i} for i in range(1     ,					n + 1	)]
	while True:
		if len(UpperCamelCase	) == 1:
			break
		print(UpperCamelCase	)
		a_     			=   e.pop()
		for i in range(len(UpperCamelCase	)	):
			if x[0] in s[i]:
				break
		for j in range(len(UpperCamelCase	)	):
			if x[1] in s[j]:
				if i == j:
					break
				s[j].update(s[i]	)
				s.pop(UpperCamelCase	)
				break

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      str	)     ->	Optional[Any]:



	"""simple docstring"""






	a_     			=   []
	for node in graph:
		if not graph[node]:
			isolated.append(UpperCamelCase	)
	return isolated