N, M = gets.strip.split(/ /).collect{|n| n.to_i}

$branches = {}
$branches_r = {}
(N - 1 + M).times{
	a, b = gets.strip.split(/ /).collect{|n| n.to_i}
	if l = $branches[a]
		l << b
	else
		$branches[a] = [b]
	end
	if l = $branches_r[b]
		l << a
	else
		$branches_r[b] = [a]
	end
}

def find_root
	n = 1
	while l = $branches_r[n]
		n = l[0]
	end
	n
end

def calc_distances(root)
	queue = [root]
	done = {}
	distances = []
	distances[root] = 0
	while n = queue.pop
		if l = $branches[n]
			d = distances[n] + 1
			l.each{|n2|
				if (d2 = distances[n2]) == nil || d > d2
					distances[n2] = d
					queue << n2
				end
			}
		end
	end
	distances
end

root = find_root
distances = calc_distances(root)

(1..N).each{|i|
	d = distances[i]
	if d == 0
		puts 0
	else
		puts $branches_r[i].collect{|x| [distances[x], x]}.max[1]
	end
}

