### constants

MAX_N = 1000

### subroutines

def get_len(an, bn, as, bs, k0)
  ai = 0
  for bi in (k0...bn)
    return bi - k0 if ai >= an
    b = bs[bi]
    aj = ai
    while aj < an && as[aj] != b
      aj += 1
    end
    return bi - k0 if aj >= an
    ai = aj + 1
  end
  bn - k0
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]

$aidxs = (MAX_N + 1).times.map{[]}
for ai in (0...an)
  $aidxs[ais[ai]] << ai
end
#p $aidxs

max_len = 0

for k0 in (0...bn)
  break if k0 + max_len > bn

  len = get_len(an, bn, ais, bis, k0)
  max_len = len if max_len < len
end

puts max_len