import java.util.Scanner

import scala.collection.mutable

object B extends App {

  val sc = new Scanner(System.in)
  type Segment = (Int, Int)

  val n = sc.nextInt()
  val checkmates = getSegments(n)
  val m = sc.nextInt()
  val programming = getSegments(m)

  val checkmatesL = checkmates.maxBy(_._1)._1
  val checkmatesR = checkmates.minBy(_._2)._2
  val programmingL = programming.maxBy(_._1)._1
  val programmingR = programming.minBy(_._2)._2

  val ans = math.max(programmingL - checkmatesR, checkmatesL - programmingR)
  println(math.max(0, ans))

  def getSegments(n: Int): Set[Segment] = {
    var checkmates = mutable.HashSet[Segment]()
    (1 to n) foreach { _ =>
      val x: Segment = (sc.next().toInt, sc.next().toInt)
      checkmates += x
    }
    checkmates.toSet
  }
}

/**
  * Created by ruslan on 3/4/17.
  */
object ProblemB extends App {
    import java.io.{BufferedReader, InputStreamReader, PrintWriter}
import java.util.StringTokenizer
  override def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    var st: StringTokenizer = null
    val out = new PrintWriter(System.out)

    def next: String = {
      while (st == null || !st.hasMoreTokens) st =
        new StringTokenizer(br.readLine)
      st.nextToken
    }

    def nextInt: Int = next.toInt

    val n = nextInt
    var maxMath = 0
    var minMath = Int.MaxValue
    var maxChess = 0
    var minChess = Int.MaxValue

    for (i <- 1 to n) {
      maxMath = math.max(nextInt, maxMath)
      minMath = math.min(nextInt, minMath)
    }

    val m = nextInt
     for (i <- 1 to m) {
      maxChess = math.max(nextInt, maxChess)
      minChess = math.min(nextInt, minChess)
    }

    val res = math.max(maxChess - minMath, maxMath - minChess)
    println(math.max(0, res))

  }
}

object _785B extends CodeForcesApp {
   import Utils._, annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._

  override def apply(io: IO) = {import io.{read, readAll, write, writeAll, writeLine}
    val chess, programming = read[Seq[(Int, Int)]]
    write(calc(chess, programming) max calc(programming, chess) max 0)
  }

  def calc(a: Seq[(Int, Int)], b: Seq[(Int, Int)]) = b.maxWith(_._1).get - a.minWith(_._2).get
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def in(set: collection.Set[A]): Boolean = set(a)
    def some: Option[A] = Some(a)
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = assuming(t.size == 1)(t.head)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def maxWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.max)
    def minWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.min)
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap withDefaultValue 0
    def X[B](s: Traversable[B]): Iterator[(A, B)] = for {a <- t.toIterator; b <- s} yield (a, b)
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairExtensions[A, B](val p: (A, B)) extends AnyVal {
    def key = p._1
    def value = p._2
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_.key).mapValues(_.value)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def lastKeyOption: Option[K] = m.lastOption.map(_.key)
    def firstKeyOption: Option[K] = m.headOption.map(_.key)
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
    def mapValuesSafely[V2](f: V => V2): Map[K, V2] = m.mapValues(f).view.force //Hack to get around https://issues.scala-lang.org/browse/SI-4776
  }
  implicit class MutableMapExtensions[K, V](val m: mutable.Map[K, V]) extends AnyVal {
    def removeLast(): Option[(K, V)] = m.lastOption.map(removeNode)
    def removeFirst(): Option[(K, V)] = m.headOption.map(removeNode)
    private def removeNode(kv: (K, V)): (K, V) = {
      m -= kv.key
      kv
    }
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class IterableExtensions[A, C[A] <: Seq[A]](val s: C[A]) extends AnyVal {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
    def foldUntil[B](z: B)(f: (B, A) => B)(c: B => Boolean): Option[(B, Int)] = s.view.scanLeft(z)(f).findWithIndex(c)
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "YES" else "NO"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    import java.lang.{Integer => JInt}
    def withHighestOneBit: Int = JInt.highestOneBit(x)
    def withLowestOneBit: Int = JInt.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JInt.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JInt.numberOfTrailingZeros(x)
    def bitCount: Int = JInt.bitCount(x)
    def setLowestBits(n: Int): Int = x | (left(n) - 1)
    def clearLowestBits(n: Int): Int = x & left(n, ~0)
    def setBit(i: Int, set: Boolean): Int = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Int = x | left(i)
    def clearBit(i: Int): Int = x & ~left(i)
    def toggleBit(i: Int): Int = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Int = 1) = assuming(bits >= 0 && bits < JInt.SIZE)(num << bits)
    def X(y: Int): Iterator[(Int, Int)] = until(x) X until(y)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    import java.lang.{Long => JLong}
    def withHighestOneBitSet: Long = JLong.highestOneBit(x)
    def withLowestOneBitSet: Long = JLong.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JLong.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JLong.numberOfTrailingZeros(x)
    def bitCount: Int = JLong.bitCount(x)
    def setLowestBits(n: Int): Long = x | (left(n) - 1)
    def clearLowestBits(n: Int): Long = x & left(n, ~0L)
    def setBit(i: Int, set: Boolean): Long = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Long = x | left(i)
    def clearBit(i: Int): Long = x & ~left(i)
    def toggleBit(i: Int): Long = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Long = 1L) = assuming(bits >= 0 && bits < JLong.SIZE)(num << bits)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def distance(y: A) = (x - y).abs()
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(x, y, if (n.lteq(x, y)) n.one else -n.one)
    def ceilDiv(y: A): A = if (mod(y) == n.zero) x/y else n.one + (x/y)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: => V): Dict[K, V] = using(_ => default)
    def using[V](f: K => V): Dict[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }
  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]
  def memoize[A, B](f: A => B): A ==> B = map[A] using f
  implicit class FuzzyDouble(val x: Double) extends AnyVal {
    def  >~(y: Double) = x > y + eps
    def >=~(y: Double) = x >= y + eps
    def  ~<(y: Double) = y >=~ x
    def ~=<(y: Double) = y >~ x
    def  ~=(y: Double) = (x distance y) <= eps
  }
  def toPolygon(points: Traversable[(Int, Int)]): java.awt.Polygon = {
    val (xs, ys) = points.unzip
    new java.awt.Polygon(xs.toArray, ys.toArray, points.size)
  }
  def toShape(points: Traversable[(Double, Double)]): java.awt.geom.GeneralPath = {
    val shape = new java.awt.geom.GeneralPath()
    points.headOption foreach {case (x, y) => shape.moveTo(x, y)}
    points.tail foreach {case (x, y) => shape.lineTo(x, y)}
    shape.closePath()
    shape
  }
  def until(until: Int): Range = 0 until until
  def not[A](f: A => Boolean): A => Boolean = {x: A => !f(x)}
  def assuming[A](cond: => Boolean)(f: => A): A = {require(cond); f}
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) f.some else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  type ==>[A, B] = collection.Map[A, B]
  val mod: Int = (1e9 + 7).toInt
  val eps: Double = 1e-9
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    when(tokenizers.nonEmpty)(tokenizers.head)
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                      : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

import java.io._
import java.util.StringTokenizer

import scala.language.postfixOps
import scala.math._

object TaskB extends App {
  val ir = new InputReader(System.in)
  def minMax(a: (Int, Int), b: (Int, Int)) = (max(a._1, b._1), min(a._2, b._2))

  val a = 1 to ir.nextInt map { _ => (ir.nextInt, ir.nextInt) } reduce minMax
  val b = 1 to ir.nextInt map { _ => (ir.nextInt, ir.nextInt) } reduce minMax

  println(max(max(a._1 - b._2, b._1 - a._2), 0))
}

class InputReader(val stream: InputStream) {
  val reader = new BufferedReader(new InputStreamReader(stream))
  var stt: StringTokenizer = _

  def nextString: String = {
    while (stt == null || !stt.hasMoreTokens) stt = new StringTokenizer(reader.readLine)
    stt.nextToken
  }

  def nextInt: Int = nextString.toInt
}

import scala.io.StdIn

object TaskB {

  def readData(): Seq[(Int, Int)] = {
    val size = StdIn.readInt()
    1 to size map { _ =>
      val tuple = StdIn.readf2("{0} {1}")
      (tuple._1.toString.toInt, tuple._2.toString.toInt)
    }
  }

  def isCrossed(chessTime: (Int, Int), programmingTime: (Int, Int)): Boolean = {
    if (chessTime._1 >= programmingTime._1 && chessTime._1 <= programmingTime._2) {
      return true
    }

    if (chessTime._2 >= programmingTime._1 && chessTime._2 <= programmingTime._2) {
      return true
    }

    false
  }

  def distance(chessTime: (Int, Int), programmingTime: (Int, Int)): Int = {
    if (isCrossed(chessTime, programmingTime)) {
      0
    } else if (chessTime._1 < programmingTime._1) {
      programmingTime._1 - chessTime._2
    } else {
      chessTime._1 - programmingTime._2
    }
  }

  def main(args: Array[String]): Unit = {
    val chessData = readData()
    val programmingData = readData()

    val distances = for {
      x <- List(
        chessData.minBy(_._1),
        chessData.minBy(_._2),
        chessData.maxBy(_._1),
        chessData.maxBy(_._2))
      y <- List(
        programmingData.minBy(_._1),
        programmingData.minBy(_._2),
        programmingData.maxBy(_._1),
        programmingData.maxBy(_._2)
      )
    } yield {
      distance(x, y)
    }

    println(distances.max)
  }

}

import java.util.Scanner

import scala.collection.mutable

/**
  * Scala implementation of a faster java.util.Scanner
  * See: http://codeforces.com/blog/entry/7018
  */

object B extends App {

  val sc = new Scanner(System.in)
  type Segment = (Int, Int)

  val n = sc.nextInt()
  val checkmates = getSegments(n)
  val m = sc.nextInt()
  val programming = getSegments(m)

  val checkmatesL = checkmates.maxBy(_._1)._1
  val checkmatesR = checkmates.minBy(_._2)._2
  val programmingL = programming.maxBy(_._1)._1
  val programmingR = programming.minBy(_._2)._2

  if (checkmatesR < programmingL) {
    println(programmingL - checkmatesR)
  } else if (checkmatesL > programmingR) {
    println(checkmatesL - programmingR)
  } else {
    println(0)
  }

  def getSegments(n: Int): Set[Segment] = {
    var checkmates = mutable.HashSet[Segment]()
    (1 to n) foreach { _ =>
      val x: Segment = (sc.next().toInt, sc.next().toInt)
      checkmates += x
    }
    checkmates.toSet
  }
}

import scala.io.StdIn

object TaskB {

  def readData(): Seq[(Int, Int)] = {
    val size = StdIn.readInt()
    1 to size map { _ =>
      val tuple = StdIn.readf2("{0} {1}")
      (tuple._1.toString.toInt, tuple._2.toString.toInt)
    }
  }

  def isCrossed(chessTime: (Int, Int), programmingTime: (Int, Int)): Boolean = {
    if (chessTime._1 >= programmingTime._1 && chessTime._1 <= programmingTime._2) {
      return true
    }

    if (chessTime._2 >= programmingTime._1 && chessTime._2 <= programmingTime._2) {
      return true
    }

    false
  }

  def distance(chessTime: (Int, Int), programmingTime: (Int, Int)): Int = {
    if (isCrossed(chessTime, programmingTime)) {
      0
    } else if (chessTime._1 < programmingTime._1) {
      programmingTime._1 - chessTime._2
    } else {
      chessTime._2 - programmingTime._1
    }
  }

  def main(args: Array[String]): Unit = {
    val chessData = readData()
    val programmingData = readData()
    val tuples = for {x <- chessData; y <- programmingData} yield (x, y)
    println(tuples.map { tuple => distance(tuple._1, tuple._2) }.max)
  }

}

import scala.io.StdIn

object TaskB {

  def readData(): Seq[(Int, Int)] = {
    val size = StdIn.readInt()
    1 to size map { _ =>
      val tuple = StdIn.readf2("{0} {1}")
      (tuple._1.toString.toInt, tuple._2.toString.toInt)
    }
  }

  def isCrossed(chessTime: (Int, Int), programmingTime: (Int, Int)): Boolean = {
    if (chessTime._1 >= programmingTime._1 && chessTime._1 <= programmingTime._2) {
      return true
    }

    if (chessTime._2 >= programmingTime._1 && chessTime._2 <= programmingTime._2) {
      return true
    }

    false
  }

  def distance(chessTime: (Int, Int), programmingTime: (Int, Int)): Int = {
    if (isCrossed(chessTime, programmingTime)) {
      0
    } else if (chessTime._1 < programmingTime._1) {
      programmingTime._1 - chessTime._2
    } else {
      chessTime._2 - programmingTime._1
    }
  }

  def main(args: Array[String]): Unit = {
    val chessData = readData()
    val programmingData = readData()
    val distances = for {x <- chessData; y <- programmingData} yield distance(x, y)
    println(distances.max)
  }

}

/**
  * Created by ruslan on 3/4/17.
  */
object ProblemB extends App {
    import java.io.{BufferedReader, InputStreamReader, PrintWriter}
import java.util.StringTokenizer
  override def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    var st: StringTokenizer = null
    val out = new PrintWriter(System.out)

    def next: String = {
      while (st == null || !st.hasMoreTokens) st =
        new StringTokenizer(br.readLine)
      st.nextToken
    }

    def nextInt: Int = next.toInt

    val n = nextInt
    var maxMath = 0
    var minMath = Int.MaxValue
    var maxChess = 0
    var minChess = Int.MaxValue

    for (i <- 1 to n) {
      maxMath = math.max(nextInt, maxMath)
      minMath = math.min(nextInt, minMath)
    }

    val m = nextInt
     for (i <- 1 to m) {
      maxChess = math.max(nextInt, maxChess)
      minChess = math.min(nextInt, minChess)
    }

    val res = math.max(maxChess - minMath, maxMath - minMath)
    println(math.max(0, res))

  }
}

/**
  * Created by ruslan on 3/4/17.
  */
object ProblemB extends App {
import java.io.{BufferedReader, InputStreamReader, PrintWriter}
import java.util.StringTokenizer

  override def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    var st: StringTokenizer = null
    val out = new PrintWriter(System.out)

    def next: String = {
      while (st == null || !st.hasMoreTokens) st =
        new StringTokenizer(br.readLine)
      st.nextToken
    }

    def nextInt: Int = next.toInt

    val n = nextInt
    var maxMath, minMath = 0
    var maxChess, minChess = 0

    for (i <- 1 to n) {
      maxMath = math.max(nextInt, maxMath)
      minMath = math.min(nextInt, minMath)
    }

    val m = nextInt
     for (i <- 1 to m) {
      maxChess = math.max(nextInt, maxChess)
      minChess = math.min(nextInt, minChess)
    }

    val res = math.max(maxChess - minMath, maxMath - minMath)
    println(math.max(0, res))

  }
}

/**
  * Created by ruslan on 3/4/17.
  */
object ProblemB extends App {
    import java.io.{BufferedReader, InputStreamReader, PrintWriter}
import java.util.StringTokenizer

  override def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    var st: StringTokenizer = null
    val out = new PrintWriter(System.out)

    def next: String = {
      while (st == null || !st.hasMoreTokens) st =
        new StringTokenizer(br.readLine)
      st.nextToken
    }

    def nextInt: Int = next.toInt

    val n = nextInt

    val timeForChess = (for (i <- 1 to n) yield (nextInt, nextInt)).sortWith((p1, p2) => p1._2 - p2._2 < 0)
    val m = nextInt
    val timeForMath = (for (i <- 1 to m) yield (nextInt, nextInt)).sortWith((p1, p2) => p1._1 - p2._1 > 0)

    println(math.max(0, timeForMath(0)._1 - timeForChess(0)._2))

  }
}

import java.io._
import java.util.StringTokenizer

import scala.language.postfixOps
import scala.math.max

object TaskB extends App {
  val ir = new InputReader(System.in)

  val r = 1 to ir.nextInt map { _ => ir.nextInt; ir.nextInt } min
  val l = 1 to ir.nextInt map { _ => val l = ir.nextInt; ir.nextInt; l } max

  println(max(l - r, 0))
}

class InputReader(val stream: InputStream) {
  val reader = new BufferedReader(new InputStreamReader(stream))
  var stt: StringTokenizer = _

  def nextString: String = {
    while (stt == null || !stt.hasMoreTokens) stt = new StringTokenizer(reader.readLine)
    stt.nextToken
  }

  def nextInt: Int = nextString.toInt
}


object Solution {

  def main(args: Array[String]): Unit = {

    val in: Iterator[String] = io.Source.stdin.getLines()
    val Array(m,n) = in.next().split(" ").map(_.toInt)
    val first = in.next().split(" ").map(_.toInt)
    val second = in.next().split(" ").map(_.toInt)

    val fe = first.count(_ % 2 == 0)
    val fo = first.count(_% 2 != 0)
    val se  = second.count(_ % 2 == 0)
    val so  = second.count(_ % 2 != 0)

    println(Math.min(fe, so) + Math.min(fo, se))

  }

}

object a extends App {
  import scala.io.StdIn.readLine
  import scala.math.min

  val _ = readLine
  def getArr: (Array[Int], Array[Int]) = readLine.split(" ").map(_.toInt).partition(_ % 2 == 0)
  val aa = getArr
  val bb = getArr

  val (aOdd, aEven) = (aa._1.length, aa._2.length)
  val (bOdd, bEven) = (bb._1.length, bb._2.length)

  println(min(aOdd, bEven) + min(aEven, bOdd))

}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N, M = ni()
    val A = na(N)
    val B = na(M)
    val evenA = A.count(_%2 == 0)
    val evenB = B.count(_%2 == 0)
    val ans = min(evenA, M - evenB) + min(N - evenA, evenB)
    out.println(ans)
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null

  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }

  def debug(as: Array[Boolean]): Unit = DEBUG {
    debug(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = DEBUG {
    debug(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = DEBUG {
    debug(as.mkString(" "))
  }

  def debugDim(m: Array[Array[Long]]): Unit = DEBUG {
    REP(m.length) { i =>
      debug(m(i))
    }
  }

  def debugDimFlip(m: Array[Array[Long]]): Unit = DEBUG {
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }

  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }

  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


object CF919A extends App{
  val sc = new java.util.Scanner(System.in)
  val n, m = sc.nextInt
  println((Seq.fill[Double](n)(sc.nextDouble / sc.nextDouble) min) * m)
}

import java.util.Scanner

import scala.language.implicitConversions

object Supermarket {
  class Lazy[A](x: => A) {
    lazy val value = x
    override def toString(): String = value.toString()
  }
  object Lazy {
    def apply[A](x: => A) = new Lazy(x)
    implicit def fromLazy[A](z: Lazy[A]): A = z.value
    implicit def toLazy[A](x: => A): Lazy[A] = Lazy(x)
  }
  def memoize[I, O](f: I => O): I => O = new collection.mutable.HashMap[I, O]() {
    override def apply(key: I) = getOrElseUpdate(key, f(key))
  }

  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val (n, m) = (sc.nextInt(), sc.nextInt())
    val min_ratio = 0.until(n).map(i => sc.nextInt() * 1.0 / sc.nextInt()).min
    print(min_ratio * m);
  }
}

import java.io._
import java.util.StringTokenizer

/**
  * Created by amir.
  */

object A extends App{

  class RW(val inputFile: String, val outputFile: String) {
    private var br = new BufferedReader(new InputStreamReader(System.in))
    private var out = new PrintWriter(new OutputStreamWriter(System.out))

    /* val f = new File(inputFile)
    if (f.exists && f.canRead)
    try {
    br = new BufferedReader(new FileReader(f))
    out = new PrintWriter(new FileWriter(outputFile))
    } catch {
    case e: IOException =>
    e.printStackTrace()
    }*/

    private var st: StringTokenizer = _
    var eof = false

    def nextLine: String = {
      var s = ""
      try
        s = br.readLine()
      catch {
        case e: IOException =>
          e.printStackTrace()
      }
      s
    }

    def next: String = {
      while (st == null || !st.hasMoreTokens)
        try
          st = new StringTokenizer(br.readLine)
        catch {
          case _: IOException =>
            eof = true
            ""
        }
      st.nextToken
    }

    def nextLong: Long = next.toLong

    def nextInt: Int = next.toInt

    def println: Unit = out.println()

    def println(o: Any): Unit = out.println(o)

    def print(o: Any): Unit = out.print(o)

    def close: Unit = {
      try {
        br.close()
      } catch {
        case e: IOException =>
          e.printStackTrace()
      }

      out.close()
    }
  }

  override def main(args: Array[String]) {
    val rw = new RW("input.txt", "output.txt")

    val n = rw.nextInt
    val m = rw.nextInt
    val costs = for (_ <- 1 to n) yield rw.nextInt.toDouble / rw.nextInt
    rw.println(costs.min * m)
    rw.close
  }
}

object A extends App {
  val Array(n, m) = scala.io.StdIn.readLine().split(" ").map(_.toInt)
  val price = (0 until n).foldLeft(Double.MaxValue)((price, _) => {
    val Array(a, b) = scala.io.StdIn.readLine().split(" ").map(_.toDouble)
    price.min(a / b)
  })
  println(price * m)
}