object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val Array(a, b) = readLine.trim.split(' ').map(_.toInt)
  val deck = new ADeck(readLine.trim.split(' ').map(_.toInt))
  val bDeck = readLine.trim.split(' ').map(_.toInt).toList
  println(
    searchMax(deck, bDeck)
  )
  def searchMax(a: ADeck, b: List[Int]): Int = {
    def sub(cards: List[Int], position: Int): Int = {
      cards match {
        case h::t ⇒ a.nextN(position, h) match {
          case Some(p) ⇒ sub(t, p) + 1
          case None ⇒ 1
        }
        case _ ⇒ 1
      }
    }
    b match {
      case h::t ⇒ a.firstN(h) match {
        case None ⇒ searchMax(a, t)
        case Some(p) ⇒ math.max(sub(t, p), searchMax(a, t))
      }
      case Nil ⇒ 0
    }
  }

  class ADeck(private val deck: Array[Int]) {
    private val cards = (0 to 1000).map{n ⇒ deck.map{
      case `n` ⇒ 1
      case _ ⇒ 0: Int
    }.also{arr ⇒ for (i ← 1 until arr.length) arr(i) += arr(i - 1)}}.toArray
    def nextN(position: Int, n: Int): Option[Int] = {
      lowerBound(cards(n), cards(n)(position) + 1)
    }
    def firstN(n: Int): Option[Int] = {
      lowerBound(cards(n), 1)
    }
  }
  def lowerBound[T](array: Array[T], target: T)(implicit ordering: Ordering[T]): Option[Int] = {
    var left = 0
    var right = array.length
    while (left < right){
      val mid = (left + right) / 2
      if (ordering.compare(array(mid), target) < 0){
        left = mid + 1
      }else {
        right = mid
      }
    }
    if (right == array.length) None
    else Some(right)
  }
  def upperBound[T](array: Array[T], target: T)(implicit ordering: Ordering[T]): Option[Int] = {
    var left = 0
    var right = array.length
    while(left < right){
      val mid = (left + right) / 2
      if (ordering.compare(array(mid), target) <= 0){
        left = mid + 1
      }else {
        right = mid
      }
    }
    if (right == array.length) None
    else Some(right)
  }
  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
