import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

object Main extends App {
  import scala.io.StdIn._


  val Array(w, h) = readLine.trim.split(' ').map(_.toInt)
  val rows = readLine.trim.split(' ').map(_.toInt)
  val columns = readLine.trim.split(' ').map(_.toInt)
  if (rows.sum == columns.sum){
    val (source, drain, nodes) = makeGraph(rows, columns)
    val maxFlow = calMaxFlow(nodes, source, drain)
    println(
      if (rows.sum == maxFlow) 1
      else 0
    )
  }else println(0)
  def makeGraph(row: Array[Int], column: Array[Int]): (Int, Int, Array[Array[Edge]]) = {
    val result = Array.tabulate(row.length + column.length + 2){_ ⇒ ArrayBuffer[Edge]()}
    val source = result.length - 2
    val drain = result.length - 1
    for (r ← row.indices){
      result(r).append(Edge(source, result(source).length,  0))
      result(source).append(Edge(r, 0, row(r)))
    }
    for (c ← column.indices.map(_ + row.length)) {
      result(c).append(Edge(drain, result(drain).length, column(c - row.length)))
      result(drain).append(Edge(c, 0, 0))
      for (r ← row.indices){
        result(c).append(Edge(r, result(r).length, 0))
        result(r).append(Edge(c, result(c).length - 1, 1))
      }
    }
    (source, drain, result.map(_.toArray))
  }
  def calMaxFlow(nodes: Array[Array[Edge]], source: Int, drain: Int): Int = {
    val depth = Array.tabulate(nodes.length){_ ⇒ -1}
    val searching = Array.tabulate(nodes.length){_ ⇒ 0}
    def bfs(): Unit = {
      depth.indices.foreach(i ⇒ depth(i) = -1)
      val queue = mutable.Queue[Int]()
      depth(source) = 0
      queue.enqueue(source)
      while (queue.nonEmpty){
        val current = queue.dequeue()
        for (Edge(next, _, flow) ← nodes(current) if flow > 0 && depth(next) == -1){
          depth(next) = depth(current) + 1
          queue.enqueue(next)
        }
      }
    }
    def dfs(current: Int): Boolean = {
      if (current == drain) true
      else {
        var i = searching(current)
        var found = false
        while(i < nodes(current).length && !found) {
          if (nodes(current)(i).flow > 0 && depth(nodes(current)(i).to) > depth(current)){
            found = dfs(nodes(current)(i).to)
            if (found){
              val Edge(to, pair, _) = nodes(current)(i)
              nodes(current)(i).flow -= 1
              nodes(to)(pair).flow += 1
            }
          }
          if (!found) i += 1
        }
        searching(current) = i
        found
      }
    }
    var count = 0
    var canPush = true
    while(canPush){
      bfs()
      searching.indices.foreach(i ⇒ searching(i) = 0)
      canPush = false
      while(dfs(source)){
        canPush = true
        count += 1
      }
    }
    count
  }
  case class Edge(to: Int, pair: Int, var flow: Int)
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
  }
}
