#encoding:utf-8
#スタックの出番だぜ！

railroad = []
while gets
 railroad.push $_.to_i
end

out_cars = []
i = 0
until railroad.length == 0
 if railroad[i] == 0
  out_cars.push railroad.slice!(i-1,2)
  i = 0
 else
  i += 1
 end
end

i = 0
while i < out_cars.length
 puts out_cars[i][0]
 i += 1
end