a, b = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
bs = gets.split.map(&:to_i)

ans = 0
bs.size.times do |from|
  from.upto(bs.size-1) do |to|
    r = bs[from..to]
    # p [from, to, r]
    ri = 0
    match = false
    as.each do |n|
      if n == r[ri]
        ri += 1
      end
      if ri >= r.size
        match = true
        break
      end
    end

    # puts "match" if match
    ans = [r.size, ans].max if match
  end
end

puts ans