n, m = read_line.split.map(&.to_i) s = read_line.split.map(&.to_i) t = read_line.split.map(&.to_i) limit = Array.new(n + 1, m) pos = m (n - 1).downto(0) do |i| if pos > 0 && s[i] == t[pos - 1] pos -= 1 end limit[i] = pos end np = 0 mp = 0 ans = [] of Int32 (n - m).times do min = 9999 min_np = n cur_mp = mp np.upto(n - 1) do |i| if s[i] < min && limit[i + 1] <= cur_mp min = s[i] min_np = i end if cur_mp < m if t[cur_mp] == s[i] cur_mp += 1 else break end else break end end mp += min_np - np np = min_np + 1 ans << min end puts ans.join(" ")