module N4
module_function
class UnionFind; def initialize(n); @p = *0...n; @r = [0] * n; end; def find(x); @p[x] == x ? x : @p[x] = find(@p[x]); end; def unite(x, y); x = find(x); y = find(y); return if x == y; if @r[x] < @r[y]; @p[x] = y; else; @p[y] = x; @r[x] += 1 if @r[x] == @r[y]; end; nil; end; def same(x, y); find(x) == find(y); end; end
end


(n,m),*ab=$<.map{|s|s.split.map &:to_i}

uf=N4::UnionFind.new(10**5+2)
par=$par=[]

def anc(a,b)
  $par[b]&&($par[b]==a||anc(a,$par[b]))
end

que=ab
until que.empty?
  a,b=que.shift
  if par[b]==nil
    par[b]=a
    uf.unite(a,b)
  elsif uf.same(a,b)
    if anc(par[b],a)
      par[b]=a
    end
  else
    que.push([a,b])
  end
end

puts par[1..-1].map{|e|e==nil ? 0 : e}
