hash = Hash.new{|h,k| h[k] = []}
inv_hash = Hash.new{|h,k| h[k] = []}

nm = gets.chomp.split(" ").map(&:to_i)

for i in 0..nm[0]-2+nm[1]
  a = gets.chomp.split(" ").map(&:to_i)
  hash[a[1]] = [] if !hash[a[1]]
  hash[a[0]] << a[1]
  inv_hash[a[1]] << a[0]
end

NODE_WEIGHT = {}

start = (hash.keys - inv_hash.keys)[0]

queue = [[start, 0]]

while queue != []
  k = queue.pop
  NODE_WEIGHT[k[0]] = k[1] if !NODE_WEIGHT[k[0]] || NODE_WEIGHT[k[0]] < k[1]
  hash[k[0]].each do |i|
    queue.push [i, k[1] + 1]
  end
end

final = Array.new(nm[0]+nm[1]-2)
inv_hash.to_a.each do |t|
  criteria = NODE_WEIGHT[t[0]]
  t[1].each do |k|
    final[t[0]] = k if NODE_WEIGHT[k] + 1 == NODE_WEIGHT[t[0]]
  end
end

for i in 1..final.length-1
  final[i] = 0 if !final[i]
  puts final[i]
end