## subroutines

def includes?(an, bn, as, bs)
  ai = 0
  for b in bs
    return false if ai >= an
    aidx = $aidxs[b]
    aj = 0
    found = false
    for aj in (0...aidx.length)
      if aidx[aj] >= ai
        found = true
        break
      end
    end
    return false if ! found
    ai = aidx[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]

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

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