### subroutines

def includes?(an, bn, as, bs)
  ai = 0
  for bi in (0...bn)
    b = bs[bi]
    return false if ai >= an
    aj = ai
    while aj < an && as[aj] != b
      aj += 1
    end
    return false if aj >= an
    ai = aj + 1
  end
  true
end

### main

an, bn = gets.strip.split(' ').map{|s| s.to_i}
ais = gets.strip.split(' ').map{|s| s.to_i}
bis = gets.strip.split(' ').map{|s| s.to_i}
#p [an, bn, ais, bis]

n = [an, bn].min

while n > 0
  k0 = 0
  found = false

  while k0 + n <= bn
    bis0 = bis[k0, n]
    if includes?(an, n, ais, bis0)
      found = true
      break
    end
    k0 += 1
  end
  break if found

  n -= 1
end

puts n