import java.io.PrintWriter
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

object Main {

  val br = new BufferedReader(new InputStreamReader(System.in))
  val MAX = 100111
  val tree = new Array[Int](MAX<<2)
  val prop = new Array[Int](MAX<<2)
  val tree2 = new Array[Int](MAX<<2)
  var out = new PrintWriter(System.out)
  var st = new StringTokenizer(br.readLine())

  def nextDouble = next.toDouble

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

  def nextLong = next.toLong

  def nextInt = next.toInt

  def or(x: Int, tl: Int, tr: Int, l: Int, r: Int, mask: Int): Unit = {

    if (prop(x) != 0) {
      tree(x) |= prop(x)
      if (tl != tr) {
        prop(x+x) |= prop(x)
        prop(x+x+1) |= prop(x)
      }
      prop(x) = 0
    }

    if (tl > tr || tl > r || tr < l)
      return
    if (tl >= l && tr <= r) {
      tree(x) |= mask
      if (tl!=tr) {
        prop(x+x) |= mask
        prop(x+x+1) |= mask
      }
      return
    }

    or(x+x, tl, (tl+tr)>>1, l, r, mask)
    or(x+x+1, ((tl+tr)>>1)+1, tr, l, r, mask)

    tree(x) = tree(x+x) | tree(x+x+1)
  }

  def get(x: Int, tl: Int, tr: Int, l: Int, r: Int): Int = {
    if (tl > tr || tl > r || tr < l) return 0

    if (prop(x) != 0) {
      tree(x) |= prop(x)
      if (tl != tr) {
        prop(x+x) |= prop(x)
        prop(x+x+1) |= prop(x)
      }
      prop(x) = 0
    }

    if (tl >= l && tr <= r) return tree(x)

    val q1 = get(x+x, tl, (tl+tr)>>1, l, r)
    val q2 = get(x+x+1, ((tl+tr)>>1)+1, tr, l, r)
    q1 | q2
  }

  def build(x: Int, tl: Int, tr: Int, a: Array[Int]): Unit = {
    if (tl > tr) return
    if (tl == tr) {
      tree2(x) = a(tl)
      return
    }
    build(x+x,tl,(tl+tr)>>1, a)
    build(x+x+1,((tl+tr)>>1)+1,tr, a)
    tree2(x) = tree2(x+x) & tree2(x+x+1)
  }

  def get2(x: Int, tl: Int, tr: Int, l: Int, r: Int): Int = {
    if (tl > tr || tl > r || tr < l) return 0x7fffffff
    if (tl >= l && tr <= r) return tree2(x)
    val q1 = get2(x+x, tl, (tl+tr)>>1, l, r)
    val q2 = get2(x+x+1, ((tl+tr)>>1)+1, tr, l, r)
    q1 & q2
  }

  def main(args: Array[String]) {
    val n = nextInt
    val m = nextInt

    val l = new Array[Int](m+1)
    val r = new Array[Int](m+1)
    val q = new Array[Int](m+1)

    for (i <- 1 to m) {
      l(i) = nextInt
      r(i) = nextInt
      q(i) = nextInt
    }

//    val n = 100
//    val m = 5
//
//    val l = new Array[Int](m+1)
//    val r = new Array[Int](m+1)
//    val q = new Array[Int](m+1)
//
//    val rand = new Random(System.currentTimeMillis())
//    for (i <- 1 to m) {
//      l(i) = 1 + rand.nextInt(n)
//      r(i) = 1 + rand.nextInt(n)
//      if (l(i) > r(i)) {
//        val tmp = l(i)
//        l(i) = r(i)
//        r(i) = tmp
//      }
//      q(i) = rand.nextInt()
//    }
//
//    /* correct answer */
//    val correct = new Array[Int](n+1)
//    for (i <- 1 to m) {
//      for (j <- l(i) to r(i))
//        correct(j) |= q(i)
//    }
//    var ans = true
//    for (i <- 1 to m) {
//      var mask = 0x7fffffff
//      for (j <- l(i) to r(i))
//        mask &= correct(j)
//      if (mask != q(i)) {
//        ans = false
//      }
//    }
//    out.println("CORRECT ANSWER: " + (if (ans) "YES" else "NO"))
//    /* end correct answer */

    for (i <- 1 to m)
      or(1,1,n,l(i),r(i),q(i))

    val a = new Array[Int](n+1)
    for (i <- 1 to n)
      a(i) = get(1,1,n,i,i)

    build(1,1,n,a)

    for (i <- 1 to m)
      if (get2(1,1,n,l(i),r(i)) != q(i)) {
        out.println("NO")
        out.close()
        return
      }

    out.println("YES")
    for (i <- 1 to n)
      out.print(a(i)+" ")
    out.close()
  }

}

import scala.collection._

object B extends App {

  case class SegTree(as: Array[Int]) {

    val n = as.length
    val t = Array.ofDim[Int](4 * Integer.highestOneBit(n))
    build(1, 0, n - 1)
//println(t.mkString(" "))
    def build(v: Int, tl: Int, tr: Int) {
      if (tl == tr) t(v) = as(tl)
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        build(v2, tl, tm)
        build(v2 + 1, tm + 1, tr)
        t(v) = t(v2) & t(v2 + 1)
      }
    }

    def query(l: Int, r: Int, v: Int = 1, tl: Int = 0, tr: Int = n - 1): Int = {
      //println(l, r, v, tl, tr)
      if (l > r) Int.MaxValue
      else if (l == tl && r == tr) t(v)
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        //println(query(l, Math.min(r, tm), v2, tl, tm), query(Math.max(l, tm + 1), r, v2 + 1, tm + 1, tr))
        query(l, Math.min(r, tm), v2, tl, tm) &
          query(Math.max(l, tm + 1), r, v2 + 1, tm + 1, tr)
      }
    }

    def update(pos: Int, delta: Int, v: Int = 1, tl: Int = 0, tr: Int = n - 1) {
      if (tl == tr) t(v) += delta
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        if (pos <= tm) update(pos, delta, v2, tl, tm)
        else update(pos, delta, v2 + 1, tm + 1, tr)
        t(v) = t(v2) + t(v2 + 1)
      }
    }

  }

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  case class Q(l: Int, r: Int, q: Int)

  val qs = Array.ofDim[Q](m)

  for (i <- 0 until m) {
    val Array(l, r, q) = readInts(3)
    qs(i) = Q(l - 1, r - 1, q)
  }

  //val sorted = qs.sortBy(_.l)

  val xs = Array.fill(n) { 0 }

  val bits = Array.fill(31) { 0 }

  val qByL = new mutable.PriorityQueue[Q]()(Ordering.by(- _.l))
  qByL ++= qs

  val qByR = new mutable.PriorityQueue[Q]()(Ordering.by(- _.r))

  var pos = 0
  for (i <- 0 until n) {
    while (qByR.nonEmpty && qByR.head.r < i) {
      val qr = qByR.dequeue
      //println(qr)
      for (b <- 0 to 30) {
        if ((qr.q & (1 << b)) > 0) bits(b) -= 1
      }
    }
    while (qByL.nonEmpty && qByL.head.l == i) {
      val ql = qByL.dequeue
      for (b <- 0 to 30) {
        if ((ql.q & (1 << b)) > 0) {
          bits(b) += 1
        }
      }
      qByR += ql
    }
    //println(bits.mkString(" "))
    for (b <- 0 to 30) {
      if (bits(b) < 0) fail(1)
      else if (bits(b) > 0) xs(i) |= (1 << b)
    }
  }

  val segTree = SegTree(xs)
  //println(xs.mkString(" "))
  for (q <- qs) {
    //println(q, segTree.query(q.l, q.r))
    if (segTree.query(q.l, q.r) != q.q) fail(2)
  }

  println("YES")
  println(xs.mkString(" "))

  def fail(x: Int) = {
    println("NO")
    System.exit(0)
  }
}

import java.io.PrintWriter
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

object Main {

  val br = new BufferedReader(new InputStreamReader(System.in))
  var out = new PrintWriter(System.out)
  var st = new StringTokenizer(br.readLine())

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

  def nextDouble = next.toDouble
  def nextLong = next.toLong
  def nextInt = next.toInt

  class SegmentTree(n: Int, op: (Int, Int) => Int, identityValue: Int = 0, nullValue: Int = 0, a: Array[Int] = null) {

    val tree = new Array[Int](n<<2)
    val prop = new Array[Int](n<<2)

    if (a != null) build(1,1,n)

    def build(x: Int, tl: Int, tr: Int): Unit = {
      if (tl > tr) return
      if (tl == tr) {
        tree(x) = a(tl)
        return
      }
      build(x+x,tl,(tl+tr)>>1)
      build(x+x+1,((tl+tr)>>1)+1,tr)
      tree(x) = op(tree(x+x), tree(x+x+1))
    }

    def push(x: Int, tl: Int, tr: Int) = {
      if (prop(x) != nullValue) {
        tree(x) = op(tree(x), prop(x))
        if (tl != tr) {
          prop(x+x) = op(prop(x+x), prop(x))
          prop(x+x+1) = op(prop(x+x+1), prop(x))
        }
        prop(x) = nullValue
      }
    }

    def getInternal(x: Int, tl: Int, tr: Int, l: Int, r: Int): Int = {
      if (tl > tr || tl > r || tr < l) return identityValue
      push(x, tl, tr)
      if (tl >= l && tr <= r) return tree(x)
      val q1 = getInternal(x+x, tl, (tl+tr)>>1, l, r)
      val q2 = getInternal(x+x+1, ((tl+tr)>>1)+1, tr, l, r)
      op(q1,q2)
    }

    def setInternal(x: Int, tl: Int, tr: Int, l: Int, r: Int, z: Int): Unit = {
      push(x, tl, tr)
      if (tl > tr || tl > r || tr < l) return
      if (tl >= l && tr <= r) {
        tree(x) = op(tree(x), z)
        if (tl != tr) {
          prop(x+x) = op(prop(x+x), z)
          prop(x+x+1) = op(prop(x+x+1), z)
        }
        return
      }
      setInternal(x+x, tl, (tl+tr)>>1, l, r, z)
      setInternal(x+x+1, ((tl+tr)>>1)+1, tr, l, r, z)
      tree(x) = op(tree(x+x), tree(x+x+1))
    }

    def getModifiedArray = {
      val ret = new Array[Int](n+1)
      for (i <- 1 to n)
        ret(i) = getInternal(1,1,n,i,i)
      ret
    }

    def get(l: Int, r: Int) = getInternal(1,1,n,l,r)
    def set(l: Int, r: Int, z: Int) = setInternal(1,1,n,l,r,z)

  }

  def main(args: Array[String]) {
    val n = nextInt
    val m = nextInt

    val l = new Array[Int](m+1)
    val r = new Array[Int](m+1)
    val q = new Array[Int](m+1)

    for (i <- 1 to m) {
      l(i) = nextInt
      r(i) = nextInt
      q(i) = nextInt
    }

    val stOr = new SegmentTree(n, (a,b) => a|b)

    for (i <- 1 to m)
      stOr.set(l(i),r(i),q(i))

    val a = stOr.getModifiedArray
    val stAnd = new SegmentTree(n, (a,b) => a&b, 0x7fffffff, 0, a)

    for (i <- 1 to m)
      if (stAnd.get(l(i),r(i)) != q(i)) {
        out.println("NO")
        out.close()
        return
      }

    out.println("YES")
    for (i <- 1 to n)
      out.print(a(i)+" ")
    out.close()
  }

}

import java.io.PrintWriter
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

object Main {

  val br = new BufferedReader(new InputStreamReader(System.in))
  val MAX = 100111
  val tree = new Array[Int](MAX<<2)
  val prop = new Array[Int](MAX<<2)
  val tree2 = new Array[Int](MAX<<2)
  var out = new PrintWriter(System.out)
  var st = new StringTokenizer(br.readLine())

  def nextDouble = next.toDouble

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

  def nextLong = next.toLong

  def nextInt = next.toInt

  def or(x: Int, tl: Int, tr: Int, l: Int, r: Int, mask: Int): Unit = {

    if (prop(x) != 0) {
      tree(x) |= prop(x)
      if (tl != tr) {
        prop(x+x) |= prop(x)
        prop(x+x+1) |= prop(x)
      }
      prop(x) = 0
    }

    if (tl > tr || tl > r || tr < l)
      return
    if (tl >= l && tr <= r) {
      tree(x) |= mask
      if (tl!=tr) {
        prop(x+x) |= mask
        prop(x+x+1) |= mask
      }
      return
    }

    or(x+x, tl, (tl+tr)>>1, l, r, mask)
    or(x+x+1, ((tl+tr)>>1)+1, tr, l, r, mask)

    tree(x) = tree(x+x) | tree(x+x+1)
  }

  def get(x: Int, tl: Int, tr: Int, l: Int, r: Int): Int = {
    if (tl > tr || tl > r || tr < l) return 0

    if (prop(x) != 0) {
      tree(x) |= prop(x)
      if (tl != tr) {
        prop(x+x) |= prop(x)
        prop(x+x+1) |= prop(x)
      }
      prop(x) = 0
    }

    if (tl >= l && tr <= r) return tree(x)

    val q1 = get(x+x, tl, (tl+tr)>>1, l, r)
    val q2 = get(x+x+1, ((tl+tr)>>1)+1, tr, l, r)
    q1 | q2
  }

  def build(x: Int, tl: Int, tr: Int, a: Array[Int]): Unit = {
    if (tl > tr) return
    if (tl == tr) {
      tree2(x) = a(tl)
      return
    }
    build(x+x,tl,(tl+tr)>>1, a)
    build(x+x+1,((tl+tr)>>1)+1,tr, a)
    tree2(x) = tree2(x+x) & tree2(x+x+1)
  }

  def get2(x: Int, tl: Int, tr: Int, l: Int, r: Int): Int = {
    if (tl > tr || tl > r || tr < l) return 0
    if (tl >= l && tr <= r) return tree2(x)
    val q1 = get2(x+x, tl, (tl+tr)>>1, l, r)
    val q2 = get2(x+x+1, ((tl+tr)>>1)+1, tr, l, r)
    q1 & q2
  }

  def main(args: Array[String]) {
    val n = nextInt
    val m = nextInt

    val l = new Array[Int](m+1)
    val r = new Array[Int](m+1)
    val q = new Array[Int](m+1)

    for (i <- 1 to m) {
      l(i) = nextInt
      r(i) = nextInt
      q(i) = nextInt
    }

    for (i <- 1 to m)
      or(1,1,n,l(i),r(i),q(i))

    val a = new Array[Int](n+1)
    for (i <- 1 to n)
      a(i) = get(1,1,n,i,i)

    build(1,1,n,a)

    for (i <- 1 to m)
      if (get2(1,1,n,l(i),r(i)) != q(i)) {
        out.println("NO")
        out.close()
        return
      }

    out.println("YES")
    for (i <- 1 to n)
      out.print(a(i)+" ")
    out.close()
  }

}

import scala.collection._

object B extends App {

  case class SegTree(as: Array[Int]) {

    val n = as.length
    val t = Array.ofDim[Int](4 * Integer.highestOneBit(n))
    build(1, 0, n - 1)
//println(t.mkString(" "))
    def build(v: Int, tl: Int, tr: Int) {
      if (tl == tr) t(v) = as(tl)
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        build(v2, tl, tm)
        build(v2 + 1, tm + 1, tr)
        t(v) = t(v2) & t(v2 + 1)
      }
    }

    def query(l: Int, r: Int, v: Int = 1, tl: Int = 0, tr: Int = n - 1): Int = {
      //println(l, r, v, tl, tr)
      if (l > r) Int.MaxValue
      else if (l == tl && r == tr) t(v)
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        //println(query(l, Math.min(r, tm), v2, tl, tm), query(Math.max(l, tm + 1), r, v2 + 1, tm + 1, tr))
        query(l, Math.min(r, tm), v2, tl, tm) &
          query(Math.max(l, tm + 1), r, v2 + 1, tm + 1, tr)
      }
    }

    def update(pos: Int, delta: Int, v: Int = 1, tl: Int = 0, tr: Int = n - 1) {
      if (tl == tr) t(v) += delta
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        if (pos <= tm) update(pos, delta, v2, tl, tm)
        else update(pos, delta, v2 + 1, tm + 1, tr)
        t(v) = t(v2) + t(v2 + 1)
      }
    }

  }

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  case class Q(l: Int, r: Int, q: Int)

  val qs = Array.ofDim[Q](m)

  for (i <- 0 until m) {
    val Array(l, r, q) = readInts(3)
    qs(i) = Q(l - 1, r - 1, q)
  }

  //val sorted = qs.sortBy(_.l)

  val xs = Array.fill(n) { 0 }

  val bits = Array.fill(31) { 0 }

  val qByL = new mutable.PriorityQueue[Q]()(Ordering.by(- _.l))
  qByL ++= qs

  val qByR = new mutable.PriorityQueue[Q]()(Ordering.by(- _.r))

  var pos = 0
  for (i <- 0 until n) {
    while (qByR.nonEmpty && qByR.head.r < i) {
      val qr = qByR.dequeue
      //println(qr)
      for (b <- 0 to 30) {
        if ((qr.q & (1 << b)) > 0) bits(b) -= 1
      }
    }
    while (qByL.nonEmpty && qByL.head.l == i) {
      val ql = qByL.dequeue
      for (b <- 0 to 30) {
        if ((ql.q & (1 << b)) > 0) {
          bits(b) += 1
        }
      }
      qByR += ql
    }
    //println(bits.mkString(" "))
    for (b <- 0 to 30) {
      if (bits(b) < 0) fail(1)
      else if (bits(b) > 0) xs(i) |= (1 << b)
    }
  }

  val segTree = SegTree(xs)
  //println(xs.mkString(" "))
  for (q <- qs) {
    //println(q, segTree.query(q.l, q.r))
    if (segTree.query(q.l, q.r) != q.q) fail(2)
  }

  println(xs.mkString(" "))

  def fail(x: Int) = {
    println(x)
    println("NO")
    System.exit(0)
  }
}

import scala.collection._

object B extends App {

  case class SegTree(as: Array[Int]) {

    val n = as.length
    val t = Array.ofDim[Int](4 * Integer.highestOneBit(n))
    build(1, 0, n - 1)
//println(t.mkString(" "))
    def build(v: Int, tl: Int, tr: Int) {
      if (tl == tr) t(v) = as(tl)
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        build(v2, tl, tm)
        build(v2 + 1, tm + 1, tr)
        t(v) = t(v2) & t(v2 + 1)
      }
    }

    def query(l: Int, r: Int, v: Int = 1, tl: Int = 0, tr: Int = n - 1): Int = {
      //println(l, r, v, tl, tr)
      if (l > r) Int.MaxValue
      else if (l == tl && r == tr) t(v)
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        //println(query(l, Math.min(r, tm), v2, tl, tm), query(Math.max(l, tm + 1), r, v2 + 1, tm + 1, tr))
        query(l, Math.min(r, tm), v2, tl, tm) &
          query(Math.max(l, tm + 1), r, v2 + 1, tm + 1, tr)
      }
    }

    def update(pos: Int, delta: Int, v: Int = 1, tl: Int = 0, tr: Int = n - 1) {
      if (tl == tr) t(v) += delta
      else {
        val tm = (tl + tr) >> 1
        val v2 = v << 1
        if (pos <= tm) update(pos, delta, v2, tl, tm)
        else update(pos, delta, v2 + 1, tm + 1, tr)
        t(v) = t(v2) + t(v2 + 1)
      }
    }

  }

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m) = readInts(2)

  case class Q(l: Int, r: Int, q: Int)

  val qs = Array.ofDim[Q](m)

  for (i <- 0 until m) {
    val Array(l, r, q) = readInts(3)
    qs(i) = Q(l - 1, r - 1, q)
  }

  //val sorted = qs.sortBy(_.l)

  val xs = Array.fill(n) { 0 }

  val bits = Array.fill(31) { 0 }

  val qByL = new mutable.PriorityQueue[Q]()(Ordering.by(- _.l))
  qByL ++= qs

  val qByR = new mutable.PriorityQueue[Q]()(Ordering.by(- _.r))

  var pos = 0
  for (i <- 0 until n) {
    while (qByR.nonEmpty && qByR.head.r < i) {
      val qr = qByR.dequeue
      //println(qr)
      for (b <- 0 to 30) {
        if ((qr.q & (1 << b)) > 0) bits(b) -= 1
      }
    }
    while (qByL.nonEmpty && qByL.head.l == i) {
      val ql = qByL.dequeue
      for (b <- 0 to 30) {
        if ((ql.q & (1 << b)) > 0) {
          bits(b) += 1
        }
      }
      qByR += ql
    }
    //println(bits.mkString(" "))
    for (b <- 0 to 30) {
      if (bits(b) < 0) fail(1)
      else if (bits(b) > 0) xs(i) |= (1 << b)
    }
  }

  val segTree = SegTree(xs)
  //println(xs.mkString(" "))
  for (q <- qs) {
    //println(q, segTree.query(q.l, q.r))
    if (segTree.query(q.l, q.r) != q.q) fail(2)
  }

  println("YES")
  println(xs.mkString(" "))

  def fail(x: Int) = {
    println(x)
    println("NO")
    System.exit(0)
  }
}


object Main extends App {
  val Array(n,t) = readLine.split(" ").map(_.toInt)
  val a = readLine.split(" ").map(_.toInt)
  println(a.view.map(86400 - _).scanLeft(0)(_ + _)
    .zipWithIndex.filter(x => x._1 >= t).head._2)
}

import scala.io.StdIn._

object Codeforces extends App {
  val seconds = 86400
  var Array(n, t) = readLine split ' ' map(_.toInt)
  val time = readLine split ' ' map(_.toInt)
  for (i <- 0 until n) {
    t = math.max(0, t - (seconds - time(i)))
    if (t == 0) {
      println(i + 1)
      System.exit(0)
    }
  }
}


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

  val t = readInt()

  (0 until t).foreach { _ =>
    val Array(n, x) = readLine().split(" ").map(_.toInt)
    val an          = readLine().split(" ").map(_.toInt - x)

    val ans =
      if (an.forall(_ == 0)) 0
      else if (an.sum == 0 || an.contains(0)) 1
      else 2

    println(ans)
  }
}

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

import scala.annotation.tailrec

object C {
  def solve(in: Input, out: PrintWriter): Unit = {
    for (_ <- 0 until in.nextInt()) {
      val n, killJoy = in.nextInt()
      val others = Array.fill(n)(in.nextInt())
      if (others.count(_ == killJoy) == n) {
        out.println(0)
      } else if (others.sum == n * killJoy || others.contains(killJoy)) {
        out.println(1)
      } else {
        out.println(2)
      }
    }
  }

  private def makeTests(): IndexedSeq[(String, String, String)] = IndexedSeq(
    ("3\n2 69\n68 70\n6 4\n4 4 4 4 4 4\n9 38\n-21 83 50 -59 -77 15 -71 -78 20", "1\n0\n2", "Sample"),
    ("1\n3 10\n10 1 1", "1", "Tricky case")
  )

  def main(args: Array[String]): Unit = {
    if (args.length == 0) {
      val input = new Input(new InputStreamReader(System.in))
      val output = new PrintWriter(System.out)
      solve(input, output)
      output.close()
      input.close()
    } else {
      for ((in, ans, name) <- makeTests()) {
        val input = new Input(new StringReader(in))
        val output = new StringWriter()
        val pwOutput = new PrintWriter(output)
        val t0 = System.nanoTime()
        solve(input, pwOutput)
        val time = System.nanoTime() - t0
        pwOutput.close()
        output.close()
        val result = output.toString
        if (ans.trim != result.trim) {
          throw new RuntimeException(s"Test '$name': expected '$ans' found '$result'")
        } else {
          println(s"Test '$name' was solved in ${time / 1e9} seconds")
        }
      }
    }
  }

  class Input(stream: Reader) extends Closeable {
    private[this] val reader = new BufferedReader(stream)
    private[this] var tokenizer = new StringTokenizer(reader.readLine())

    @tailrec
    final def nextToken(): String = if (tokenizer.hasMoreTokens) tokenizer.nextToken() else {
      tokenizer = new StringTokenizer(reader.readLine())
      nextToken()
    }

    def nextInt(): Int = nextToken().toInt
    def nextLong(): Long = nextToken().toLong
    def nextDouble(): Double = nextToken().toDouble

    override def close(): Unit = reader.close()
  }
}

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

import scala.annotation.tailrec

object C {
  def solve(in: Input, out: PrintWriter): Unit = {
    for (_ <- 0 until in.nextInt()) {
      val n, killJoy = in.nextInt()
      val others = Array.fill(n)(in.nextInt())
      if (others.count(_ == killJoy) == n) {
        out.println(0)
      } else if (others.sum == n * killJoy) {
        out.println(1)
      } else {
        out.println(2)
      }
    }
  }

  private def makeTests(): IndexedSeq[(String, String, String)] = IndexedSeq(
    ("3\n2 69\n68 70\n6 4\n4 4 4 4 4 4\n9 38\n-21 83 50 -59 -77 15 -71 -78 20", "1\n0\n2", "Sample")
  )

  def main(args: Array[String]): Unit = {
    if (args.length == 0) {
      val input = new Input(new InputStreamReader(System.in))
      val output = new PrintWriter(System.out)
      solve(input, output)
      output.close()
      input.close()
    } else {
      for ((in, ans, name) <- makeTests()) {
        val input = new Input(new StringReader(in))
        val output = new StringWriter()
        val pwOutput = new PrintWriter(output)
        val t0 = System.nanoTime()
        solve(input, pwOutput)
        val time = System.nanoTime() - t0
        pwOutput.close()
        output.close()
        val result = output.toString
        if (ans.trim != result.trim) {
          throw new RuntimeException(s"Test '$name': expected '$ans' found '$result'")
        } else {
          println(s"Test '$name' was solved in ${time / 1e9} seconds")
        }
      }
    }
  }

  class Input(stream: Reader) extends Closeable {
    private[this] val reader = new BufferedReader(stream)
    private[this] var tokenizer = new StringTokenizer(reader.readLine())

    @tailrec
    final def nextToken(): String = if (tokenizer.hasMoreTokens) tokenizer.nextToken() else {
      tokenizer = new StringTokenizer(reader.readLine())
      nextToken()
    }

    def nextInt(): Int = nextToken().toInt
    def nextLong(): Long = nextToken().toLong
    def nextDouble(): Double = nextToken().toDouble

    override def close(): Unit = reader.close()
  }
}


import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  def binarySearch(l: Int, key: Int): Int = {
    var mid = 0
    var low = 0
    var hi = l - 1
    while (low <= hi) {
      mid = (low + hi) >>> 1
      val d = as(ms(mid))
      if (d == key) return mid
      else if (d > key) hi = mid - 1
      else {
        mid += 1
        low = mid
      }
    }
    return -mid - 1 // insertion point is -mid - 1
  }

  val ms = Array.ofDim[Int](n)
  //val ps = Array.ofDim[Int](n)

  var l = 0
  for (i <- 0 until n) {
    var j = binarySearch(l, as(i))
    if (j < 0) j = -j - 1
    //ps(i) = ms(j)
    if (j == l || as(i) < as(ms(j))) {
      ms(j) = i
      l = math.max(l, j + 1)
    }
  }

  println(l)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  def lis(as: Array[Int]): Array[Int] = {

    val bestLen = Array.ofDim[Int](n)

    def binarySearch(l: Int, key: Int): (Boolean, Int) = {
      var mid = 0
      var low = 0
      var hi = l - 1
      while (low <= hi) {
        mid = (low + hi) >>> 1
        val d = as(bestLen(mid))
        if (d == key) return (true, mid)
        else if (d > key) hi = mid - 1
        else {
          mid += 1
          low = mid
        }
      }
      (false, mid)  // not found - return insertion point
    }

  @annotation.tailrec
  def binSearch(lo: Int, hi: Int, f: Int => Int): Int = {
    if (hi < lo) lo
    else {
      val mid = lo + (hi - lo) / 2
      val fmid = f(mid)
      if (fmid == 0) mid
      else if (fmid < 0) binSearch(mid + 1, hi, f)
      else binSearch(lo, mid - 1, f)
    }
  }

    var len = 0
    for (i <- 0 until n) {
      val j = binSearch(0, len - 1, k => as(bestLen(k)) - as(i))
      if (j == len || as(i) < as(bestLen(j))) {
        bestLen(j) = i
        len = math.max(len, j + 1)
      }
    }

    bestLen.take(len).map(as(_))
  }

  println(lis(as).length)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  @annotation.tailrec
  def binSearch(low: Int, hi: Int, f: Int => Int): (Boolean, Int) = {
    if (hi < low) (false, low) // not found - return insertion point
    else {
      val mid = (low + hi) >>> 1
      val fmid = f(mid)
      if (fmid == 0) (true, mid)
      else if (fmid < 0) binSearch(mid + 1, hi, f)
      else binSearch(low, mid - 1, f)
    }
  }

  def lis(as: Array[Int]): Array[Int] = {

    val minByLen = Array.ofDim[Int](n)
    var len = 0

    for (a <- as) {
      val (_, j) = binSearch(0, len - 1, minByLen(_) - a)
      if (j == len || a < minByLen(j)) {
        minByLen(j) = a
        if (j + 1 > len) len = j + 1
      }
    }

    minByLen.take(len)
  }

  println(lis(as).length)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  def lis(as: Array[Int]): Array[Int] = {

    val bestLen = Array.ofDim[Int](n)

    def binarySearch(l: Int, key: Int): (Boolean, Int) = {
      var mid = 0
      var low = 0
      var hi = l - 1
      while (low <= hi) {
        mid = (low + hi) >>> 1
        val d = as(bestLen(mid))
        if (d == key) return (true, mid)
        else if (d > key) hi = mid - 1
        else {
          mid += 1
          low = mid
        }
      }
      (false, mid)  // not found - return insertion point
    }

    var len = 0
    for (i <- 0 until n) {
      val (_, j) = binarySearch(len, as(i))
      if (j == len || as(i) < as(bestLen(j))) {
        bestLen(j) = i
        len = math.max(len, j + 1)
      }
    }

    bestLen.take(len).map(as(_))
  }

  println(lis(as).length)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  @annotation.tailrec
  def binSearch(low: Int, hi: Int, f: Int => Int, target: Int): (Boolean, Int) = {
    if (hi < low) (false, low) // not found - return insertion point
    else {
      val mid = (low + hi) >>> 1
      val fmid = f(mid)
      if (fmid == target) (true, mid)
      else if (fmid < target) binSearch(mid + 1, hi, f, target)
      else binSearch(low, mid - 1, f, target)
    }
  }

  def lis(as: Array[Int]): IndexedSeq[Int] = {

    val minByLen = new mutable.ArrayBuffer[Int](n)

    for (a <- as) {
      val (_, j) = binSearch(0, minByLen.length - 1, minByLen(_), a)
      if (j == minByLen.length) minByLen += a
      else if (a < minByLen(j)) minByLen(j) = a
    }

    minByLen
  }

  println(lis(as).length)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  @annotation.tailrec
  def binSearch(low: Int, hi: Int, f: Int => Int, target: Int): (Boolean, Int) = {
    if (hi < low) (false, low) // not found - return insertion point
    else {
      val mid = (low + hi) >>> 1
      val fmid = f(mid)
      if (fmid == target) (true, mid)
      else if (fmid < target) binSearch(mid + 1, hi, f, target)
      else binSearch(low, mid - 1, f, target)
    }
  }

  def lis(as: Array[Int]): IndexedSeq[Int] = {

    val minByLen = new mutable.ArrayBuffer[Int](n)

    for (a <- as) {
      val (_, j) = binSearch(0, minByLen.length - 1, minByLen, a)
      if (j == minByLen.length) minByLen += a
      else if (a < minByLen(j)) minByLen(j) = a
    }

    minByLen
  }

  println(lis(as).length)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val n = readString.toInt
  val as = readInts(n)

  @annotation.tailrec
  def binSearch(low: Int, hi: Int, f: Int => Int): (Boolean, Int) = {
    if (hi < low) (false, low) // not found - return insertion point
    else {
      val mid = (low + hi) >>> 1
      val fmid = f(mid)
      if (fmid == 0) (true, mid)
      else if (fmid < 0) binSearch(mid + 1, hi, f)
      else binSearch(low, mid - 1, f)
    }
  }

  def lis(as: Array[Int]): Array[Int] = {

    val bestLen = Array.ofDim[Int](n)
    var len = 0

    for (i <- 0 until n) {
      val (_, j) = binSearch(0, len - 1, k => as(bestLen(k)) - as(i))
      if (j == len || as(i) < as(bestLen(j))) {
        bestLen(j) = i
        len = math.max(len, j + 1)
      }
    }

    bestLen.take(len).map(as(_))
  }

  println(lis(as).length)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }
  def readBigs(n: Int) = { val t = tokenizeLine; Array.fill(n)(BigInt(t.nextToken)) }

  val n = readString.toInt
  val as = readInts(n)

  var c = 0
  for (i <- 0 until n - 1; if as(i) <= as(i + 1)) c += 1

  println(if (c == 0) 0 else c + 1)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }
  def readBigs(n: Int) = { val t = tokenizeLine; Array.fill(n)(BigInt(t.nextToken)) }

  val n = readString.toInt
  val as = readInts(n)

  var c = 1
  for (i <- 0 until n - 1; if as(i) <= as(i + 1)) c += 1

  println(c)
}


//package codeforce
import scala.io.StdIn

object CalcDrawNum extends App {
  val momentsNum = StdIn.readLine().toInt
  val array = scala.collection.mutable.ArrayBuffer[(Long, Long)]()
  for (_ <- 0 until momentsNum) {
    val scores = StdIn.readLine().split(" ")
    array.append((scores(0).toLong, scores(1).toLong))
  }
  var from = (0L, 0L)
  var drawTimes = 1L
  var lastDraw = (0L, 0L)
  for (s <- array) {
    val (dt, d) = calcDrawTimes(from, s, lastDraw)
    drawTimes += dt
    from = s
    lastDraw = d
  }
  println(drawTimes)

  def calcDrawTimes(from: (Long, Long), to: (Long, Long), lastDraw: (Long, Long)): (Long, (Long, Long)) = {
    var curDraw = lastDraw
    val fromMax = scala.math.max(from._1, from._2)
    val toMin = scala.math.min(to._1,to._2)
    var times = 0L
    if (fromMax <= toMin){
      times += toMin - fromMax
      if ((fromMax,fromMax) != lastDraw){
        times += 1
      }
      curDraw = (toMin,toMin)
    }
    (times,curDraw)
  }
}

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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  def solve(): Unit = {
    val N = ni()
    val (a, b) = na2(N)

    var ans = 1 // (0:0)
    REP(N) { i =>
      val a0 = if (i ==0) 0 else a(i - 1)
      val a1 = a(i)
      val b0 = if (i ==0) 0 else b(i - 1)
      val b1 = b(i)

      // 点数が動いた時だけ考慮すればいい
//      if (a1 > a0 || b1 > b0) {
      val rg_a = (a0, a1)
      val rg_b = (b0, b1)
      val rg = (max(rg_a._1, rg_b._1), min(rg_a._2, rg_b._2))

      val prevDraw = a0 == b0 // 前回ドローだと２回足してしまうので引かないといけない

      val len = max(0, rg._2 - rg._1 + 1) - (if (prevDraw) 1 else 0)
//      debug(s"$i $rg_a $rg_b $rg $len")
      ans += len
//      }
    }

//    if (a(N - 1) == 0 && b(N - 1) == 0) ans += 1

    out.println(ans)
  }

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

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

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

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

  def debug(num: Long): Unit = {
    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 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 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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  def solve(): Unit = {
    val N = ni()
    val (a, b) = na2(N)

    var ans = 0
    REP(N) { i =>
      val a0 = if (i ==0) 0 else a(i - 1)
      val a1 = a(i)
      val b0 = if (i ==0) 0 else b(i - 1)
      val b1 = b(i)

      // 点数が動いた時だけ考慮すればいい
      if (a1 > a0 || b1 > b0) {
        val rg_a = (a0, a1)
        val rg_b = (b0, b1)
        val rg = (max(rg_a._1, rg_b._1), min(rg_a._2, rg_b._2))
        val len = max(0, rg._2 - rg._1 + 1)
        debug(s"$i $rg_a $rg_b $rg $len")
        ans += len
      }
    }

    // やべえうまくいかない
    if (a(N - 1) == 0 && b(N - 1) == 0) ans += 1

    out.println(ans)
  }

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

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

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

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

  def debug(num: Long): Unit = {
    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 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
  }
}

import scala.io.StdIn

object CalcDrawNum extends App {
  val momentsNum = StdIn.readLine().toInt
  val array = scala.collection.mutable.ArrayBuffer[(Int, Int)]()
  for (_ <- 0 until momentsNum) {
    val scores = StdIn.readLine().split(" ")
    array.append((scores(0).toInt, scores(1).toInt))
  }
  var from = (0, 0)
  var drawTimes = 1
  var lastDraw = (0, 0)
  for (s <- array) {
    val (dt, d) = calcDrawTimes(from, s, lastDraw)
    drawTimes += dt
    from = s
    lastDraw = d
  }
  println(drawTimes)

  def calcDrawTimes(from: (Int, Int), to: (Int, Int), lastDraw: (Int, Int)): (Int, (Int, Int)) = {
    var drawTimes = 0
    var draw: (Int, Int) = (0, 0)
    if (from._1 != from._2) {
      val fromMax = scala.math.max(from._1, from._2)
      draw = (fromMax, fromMax)
    } else {
      draw = (from._1 + 1, from._2 + 1)
    }
    while (draw._1 <= to._1 && draw._2 <= to._2 && draw != lastDraw) {
      drawTimes += 1
      draw = (draw._1 + 1, draw._2 + 1)
    }
    (drawTimes, draw)
  }
}


import java.util.Scanner
import scala.collection.mutable

object Main {

  def main(args: Array[String]): Unit = {
    val scan = new Scanner(System.in)
    val n = scan.nextInt
    scan.nextLine
    var grid = mutable.ArrayBuffer[String]()
    for (i <- 0 until n) {
      grid += scan.nextLine
    }
    var ans = 0
    for {
      i <- 1 until n - 1
      j <- 1 until n - 1
    } if (isCross(grid, i, j)) ans += 1
    println(ans)
  }

  def isCross(g: mutable.ArrayBuffer[String], x: Int, y: Int): Boolean = {
    g(x)(y) == 'X' &&
      g(x - 1)(y - 1) == 'X' &&
      g(x + 1)(y - 1) == 'X' &&
      g(x - 1)(y + 1) == 'X' &&
      g(x + 1)(y + 1) == 'X'
  }
}

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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  def solve(): Unit = {
    val N = ni()
    val M = nm_c(N, N)
    var ans = 0
    val X = Array(0, -1, -1, 1, 1)
    val Y = Array(0, -1, 1, -1, 1)

    def test(x: Int, y: Int) = {
      0 until 5 forall { i =>
        0 <= x + X(i) && x + X(i) < N &&
        0 <= y + Y(i) && y + Y(i) < N &&
        M(X(i) + x)(Y(i) + y) == 'X'
      }
    }

    REP(N) { i =>
      REP(N) { j =>
        if (test(i, j)) ans += 1
      }
    }

    out.println(ans)
  }

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

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

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

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

  def debug(num: Long): Unit = {
    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 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 CF_528_2_C {

  type In = Seq[(Int, Int)]
  type Out = String

  def solve(in: In): Out = {
    val Seq(a, b, c) = in.sorted
    def right(a: (Int, Int), b: Int) = (a._1 to b).map(x => (x, a._2))
    def up(a: (Int, Int), b: Int) = if(b >= a._2) (a._2 to b).map(y => (a._1, y)) else (b to a._2).map(y => (a._1, y))
    // Go across, then up/down, then up/down, then across
    val squares = (right(a, b._1) ++ up((b._1, a._2), b._2) ++ up(b, c._2) ++ right((b._1, c._2), c._1)).toSet
    squares.size.toString + "\n" + squares.map { case (a, b) => s"$a $b" }.mkString("\n")
  }

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Specify Input and Output formats on RHS here:

  def formatIn(i: Input)    = i.getLines.map{_.split(" ")}.map{case Array(a,b) => (a.toInt, b.toInt)}
  def formatOut(out: Out): String  = out.toString

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Boilerplate & utility methods that don't change:

  import java.util.Scanner
  import java.io.InputStream
  import language.implicitConversions

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  class Input(sc: Scanner) {
    // This class is useful for convenience, and for mock stdin in test scripts
    // Remember to call nextLine to advance past the newline character (if required) before taking a whole line
    def this(i: InputStream) =       this(new Scanner(i))
    def this(s: String) =            this(new Scanner(s.stripMargin))
    def int =                        sc.nextInt()
    def long =                       sc.nextLong()
    def double =                     sc.nextDouble()
    def nextLine =                   sc.nextLine()
    def collect[T](f: String => T) = sc.nextLine().split(" ").toVector.map(f)
    def intSeq =                     collect(_.toInt)
    def doubleSeq =                  collect(_.toDouble)
    def getLines: Vector[String] =   if (!sc.hasNextLine) Vector.empty
                                     else sc.nextLine +: getLines
    def solveVal: Out =              (solve _).tupled(formatIn(this))
    def solveStr: String =           formatOut(solveVal)
  }

  // Ceiling division for Int & Long
  def ceil(a: Int, b: Int)   = (a - 1)/b + 1
  def ceil(a: Long, b: Long) = (a - 1)/b + 1

  // A frequently used number
  val modulo = 1000000007

  // For convenience in test scripts, treat a String as an Input
  implicit def stringToInput(s: String): Input = new Input(s)
  // Required to make `solveVal` work when input has just 1 value (i.e. not a Tuple)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) {
    def tupled: T => R = x => f(x)
  }
}

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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  sealed trait Move[A] {
    val l: Int
    val r: Int
    def axis: Int
    def len = r - l + 1
    def extend(l: Int, r: Int): A
  }
  case class MoveX(y: Int, l: Int, r: Int) extends Move[MoveX] {
    override def axis = y
    def extend(l: Int, r: Int) = MoveX(y, l, r)
  }
  case class MoveY(x: Int, l: Int, r: Int) extends Move[MoveY] {
    override def axis = x
    override def extend(l: Int, r: Int) = MoveY(x, l, r)
  }
  case class Coord(x: Int, y: Int)
  case class Path(c1: Coord, c2: Coord)

  case class Moves(xs: Seq[MoveX], ys: Seq[MoveY])

  def solve(): Unit = {
    val c1, c2, c3 = Coord(ni(), ni())
    val C = Array(c1, c2, c3)

    def moveX(p1: Coord, p2: Coord) = {
      assert(p1.y == p2.y)
      MoveX(p1.y, min(p1.x, p2.x), max(p1.x, p2.x))
    }

    def moveY(p1: Coord, p2: Coord) = {
      assert(p1.x == p2.x)
      MoveY(p1.x, min(p1.y, p2.y), max(p1.y, p2.y))
    }

    def move(p: Path): (MoveX, MoveY) = {
      val Path(c1, c2) = p
      val xmin = min(c1.x, c2.x)
      val xmax = max(c1.x, c2.x)
      val ymin = min(c1.y, c2.y)
      val ymax = max(c1.y, c2.y)
      val lt = Coord(xmin, ymax)
      val rt = Coord(xmax, ymax)
      val lb = Coord(xmin, ymin)
      val rb = Coord(xmax, ymin)

      // 時計回り
      val P = Array(lt, rt, rb, lb)
      val s = P.indexOf(c1)
      val m = (s + 1) % 4
      val e = (s + 2) % 4

      // X -> Y
      if (s % 2 == 0) {
        (moveX(P(s), P(m)), moveY(P(m), P(e)))

      // Y -> X
      } else {
        (moveX(P(m), P(e)), moveY(P(s), P(m)))
      }
    }

    def len(moves: Moves) = {
      var len = (moves.xs ++ moves.ys).map(_.len).sum
      moves.xs foreach { x =>
        len -= moves.ys.count(y => y.l <= x.y && x.y <= y.r && x.l <= y.x && y.x <= x.r)
      }
      len
    }

    def merge2[A <: Move[A]](m1: A, m2: A): Seq[A] = {
      if (m1.axis == m2.axis) {
        val l = max(m1.l, m2.l)
        val r = min(m1.r, m2.r)
        // 重複部分あり
        if (l <= r) {
          Seq(m1.extend(min(m1.l, m2.l), max(m1.r, m2.r)))
        } else {
          Seq(m1, m2)
        }
      } else {
        Seq(m1, m2)
      }
    }

    def merge4(arg: (MoveX, MoveX, MoveY, MoveY)): Moves = {
      val (x1, x2, y1, y2) = arg

      val xs = merge2(x1, x2)
      val ys = merge2(y1, y2)

      Moves(xs, ys)
    }

    val ans = (for {
      c1 <- C
      c2 <- C
      c3 <- C
      if Set(c1, c2, c3).size == 3
      (x1, y1) = move(Path(c1, c2))
      (x2, y2) = move(Path(c2, c3))
    } yield merge4((x1, x2, y1, y2))).minBy(len)

    out.println(len(ans))

    val flg = Array.ofDim[Boolean](1001, 1001)

    (ans.xs ++ ans.ys) foreach { move =>
      REP(move.len) { i =>
        move match {
          case MoveX(y, l, r) =>
            if (!flg(l + i)(y)) {
              out.println(s"${l + i} $y")
              flg(l + i)(y) = true
            }

          case MoveY(x, l, r) =>
            if (!flg(x)(l + i)) {
              out.println(s"$x ${l + i}")
              flg(x)(l + i) = true
            }
        }
      }
    }
  }

  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 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 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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  case class Coord(x: Int, y: Int)
  case class Path(p1: Coord, p2: Coord)
  type Grid = Array[Array[Boolean]]

  def solve(): Unit = {
    val MAX = 1001
//    val MAX = 3
    val C1, C2, C3 = Coord(ni(), ni())
    val E = Array(Path(C1, C2), Path(C2, C3), Path(C3, C1))

    def cnt(g: Grid) = g.map(_.count(identity)).sum

    def paintLine(c1: Coord, c2: Coord, g: Grid): Unit = {
      if (c1.x == c2.x) {
        val iniY = min(c1.y, c2.y)
        REP(abs(c1.y - c2.y) + 1, iniY) { y =>
          g(c1.x)(y) = true
        }
      } else if (c1.y == c2.y){
        val iniX = min(c1.x, c2.x)
        REP(abs(c1.x - c2.x) + 1, iniX) { x =>
          g(x)(c1.y) = true
        }
      } else {
        throw new IllegalArgumentException(s"$c1 $c2")
      }
    }

    def onLine(p: Path) = p.p1.x == p.p2.x || p.p1.y == p.p2.y

    def paint(p: Path, g: Grid, flg: Boolean): Unit = {
      if (onLine(p)) {
        paintLine(p.p1, p.p2, g)
      } else {
        val lt = Coord(min(p.p1.x, p.p2.x), max(p.p1.y, p.p2.y))
        val rt = Coord(max(p.p1.x, p.p2.x), max(p.p1.y, p.p2.y))
        val lb = Coord(min(p.p1.x, p.p2.x), min(p.p1.y, p.p2.y))
        val rb = Coord(max(p.p1.x, p.p2.x), min(p.p1.y, p.p2.y))
        val Array(c1, c2) = Array(lt, rt, lb, rb).filter(c => c != p.p1 && c != p.p2)
        val c = if (flg) c1 else c2
        paintLine(p.p1, c, g)
        paintLine(p.p2, c, g)
      }
    }

    val memo = Array.ofDim[Boolean](12, MAX, MAX)

    val grids = map(3)(identity).flatMap { i =>
      for {
        x1 <- 0 until 2
        x2 <- 0 until 2
      } yield {
        val Array(p1, p2) = E.zipWithIndex.filterNot(_._2 == i).map(_._1)
        val g = memo(i * 4 + x1 * 2 + x2)
        paint(p1, g, x1 == 1)
        paint(p2, g, x2 == 1)

        g
      }
    }

    val ans = grids.minBy(cnt)
    out.println(cnt(ans))
    REP(MAX) { i =>
      REP(MAX) { j =>
        if (ans(i)(j)) out.println(s"$i $j")
      }
    }
  }

  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 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 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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  case class Coord(x: Int, y: Int)
  case class Path(p1: Coord, p2: Coord)
  type Grid = Array[Array[Boolean]]

  def solve(): Unit = {
    val C1, C2, C3 = Coord(ni(), ni())
    val E = Array(Path(C1, C2), Path(C2, C3), Path(C3, C1))

    def paint(p: Path, g: Grid): Unit = {
      val x = min(p.p1.x, p.p2.x)
      val xLen = abs(p.p1.x - p.p2.x)
      val y = min(p.p1.y, p.p2.y)
      val yLen = abs(p.p1.y - p.p2.y)

      REP(xLen + 1) { i =>
        g(x + i)(y) = true
      }

      REP(yLen + 1) { i =>
        g(x + xLen)(y + i) = true
      }
    }

    val grids = map(3) { i =>
      val Array(e1, e2) = E.zipWithIndex.filterNot(_._2 == i).map(_._1)
      val g = Array.ofDim[Boolean](1001, 1001)
      paint(e1, g)
      paint(e2, g)
      g
    }

    def cnt(g: Grid) = g.map(_.count(identity)).sum

    val ans = grids.minBy(cnt)
    out.println(cnt(ans))
    REP(1001) { i =>
      REP(1001) { j =>
        if (ans(i)(j)) out.println(s"$i $j")
      }
    }
  }

  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 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 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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  sealed trait Move[A] {
    val l: Int
    val r: Int
    def axis: Int
    def len = r - l + 1
    def extend(l: Int, r: Int): A
  }
  case class MoveX(y: Int, l: Int, r: Int) extends Move[MoveX] {
    override def axis = y
    def extend(l: Int, r: Int) = MoveX(y, l, r)
  }
  case class MoveY(x: Int, l: Int, r: Int) extends Move[MoveY] {
    override def axis = x
    override def extend(l: Int, r: Int) = MoveY(x, l, r)
  }
  case class Coord(x: Int, y: Int)
  case class Path(c1: Coord, c2: Coord)

  case class Moves(xs: Seq[MoveX], ys: Seq[MoveY])

  def solve(): Unit = {
    val c1, c2, c3 = Coord(ni(), ni())
    val C = Array(c1, c2, c3)

    def moveX(p1: Coord, p2: Coord) = {
      assert(p1.y == p2.y)
      MoveX(p1.y, min(p1.x, p2.x), max(p1.x, p2.x))
    }

    def moveY(p1: Coord, p2: Coord) = {
      assert(p1.x == p2.x)
      MoveY(p1.x, min(p1.y, p2.y), max(p1.y, p2.y))
    }

    def move(p: Path): (MoveX, MoveY) = {
      val Path(c1, c2) = p
      val xmin = min(c1.x, c2.x)
      val xmax = max(c1.x, c2.x)
      val ymin = min(c1.y, c2.y)
      val ymax = max(c1.y, c2.y)
      val lt = Coord(xmin, ymax)
      val rt = Coord(xmax, ymax)
      val lb = Coord(xmin, ymin)
      val rb = Coord(xmax, ymin)

      // 時計回り
      val P = Array(lt, rt, rb, lb)
      val s = P.indexOf(c1)
      val m = (s + 1) % 4
      val e = (s + 2) % 4

      // X -> Y
      if (s % 2 == 0) {
        (moveX(P(s), P(m)), moveY(P(m), P(e)))

      // Y -> X
      } else {
        (moveX(P(m), P(e)), moveY(P(s), P(m)))
      }
    }

    def len(moves: Moves) = {
      var len = (moves.xs ++ moves.ys).map(_.len).sum
      moves.xs foreach { x =>
        len -= moves.ys.count(y => y.l <= x.y && x.y <= y.r)
      }
      len
    }

    def merge2[A <: Move[A]](m1: A, m2: A): Seq[A] = {
      if (m1.axis == m2.axis) {
        val l = max(m1.l, m2.l)
        val r = min(m1.r, m2.r)
        // 重複部分あり
        if (l <= r) {
          Seq(m1.extend(min(m1.l, m2.l), max(m1.r, m2.r)))
        } else {
          Seq(m1, m2)
        }
      } else {
        Seq(m1, m2)
      }
    }

    def merge4(arg: (MoveX, MoveX, MoveY, MoveY)): Moves = {
      val (x1, x2, y1, y2) = arg

      val xs = merge2(x1, x2)
      val ys = merge2(y1, y2)

      Moves(xs, ys)
    }

    val ans = (for {
      c1 <- C
      c2 <- C
      c3 <- C
      if Set(c1, c2, c3).size == 3
      (x1, y1) = move(Path(c1, c2))
      (x2, y2) = move(Path(c2, c3))
    } yield merge4((x1, x2, y1, y2))).minBy(len)

    out.println(len(ans))

    val flg = Array.ofDim[Boolean](1001, 1001)

    (ans.xs ++ ans.ys) foreach { move =>
      REP(move.len) { i =>
        move match {
          case MoveX(y, l, r) =>
            if (!flg(l + i)(y)) {
              out.println(s"${l + i} $y")
              flg(l + i)(y) = true
            }

          case MoveY(x, l, r) =>
            if (!flg(x)(l + i)) {
              out.println(s"$x ${l + i}")
              flg(x)(l + i) = true
            }
        }
      }
    }
  }

  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 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 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 scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

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

  sealed trait Move[A] {
    val l: Int
    val r: Int
    def axis: Int
    def len = r - l + 1
    def extend(l: Int, r: Int): A
  }
  case class MoveX(y: Int, l: Int, r: Int) extends Move[MoveX] {
    override def axis = y
    def extend(l: Int, r: Int) = MoveX(y, l, r)
  }
  case class MoveY(x: Int, l: Int, r: Int) extends Move[MoveY] {
    override def axis = x
    override def extend(l: Int, r: Int) = MoveY(x, l, r)
  }
  case class Coord(x: Int, y: Int)
  case class Path(c1: Coord, c2: Coord)

  case class Moves(xs: Seq[MoveX], ys: Seq[MoveY])

  def solve(): Unit = {
    val c1, c2, c3 = Coord(ni(), ni())
    val P = Array(Path(c1, c2), Path(c2, c3), Path(c3, c1))

    def moveX(p1: Coord, p2: Coord) = {
      assert(p1.y == p2.y)
      MoveX(p1.y, min(p1.x, p2.x), max(p1.x, p2.x))
    }

    def moveY(p1: Coord, p2: Coord) = {
      assert(p1.x == p2.x)
      MoveY(p1.x, min(p1.y, p2.y), max(p1.y, p2.y))
    }

    def move(p: Path): (MoveX, MoveY) = {
      val Path(c1, c2) = p
      val xmin = min(c1.x, c2.x)
      val xmax = max(c1.x, c2.x)
      val ymin = min(c1.y, c2.y)
      val ymax = max(c1.y, c2.y)
      val lt = Coord(xmin, ymax)
      val rt = Coord(xmax, ymax)
      val lb = Coord(xmin, ymin)
      val rb = Coord(xmax, ymin)

      // 時計回り
      val P = Array(lt, rt, rb, lb)
      val s = P.indexOf(c1)
      val m = (s + 1) % 4
      val e = (s + 2) % 4

      // X -> Y
      if (s % 2 == 0) {
        (moveX(P(s), P(m)), moveY(P(m), P(e)))

      // Y -> X
      } else {
        (moveX(P(m), P(e)), moveY(P(s), P(m)))
      }
    }

    def len(moves: Moves) = {
      var len = (moves.xs ++ moves.ys).map(_.len).sum
      moves.xs foreach { x =>
        len -= moves.ys.count(y => y.l <= x.y && x.y <= y.r)
      }
      len
    }

    def merge2[A <: Move[A]](m1: A, m2: A): Seq[A] = {
      if (m1.axis == m2.axis) {
        val l = max(m1.l, m2.l)
        val r = min(m1.r, m2.r)
        // 重複部分あり
        if (l <= r) {
          Seq(m1.extend(min(m1.l, m2.l), max(m1.r, m2.r)))
        } else {
          Seq(m1, m2)
        }
      } else {
        Seq(m1, m2)
      }
    }

    def merge4(arg: (MoveX, MoveX, MoveY, MoveY)): Moves = {
      val (x1, x2, y1, y2) = arg

      val xs = merge2(x1, x2)
      val ys = merge2(y1, y2)

      Moves(xs, ys)
    }

    val ans = (for {
      p1 <- P
      p2 <- P
      if p1 != p2
      (x1, y1) = move(p1)
      (x2, y2) = move(p2)
    } yield merge4((x1, x2, y1, y2))).minBy(len)

    out.println(len(ans))

    val flg = Array.ofDim[Boolean](1001, 1001)

    (ans.xs ++ ans.ys) foreach { move =>
      REP(move.len) { i =>
        move match {
          case MoveX(y, l, r) =>
            if (!flg(l + i)(y)) {
              out.println(s"${l + i} $y")
              flg(l + i)(y) = true
            }

          case MoveY(x, l, r) =>
            if (!flg(x)(l + i)) {
              out.println(s"$x ${l + i}")
              flg(x)(l + i) = true
            }
        }
      }
    }
  }

  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 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 CF_528_2_C {

  type In = Seq[(Int, Int)]
  type Out = String

  def solve(in: In): Out = {
    val Seq(a, b, c) = in.sorted
    def right(a: (Int, Int), b: Int) = (a._1 to b).map(x => (x, a._2))
    def up(a: (Int, Int), b: Int) = (a._2 to b).map(y => (a._1, y))
    // Go across, then up/down, then up/down, then across
    val squares = (right(a, b._1) ++ up((b._1, a._1), b._2) ++ up(b, c._2) ++ right((b._1, c._2), c._1)).toSet
    squares.size.toString + "\n" + squares.map { case (a, b) => s"$a $b" }.mkString("\n")
  }

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Specify Input and Output formats on RHS here:

  def formatIn(i: Input)    = i.getLines.map{_.split(" ")}.map{case Array(a,b) => (a.toInt, b.toInt)}
  def formatOut(out: Out): String  = out.toString

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Boilerplate & utility methods that don't change:

  import java.util.Scanner
  import java.io.InputStream
  import language.implicitConversions

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  class Input(sc: Scanner) {
    // This class is useful for convenience, and for mock stdin in test scripts
    // Remember to call nextLine to advance past the newline character (if required) before taking a whole line
    def this(i: InputStream) =       this(new Scanner(i))
    def this(s: String) =            this(new Scanner(s.stripMargin))
    def int =                        sc.nextInt()
    def long =                       sc.nextLong()
    def double =                     sc.nextDouble()
    def nextLine =                   sc.nextLine()
    def collect[T](f: String => T) = sc.nextLine().split(" ").toVector.map(f)
    def intSeq =                     collect(_.toInt)
    def doubleSeq =                  collect(_.toDouble)
    def getLines: Vector[String] =   if (!sc.hasNextLine) Vector.empty
                                     else sc.nextLine +: getLines
    def solveVal: Out =              (solve _).tupled(formatIn(this))
    def solveStr: String =           formatOut(solveVal)
  }

  // Ceiling division for Int & Long
  def ceil(a: Int, b: Int)   = (a - 1)/b + 1
  def ceil(a: Long, b: Long) = (a - 1)/b + 1

  // A frequently used number
  val modulo = 1000000007

  // For convenience in test scripts, treat a String as an Input
  implicit def stringToInput(s: String): Input = new Input(s)
  // Required to make `solveVal` work when input has just 1 value (i.e. not a Tuple)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) {
    def tupled: T => R = x => f(x)
  }
}

object CF_528_2_C {

  type In = Seq[(Int, Int)]
  type Out = String

  def solve(in: In): Out = {
    val Seq(a, b, c) = in.sorted
    def right(a: (Int, Int), b: Int) = (a._1 to b).map(x => (x, a._2))
    def up(a: (Int, Int), b: Int) = (a._2 to b).map(y => (a._1, y))
    // Go across, then up/down, then up/down, then across
    val squares = (right(a, b._1) ++ up((b._1, a._2), b._2) ++ up(b, c._2) ++ right((b._1, c._2), c._1)).toSet
    squares.size.toString + "\n" + squares.map { case (a, b) => s"$a $b" }.mkString("\n")
  }

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Specify Input and Output formats on RHS here:

  def formatIn(i: Input)    = i.getLines.map{_.split(" ")}.map{case Array(a,b) => (a.toInt, b.toInt)}
  def formatOut(out: Out): String  = out.toString

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Boilerplate & utility methods that don't change:

  import java.util.Scanner
  import java.io.InputStream
  import language.implicitConversions

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  class Input(sc: Scanner) {
    // This class is useful for convenience, and for mock stdin in test scripts
    // Remember to call nextLine to advance past the newline character (if required) before taking a whole line
    def this(i: InputStream) =       this(new Scanner(i))
    def this(s: String) =            this(new Scanner(s.stripMargin))
    def int =                        sc.nextInt()
    def long =                       sc.nextLong()
    def double =                     sc.nextDouble()
    def nextLine =                   sc.nextLine()
    def collect[T](f: String => T) = sc.nextLine().split(" ").toVector.map(f)
    def intSeq =                     collect(_.toInt)
    def doubleSeq =                  collect(_.toDouble)
    def getLines: Vector[String] =   if (!sc.hasNextLine) Vector.empty
                                     else sc.nextLine +: getLines
    def solveVal: Out =              (solve _).tupled(formatIn(this))
    def solveStr: String =           formatOut(solveVal)
  }

  // Ceiling division for Int & Long
  def ceil(a: Int, b: Int)   = (a - 1)/b + 1
  def ceil(a: Long, b: Long) = (a - 1)/b + 1

  // A frequently used number
  val modulo = 1000000007

  // For convenience in test scripts, treat a String as an Input
  implicit def stringToInput(s: String): Input = new Input(s)
  // Required to make `solveVal` work when input has just 1 value (i.e. not a Tuple)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) {
    def tupled: T => R = x => f(x)
  }
}


object Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  //
  def main(args: Array[String]): Unit = {
    solve(System.in,System.out)
  }
  def minDays(a: Array[Int],pos: Int, target: Int): Option[Int] = {
    if(a(pos) >= target){
      Some(0)
    }else{
      if(pos >= a.length-1)
        None
      else
        minDays(a,pos+1,target-a(pos)).map{ d =>
          d+target-a(pos)
        }
    }
  }

  def solve(inputStream: InputStream, out: PrintStream): Unit = {
    val in = new InputReader(inputStream)
    val t = in.nextInt()
    (1 to t).foreach(_ => {
      val n = in.nextInt()
      val d = in.nextInt()
      val a = (1 to n).map(_ => in.nextInt()).toArray
      val res = (0 to 200).zipWithIndex.flatMap{case (h,i) => minDays(a,0,h).flatMap(m => if(m <= d) Some(i) else None)}.max
      out.println(res)
    })
  }
  class InputReader(stream: InputStream){
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(stream),32768)
    var tokenizer: StringTokenizer = _
    def next(): String = {
      while(tokenizer == null || !tokenizer.hasMoreTokens){
        try{
          tokenizer = new StringTokenizer(reader.readLine())
        }catch {
          case e: IOException => throw new RuntimeException(e)
        }
      }
      tokenizer.nextToken()
    }
    def nextInt(): Int =  next().toInt
    def nextLong(): Long =  next().toLong
  }
}

import java.io.PrintWriter

import scala.collection.mutable
import util.control.Breaks._

object Solution {
    val reader = scala.io.StdIn
    def readInt(): Int = reader.readLine().toInt
    def readArrayInt(): Array[Int] = reader.readLine().split(" ").map(_.toInt)
    def readString(): String = reader.readLine()

    def multiTest(f: () => Unit): Unit = {
        var t = readInt()
        while (t > 0) {
            f()
            t -= 1
        }
    }

    def solve(): Unit = {
        var Array(n, d) = readArrayInt()
        val a = readArrayInt()
        var rs = a(0)
        for (i <- 1 until n if a(i) > 0) {
            rs += math.min(d / i, a(i))
            d -= math.min(d/i, a(i))*i
        }
        println(rs)
    }

    def main(args: Array[String]) {
        multiTest(solve)
        //solve()
    }
}

import java.io.PrintWriter

import scala.collection.mutable
import util.control.Breaks._

object Solution {
    val reader = scala.io.StdIn
    def readInt(): Int = reader.readLine().toInt
    def readArrayInt(): Array[Int] = reader.readLine().split(" ").map(_.toInt)
    def readString(): String = reader.readLine()

    def multiTest(f: () => Unit): Unit = {
        var t = readInt()
        while (t > 0) {
            f()
            t -= 1
        }
    }

    def solve(): Unit = {
        var Array(n, d) = readArrayInt()
        val a = readArrayInt()
        var rs = a(0)
        for (i <- 1 until n) {
            while (d > 0 && a(i) >= i) {
                rs += 1
                d -= i
                a(i) -= 1
            }
        }
        println(rs)
    }

    def main(args: Array[String]) {
        multiTest(solve)
        //solve()
    }
}


object B {
    def main(args: Array[String]) {
        val n = readLine
        println(n + n.reverse)
    }
}

object B {
  def main(args: Array[String]) = {
    val n = readLine()
    println(n + n.reverse)
  }

  def readLine() = io.StdIn.readLine()
  def readInts() = readLine().split(' ').map(_.toInt)

  def digits(s: String) = s.map(_.toString.toInt)
}

object Main {
  def main(args: Array[String]) {
    val  x = readLine()
    println(x + x.reverse)
  }
}

import scala.io._
import java.util.Scanner

object Main {
    def main(args: Array[String]) {
        val scanner = new Scanner(System.in)
        val a = scanner.next()

        println(a + a.reverse)
    }
}

object Solution extends App {
  val lines = io.Source.stdin.getLines()
  val line = lines.next()
  print(line)
  print(line.reverse)
}

object B688 {
  @inline def read = scala.io.StdIn.readLine

  @inline def tokenizeLine = new java.util.StringTokenizer(read)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val n = read
    println(n + n.reverse)
  }
}

object cf_688B {
  def main(args: Array[String]) {
    val input = readLine()
    print(input)
    for (s <- input.reverse) print(s)
  }
}

import Utils._, annotation.tailrec, collection.mutable, util._, control._, math.Ordering.Implicits._

object _688B extends CodeForcesApp {
  override def apply(io: IO) = {import io.{read, write}
    val s = read[String]
    write(s + s.reverse)
  }
}
/****************************[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 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 = t.head.ensuring(t.size == 1)
    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 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
  }
  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
  }
  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 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 toYesNo = if(x) "yes" else "no"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    def bitCount: Int = java.lang.Integer.bitCount(x)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    def bitCount: Int = java.lang.Long.bitCount(x)
  }
  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(n.min(x, y), n.max(x, y), 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]: mutable.MultiMap[K, V] = new mutable.HashMap[K, mutable.Set[V]] with mutable.MultiMap[K, 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
  }
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) 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")

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

  def write(obj: Any, more: Any*): this.type = {
    printer.print(obj)
    more foreach {i =>
      printer.print(' ')
      printer.print(i)
    }
    this
  }
  def writeLine(obj: Any*): this.type = {
    if (obj.isEmpty) printer.println() else obj foreach printer.println
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)

  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.util.Scanner

object MainApp {
  def isEvenPalindrome(n: List[Char]): Boolean =
    n match {
      case Nil => true
      case x if n.length % 2 != 0 => false
      case x :: xs => if (n.head == n.last) isEvenPalindrome(xs.init) else false
    }

  def generatePalindromes(last: Int): Seq[(Int, String)] = {
    (1 to last) zip {
      for (
        x <- 1 to last if isEvenPalindrome(x.toString.toList)
      ) yield x.toString
    }
  }

  def main(args: Array[String]) = {
    val n = new Scanner(System.in).nextLine()
    println(n ++ n.reverse)
//    println(generatePalindromes(1000000000))
  }
}

object problem_B{
  def main(args: Array[String]): Unit = {
    val scanner = new java.util.Scanner(System.in)
    val line = scanner.nextLine()

    println(line + line.reverse)

  }
}


import scala.io.StdIn._

object B_1181 extends App {
  val l = readInt()
  val A = readLine()

  def split(i: Int): BigInt = {
    if (i < 1 || i >= l) return BigInt("0")

    var (a, b) = A.splitAt(i)

    val aa = BigInt(a)
    val bb = BigInt(b)

//    println(a + " + " + b + " = " + (aa + bb))

    aa + bb
  }

  def getLeft(i: Int) = {
    var left = i
    while (left >= 0 && A(left) == '0') left -= 1
    left
  }

  def getRight(i: Int) = {
    var right = i
    while (right < l && A(right) == '0') right += 1
    right
  }

  val l1 = getLeft(l / 2)
  val l2 = getLeft(l / 2 - 1)
  val r1 = getRight(l / 2)
  val r2 = getRight(l / 2 + 1)

  val l3 = r1
  val l4 = r2
  val r3 = l1
  val r4 = l2

  val result = Set(l1, l2, l3, l4, r1, r2, r3, r4)
    .map { i =>
      split(i)
    }
    .filterNot(_ == 0)
    .min

  println(result)
}

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    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 = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = 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]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

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

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  def solve(): Unit = {
    val N = ni()
    val S = ns(N)

    var ms = Integer.MAX_VALUE
    val cand = ArrayBuffer[Int]()

    TO(1, N - 1) { i =>
      if (S(i) != '0') {
        val d = max(i, N - i) + 1
        if (ms > d) {
          ms = d
          cand.clear()
          cand += i
        } else if (ms == d) {
          cand += i
        }
      }
    }

    debug(s"ms:$ms")
    debug(cand.mkString(" "))

    val ans = (for {
      i <- cand
    } yield {
      val l = BigInt(S.take(i).mkString)
      val r = BigInt(S.drop(i).mkString)
      l + r
    }).min

    out.println(ans)
  }
}