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 = na(N)
    val dp = Array.ofDim[Long](N + 1)
    dp(0) = 1

    rep(N) { i =>
      val ds = divisors(A(i), i + 1)
      rep_r(ds.length){ i =>
        val d = ds(i)
        dp(d) += dp(d - 1)
        if (dp(d) >= MOD) dp(d) -= MOD
      }
    }

    var ans = 0L
    rep(N, 1) { i =>
      ans += dp(i)
      if (ans >= MOD) ans -= MOD
    }
    out.println(ans)
  }

  def divisors(x: Int, mx: Int): Array[Int] = {
    val pre = ArrayBuffer[Int]()
    val post = ArrayBuffer[Int]()
    rep(math.sqrt(x).toInt, 1) { d =>
      if (x % d == 0) {
        if (d <= mx) pre += d
        if (d * d != x && x / d <= mx) post += x / d
      }
    }

    val res = Array.ofDim[Int](pre.length + post.length)
    rep(pre.length)(i => res(i) = pre(i))
    val preLen = pre.length
    rep_r(post.length)(i => res(i + preLen) = post(i))
    res
  }

  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[Int](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 = na(N)

    val MAX = 1e6.toInt
    val divisors = Array.fill[ArrayBuffer[Int]](1e6.toInt + 1)(ArrayBuffer())
    val hasA = Array.ofDim[Boolean](MAX + 1)
    rep(N) { i =>
      hasA(A(i)) = true
    }

    rep_r(MAX, 1) { d =>
      var j = d
      while(j <= MAX) {
        if (hasA(j)) {
          divisors(j) += d
        }
        j += d
      }
    }

    val table = Array.ofDim[Long](N + 1)
    table(0) = 1
    rep(N) { i =>
      val ds = divisors(A(i))
      rep(ds.length) { j =>
        val d = ds(j)
        if (d <= i + 1) {
          table(d) += table(d - 1)
          if (table(d) >= MOD) table(d) -= MOD
        }
      }
    }
    var ans = 0L
    rep(N) { i =>
      ans += table(i + 1)
      if (ans >= MOD) ans -= MOD
    }

    out.println(ans)
  }

  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[Int](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 = na(N)
    val table = Array.ofDim[Long](N + 1)
    table(0) = 1
    rep(N) { i =>
      val ds = dividers(A(i))
      Sorting.quickSort(ds)
      rep_r(ds.length) { j =>
        val d = ds(j)
        if (d <= N) {
          table(d) += table(d - 1)
          if (table(d) >= MOD) table(d) -= MOD
        }
      }
    }
    var ans = 0L
    rep(N) { i =>
      ans += table(i + 1)
    }

    out.println(ans)
  }

  def dividers(x: Int): Array[Int] = {
    if (x == 1) Array(1)
    else {
      val fs_map = factorize(x)
      val fs = fs_map.toArray
      val len = fs.length
      val digits = Array.ofDim[Int](len)
      val res = Array.ofDim[Int](fs_map.map(_._2 + 1).product)
      var p = 0

      def next(): Boolean = {
        var k = 0
        digits(0) += 1
        while (k < len - 1) {
          if (digits(k) > fs(k)._2) {
            digits(k) = 0
            digits(k + 1) += 1
            k += 1
          } else {
            return true
          }
        }
        digits(len - 1) <= fs(len - 1)._2
      }

      def mkVal: Int = {
        var res = 1
        rep(len) { i =>
          rep(digits(i)) { _ =>
            res *= fs(i)._1
          }
        }
        res
      }

      res(0) = 1
      p += 1
      while (next()) {
        res(p) = mkVal
        p += 1
      }

      res
    }
  }

  def factorize(n: Int): mutable.Map[Int, Int] = {
    import scala.collection.mutable
    val res = mutable.Map[Int, Int]() withDefaultValue 0

    def minFactor(n0: Int, rt: Int, i: Int): Int = {
      if (i > rt) n0 // √n まで見つからなかったら n0は素数か1
      else if (n0 % i == 0) i
      else minFactor(n0, rt, i + 1)
    }

    def step(n0: Int): Unit = {
      minFactor(n0, math.sqrt(n0).toInt, 2) match {
        case 1 =>
        case f =>
          res(f) = res(f) + 1
          step(n0 / f)
      }
    }

    step(n)
    res
  }

  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[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


import scala.io.StdIn
import scala.collection.mutable.Buffer

object Main {
  def main(args: Array[String]): Unit = {
    val n = StdIn.readLine()
    val flats: Array[Int] = StdIn.readLine().split(" ").map(_.toInt)
    val pr = getPriorities(flats)
    val after2Deleting = handlePriorities(pr.toBuffer, 2)
    val after1Deleting = handlePriorities(after2Deleting._2, 1)
    println(after2Deleting._1 + after1Deleting._1)
   }

  def getPriorities(flats: Array[Int]): IndexedSeq[Int]  = {
    for(i <- flats.indices) yield getPriority(flats, i)
  }

  def getPriority(flats: Array[Int], i: Int): Int = {
    flats(i) match {
      case 0 => 0
      case 1 =>
        val tail = flats.length - 1
        val preTail = flats.length - 2
        i match {
          case 0 => {
            if(flats(1) == 0 && flats(2) == 1) 1
            else 0
          }
          case 1 => {
            if(flats.length != 3 && flats(2) == 0 && flats(3) == 1) 1
            else 0
          }
          case `tail` => {
            if(flats(flats.length - 2) == 0 && flats(flats.length - 3) == 1) 1
            else 0
          }
          case `preTail` => {
            if(flats(flats.length - 3) == 0 && flats(flats.length - 4) == 1) 1
            else 0
          }
          case other => {
            val right = (flats(other + 1), flats(other + 2)) match {
              case (0, 1) => 1
              case _ => 0
            }
            val left = (flats(other - 1), flats(other - 2)) match {
              case (0, 1) => 1
              case _ => 0
            }
            right + left
          }
        }
    }
  }

  def handlePriorities(priorities: Buffer[Int], num: Int) = {
    var counter: Int = 0
    for(i <- priorities.indices) {
     priorities(i) match {
       case `num` => {
         priorities(i) = 0
         if(i - 2 >= 0) priorities(i - 2) -= 1
         if(i + 2 < priorities.length)priorities(i + 2) -= 1
         counter += 1
       }
       case _ =>
     }
    }
    (counter, priorities)
  }
}

object A extends App{
    import java.io.{PrintWriter}
    import java.util.{Locale, Scanner}

    val in = new Scanner(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt

    def solve = {
        val n = nextInt
        var ans = 0
        var stats = (for(i <- 1 to n) yield nextInt).toList
        stats = (0::stats):+0
        var input = stats.toBuffer

        for (i <- 1 to n) {
            if(input(i) == 0) {
                if(input(i - 1) == 1 && input(i + 1) == 1) {
                    input(i + 1) = 0
                    ans += 1
                }
            }
        }
        println(ans)
    }

    solve
}

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 = na(N)
    val flg = Array.ofDim[Boolean](N)
    var ans = 0
    rep(N - 2, 1) { i =>
      if (A(i - 1) == 1 && A(i + 1) == 1 && A(i) == 0) {
        flg(i) = true
      }
    }

    rep(N - 2, 1) { i =>
      if (flg(i)) {
        flg(i) = false
        if (i < N - 2) flg(i + 2) = false
        ans += 1
      }
    }

    out.println(ans)
  }

  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[Int](as.length + 1)
    rep(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import scala.io.StdIn

object B extends App {
	val n = StdIn.readInt
	val arr = StdIn.readLine.split(" ").map(_.toInt)

	def f(p: Int, cnt: Int): Int = {
		if(p >= n)
			cnt

		else {
			if(p + 2 < n && arr(p) == 1 && arr(p + 1) == 0 && arr(p + 2) == 1)
				f(p + 3, cnt + 1)

			else f(p + 1, cnt)
		}
	}

	println(f(0, 0))
}

object CF_521_3_B {
  def solve(n: Int, as: Seq[Int]): Int = {
    def turnOff(as: Seq[Int], count: Int, out: Seq[Int]): (Seq[Int], Int) = as match {
      case 1 +: 0 +: 1 +: _  => turnOff(as.updated(2,0).tail, count + 1, out :+ 1)
      case a +: _ +: _ +: _  => turnOff(as.tail, count, out :+ a)
      case Seq(a,b) => (out :+ a :+ b, count)
    }
    val result = turnOff(as, 0, Seq.empty)
    result._2
  }

  // `solution` is defined by the input format - and can be called both from main and test script without repeating
  def solution(i: Input) = solve(i.int, {i.nextLine; i.intSeq()})

//     ~~~ Boilerplate that doesn't change ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def main(args: Array[String]) = {
    val out = new java.io.PrintWriter(System.out)
    val sol = solution(new Input(System.in))
    out.println(sol)
    out.close()
  }

//     ~~~ Utility methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  import java.util.Scanner
  class Input(sc: Scanner) {
    def this(i: java.io.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()
    // If there are prior values, remember to call nextLine to advance past the newline character before collecting the line:
    def collect[T](f: String => T) = {sc.nextLine().split(" ").toVector.map(f)}
    def intSeq() = collect(_.toInt)
    def doubleSeq() = collect(_.toDouble)
  }

  def ceil(a: Int, b: Int) = (a - 1) / b + 1
  def ceil(a: Long, b: Long) = (a - 1) / b + 1

  import language.implicitConversions
  implicit def stringToInput(s: String): Input = new Input(s)
}


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

import collection.mutable.HashMap
import scala.collection.{SortedSet, mutable}
import scala.util.Random
  protected val EPS = 1e-11

  private def default = (
    new StreamTokenizer(new InputStreamReader(System.in)),
    new Scanner(System.in),
    new PrintWriter(System.out)
  )

  val (in, ins, out) = {
    try {
      if (sys.props.contains("xDx")) {
        (
          new StreamTokenizer(new FileReader("input.txt")),
          new Scanner(new FileReader("input.txt")),
          new PrintWriter(new FileWriter("output.txt"))
        )
      } else {
        default
      }
    } catch {
      case _: Throwable => default
    }
  }

  private def nextInt = {
    in.nextToken
    in.nval.toInt
  }

  private def nextLong = {
    in.nextToken
    in.nval.toLong
  }

  private def nextDouble = {
    in.nextToken
    in.nval
  }

  private def nextString = {
    in.nextToken
    in.sval
  }

  private def nextChar = {
    in.nextToken
    in.ttype.toChar
  }

  def main(args: Array[String]): Unit = {
    try {
      new Run().run()
    } catch {
      case e: Throwable =>
        throw new RuntimeException(e)
    } finally out.close()
  }

  class Run {
    import collection.breakOut
    def run(): Unit = {
        val n = nextInt
        val a = readArr(n)
        val m = nextInt
        val b = readArr(m)
        if(a(n - 1) != b(m - 1)) {
            println(-1)
        } else {
            var res = 0
            var ai = 0
            var bi = 0
            while(ai < n && bi < m) {
                if(a(ai) == b(bi)) {
                    res += 1
                    ai += 1
                    bi += 1
                } else if(a(ai) < b(bi)) {
                    ai += 1
                } else {
                    bi += 1
                }
            }
            println(res)
        }
    }

    def readArr(n: Int): Array[Long] = {
        val a = Array.ofDim[Long](n)
        var prev = 0L
        for(i <- 0 until n) {
            a(i) = prev + nextInt
            prev = a(i)
        }
        a
    }

  }

}

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 = map(N)(_ => nl())
    val M = ni()
    val B = map(M)(_ => nl())

    if (A.sum != B.sum) out.println(-1)
    else {
      val cumA = Array.ofDim[Long](N)
      cumA(0) = A(0)
      rep(N - 1) { i =>
        cumA(i + 1) = cumA(i) + A(i + 1)
      }
      val cumB = Array.ofDim[Long](M)
      cumB(0) = B(0)
      rep(M - 1) { i =>
        cumB(i + 1) = cumB(i) + B(i + 1)
      }

      var ans = 0
      var a, b = 0
      while (a < N && b < M) {
        if (cumA(a) == cumB(b)) {
          ans += 1
          a += 1
          b += 1
        } else if (cumA(a) > cumB(b)) {
          b += 1
        } else {
          a += 1
        }
      }

      out.println(if (ans == 0) -1 else ans)
    }
  }

  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): Array[Int] = map(n)(_ => ni())
  def nm(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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}

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 = na(N)
    val M = ni()
    val B = na(M)

    val cumA = Array.ofDim[Long](N)
    cumA(0) = A(0)
    rep(N - 1) { i =>
      cumA(i + 1) = cumA(i) + A(i + 1)
    }
    val cumB = Array.ofDim[Long](M)
    cumB(0) = B(0)
    rep(M - 1) { i =>
      cumB(i + 1) = cumB(i) + B(i + 1)
    }

    var ans = 0
    var a, b = 0
    while(a < N && b < M) {
      if (cumA(a) == cumB(b)) {
        ans += 1
        a += 1
        b += 1
      } else if (cumA(a) > cumB(b)) {
        b += 1
      } else {
        a += 1
      }
    }

    out.println(if (ans == 0) - 1 else ans)
  }

  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): Array[Int] = map(n)(_ => ni())
  def nm(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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}

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 = map(N)(_ => ni())
    val M = ni()
    val B = map(M)(_ => ni())

    if (A.sum != B.sum) out.println(-1)
    else {
      val cumA = Array.ofDim[Long](N)
      cumA(0) = A(0)
      rep(N - 1) { i =>
        cumA(i + 1) = cumA(i) + A(i + 1)
      }
      val cumB = Array.ofDim[Long](M)
      cumB(0) = B(0)
      rep(M - 1) { i =>
        cumB(i + 1) = cumB(i) + B(i + 1)
      }

      var ans = 0
      var a, b = 0
      while (a < N && b < M) {
        if (cumA(a) == cumB(b)) {
          ans += 1
          a += 1
          b += 1
        } else if (cumA(a) > cumB(b)) {
          b += 1
        } else {
          a += 1
        }
      }

      out.println(if (ans == 0) -1 else ans)
    }
  }

  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): Array[Int] = map(n)(_ => ni())
  def nm(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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}

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 = na(N)
    val M = ni()
    val B = na(M)

    if (A.sum != B.sum) out.println(-1)
    else {
      val cumA = Array.ofDim[Long](N)
      cumA(0) = A(0)
      rep(N - 1) { i =>
        cumA(i + 1) = cumA(i) + A(i + 1)
      }
      val cumB = Array.ofDim[Long](M)
      cumB(0) = B(0)
      rep(M - 1) { i =>
        cumB(i + 1) = cumB(i) + B(i + 1)
      }

      var ans = 0
      var a, b = 0
      while (a < N && b < M) {
        if (cumA(a) == cumB(b)) {
          ans += 1
          a += 1
          b += 1
        } else if (cumA(a) > cumB(b)) {
          b += 1
        } else {
          a += 1
        }
      }

      out.println(if (ans == 0) -1 else ans)
    }
  }

  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): Array[Int] = map(n)(_ => ni())
  def nm(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)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }
}


object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(a, b) = in.next().split(" ").map(_.toInt)
    (a, b)
  }
  var count = 0
  println((0 until n).count { i =>
    val (a, b) = data(i)
    var up = false
    var down = false
    var left = false
    var right = false
    (0 until n).foreach { j =>
      val (a1, b1) = data(j)
      if (a1 == a && b1 > b)
        up = true
      else if (a1 == a && b1 < b)
        down = true
      else if (b1 == b && a1 < a)
        right = true
      else if (b1 == b && a1 > a)
        left = true
    }
    up && left && down && right
  })

}

object A165 {

  import IO._
  import collection.{mutable => cu}

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val in = Array.fill(n)(readInts(2)).map(arr => (arr(0), arr(1)))
    var res = 0
    for(i <- 0 until n) {
      var (right, left, upper, lower) = (false, false, false, false)
      for(j <- 0 until n if i != j) {
        if(in(i)._2 == in(j)._2) {
          if(in(i)._1 > in(j)._1)
            right = true
          else if(in(i)._1 < in(j)._1)
            left = true
        } else if(in(i)._1 == in(j)._1) {
          if(in(i)._2 > in(j)._2)
            upper = true
          else if(in(i)._2 < in(j)._2)
            lower = true
        }
      }
      if(right && left && upper && lower)
        res += 1
    }
    println(res)
  }

  object IO {
    @inline def read: String = scala.io.StdIn.readLine

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

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

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

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

}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P165A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val a = Array.fill(N, 2)(sc.nextInt)

  def isSupercentral(p: Array[Int]): Boolean = {
    @tailrec
    def loop(right: Int, left: Int, lower: Int, upper: Int, i: Int): Boolean =
      if (i == N) right > 0 && left > 0 && lower > 0 && upper > 0
      else {
        val q = a(i)
        if (p(0) == q(0) && p(1) < q(1)) loop(right, left, lower, upper + 1, i + 1)
        else if (p(0) == q(0) && p(1) > q(1)) loop(right, left, lower + 1, upper, i + 1)
        else if (p(1) == q(1) && p(0) < q(0)) loop(right, left + 1, lower, upper, i + 1)
        else if (p(1) == q(1) && p(0) > q(0)) loop(right + 1, left, lower, upper, i + 1)
        else loop(right, left, lower, upper, i + 1)
      }
    loop(0, 0, 0, 0, 0)
  }

  val answer = (0 until N).filter((x: Int) => isSupercentral(a(x))).size

  out.println(answer)
  out.close
}

import annotation.tailrec
import java.io._

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInts = reader.readLine().split(" ").map(_.toInt)
  def readInt = reader.readLine().toInt

  val n = readInt
  val points = for(_ <- 1 to n) yield readInts
  def ans = points.count {
    case Array(x, y) => (for(i <- 0 until 4) yield {
      val dx = (i & 1) * i - 2 * (i & 1)
      val dy = (1 - i) * (1 - (i & 1))
      points.count {
        case Array(x1, y1) => (x - x1).signum == dx && (y - y1).signum == dy
      }
    }).reduce(_ * _) != 0
  }

  def main(args: Array[String]) {
    println(ans)
  }
}

object Main {
  case class Point(x: Int, y: Int)
  class System {
    val xmap = scala.collection.mutable.Map[Int, List[Point]]()
    val ymap = scala.collection.mutable.Map[Int, List[Point]]()

    def read() = {
      val a = readLine().split(" ").map(_.toInt)
      Point(a(0), a(1))
    }

    def add() {
      val p = read
      xmap(p.x) = xmap.getOrElse(p.x, List[Point]()) ::: List(p)
      ymap(p.y) = ymap.getOrElse(p.y, List[Point]()) ::: List(p)
    }

    def count = {
      val x = xmap.values.map(_.sortBy(_.y))
      val xs = x.flatMap{ x0 =>
        if (x0.size >= 3) {
          x0.slice(1, x0.size - 1)
        } else Nil
      }
      val y = ymap.values.map(_.sortBy(_.x))
      val ys = y.flatMap{ y0 =>
        if (y0.size >= 3) {
          y0.slice(1, y0.size - 1)
        } else Nil
      }

      (xs.toSet & ys.toSet).size
    }
  }

  def main(args: Array[String]) {
    val n = readInt
    val s = new System()
    for (_ <- 1 to n) { s.add() }
    println(s.count)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(a, b) = in.next().split(" ").map(_.toInt)
    (a, b)
  }
  var count = 0
  println((0 until n).count { i =>
    val (a, b) = data(i)
    var up = false
    var down = false
    var left = false
    var right = false
    (1 until n).foreach { j =>
      val (a1, b1) = data(j)
      if (a1 == a && b1 > b)
        up = true
      else if (a1 == a && b1 < b)
        down = true
      else if (b1 == b && a1 < a)
        right = true
      else if (b1 == b && a1 > a)
        left = true
    }
    up && left && down && right
  })

}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(a, b) = in.next().split(" ").map(_.toInt)
    (a, b)
  }.groupBy(_._1)
  println(data.foldLeft(0) {
    case(acc, (a, b)) if b.size < 3 => acc
    case(acc, (a, b)) => acc + b.size - 2
  })

}


import scala.collection.mutable.ListBuffer

object Main {

    def main(args: Array[String]) {
        val n = nextInt
        val lst = for (i <- 1 to n) yield nextInt
        val s = lst.sum
        val lb = new ListBuffer[Int]()
        for ((ai, i) <- lst zip (1 to n) ; if ai * n == s)
            lb += i
        println(lb.length)
        println(lb.mkString(" "))
    }

    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    var st = new java.util.StringTokenizer("");

    def next() = {
        while (!st.hasMoreTokens())
            st = new java.util.StringTokenizer(in.readLine());
        st.nextToken();
    }

    def nextInt() = java.lang.Integer.parseInt(next());
    def nextDouble() = java.lang.Double.parseDouble(next());
    def nextLong() = java.lang.Long.parseLong(next());
}

object App extends App {
  Round3.print(Round3.solve(readLine(), readLine()))
}

object Round3 {
  def print(r: Result): Unit = {
    println(r.n)
    println(r.indices.mkString(" "))
  }

  case class Result(n: Int, indices: Array[Int])

  def solve(i1: String, i2: String): Result = {
    val nums = i2.split(" ").map { _.toDouble }
    val length = i1.toDouble
    val ave = nums.sum / length
    val indices = nums.zipWithIndex collect { case (n, i) if n == ave => i+1 }
    Result(indices.length, indices)
  }

}


object CaptainMarmot extends App {

  import scala.io.StdIn

  def readAxis: (Int, Int, Int, Int) = {
    val Array(a, b, c, d) = StdIn.readLine().split(' ').map(_.toInt)
    (a, b, c, d)
  }

  val n = StdIn.readLine().toInt
  val moles: Seq[Seq[(Int, Int, Int, Int)]] = (1 to n).map(_ => Seq.fill(4)(readAxis)).toSeq

  moles.foreach(xs => println(CaptainMarmot(xs).solve))
}

case class CaptainMarmot(moles: Seq[(Int, Int, Int, Int)]) {
  type Coord = (Int, Int)

  def solve: Int = {
    val scores = (for {
      (s1, c1) <- rotates(moles(0))
      (s2, c2) <- rotates(moles(1))
      (s3, c3) <- rotates(moles(2))
      (s4, c4) <- rotates(moles(3))
    } yield {
      (s1 + s2 + s3 + s4, isSquare(c1, c2, c3, c4))
    }).filter(_._2).map(_._1)
    if (scores.isEmpty) -1 else scores.min
  }

  private[this] def rotates(xs: (Int, Int, Int, Int)): Seq[(Int, Coord)] = {
    val (homeX, homeY, centerX, centerY) = xs
    Seq(
      (0, (homeX, homeY)),
      (1, (centerX - homeY + centerY, centerY + homeX - centerX)),
      (2, (centerX * 2 - homeX, centerY * 2 - homeY)),
      (3, (centerX + homeY - centerY, centerY - homeX + centerX))
    )
  }

  private[this] def isSquare(c1: Coord, c2: Coord, c3: Coord, c4: Coord): Boolean = {
    val sets = Set(c1, c2, c3, c4).map{ case (x, y) => (x * 4, y * 4) }
    if (sets.size != 4) {
      false
    } else {
      val (x, y) = sets.head
      val rs = rotates(x, y, c1._1 + c2._1 + c3._1 + c4._1, c1._2 + c2._2 + c3._2 + c4._2).map(_._2)
      rs.toSet == sets
    }
  }
}

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) {
    def ccw: Coord = Coord(-y, x)
    def +(o: Coord) = Coord(x + o.x, y + o.y)
  }
  case class Mole(v: Coord, center: Coord) {
    def relative: Coord = Coord(v.x - center.x, v.y - center.y) // 中心からの相対位置
  }

  def solve(): Unit = {

    def readMole(): Mole = {
      Mole(Coord(ni(), ni()), Coord(ni(), ni()))
    }

    def ccw(m: Mole): Seq[(Coord, Int)] = {
      val res = Array.ofDim[(Coord, Int)](4)
      var cur = m.relative
      var i = 0
      while (i < 4) {
        res(i) = (cur + m.center, i)
        cur = cur.ccw
        i += 1
      }
      res
    }

    def isSquare(p1: Coord, p2: Coord, p3: Coord, p4: Coord): Boolean = {
      val nums = mutable.Map[Long, Int]().withDefaultValue(0)
      val ps = Array(p1, p2, p3, p4)
      REP(4) { i =>
        i + 1 until 4 foreach { j =>
          val a = ps(i)
          val b = ps(j)
          val x = abs(a.x - b.x)
          val y = abs(a.y - b.y)
          val dist = x.toLong * x + y.toLong * y // ２乗の形を維持する
          nums(dist) = nums(dist) + 1
        }
      }

//      val a = nums.toSeq.sortBy(_._1).map(_._2)
////      System.err.println(nums.toSeq.sortBy(_._1).map(_._2))
//      if (a == Seq(2, 2, 2) || a == Seq(2, 4)) {
//        System.err.println(nums.toSeq.sortBy(_._1).map(_._2))
//        System.err.println(nums)
//      }

      nums.size == 2 && nums.toSeq.sortBy(_._1).map(_._2) == Seq(4, 2)
    }

    REP(ni()) { _ =>
      val m1, m2, m3, m4 = readMole()
      val ans = for {
        (p1, c1) <- ccw(m1)
        (p2, c2) <- ccw(m2)
        (p3, c3) <- ccw(m3)
        (p4, c4) <- ccw(m4)
        if isSquare(p1, p2,p3, p4)
      } yield c1 + c2 + c3 + c4

      if (ans.isEmpty) out.println(-1)
      else out.println(ans.min)
    }
  }

  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.annotation.tailrec

object    CF271_3 extends App {
  case class Mole(x:Long, y:Long, a:Long, b:Long)

  @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 rotate(x:Long, y:Long, a:Long, b:Long):(Long, Long) = {
    ((a + b - y), (x - a +b))
  }

  def rotateMole(mole:Mole, n:Int):(Long,Long) = {
    var vp = (mole.x, mole.y)
    for(i <- 0 until n) {
      vp = rotate(vp._1, vp._2, mole.a, mole.b)
    }
    vp
  }

  def distance (p1: (Long, Long), p2: (Long, Long)) = {
    val (p1x, p1y) = p1
    val (p2x, p2y) = p2
    val dx = p1x - p2x
    val dy = p1y - p2y
    Math.sqrt(dx*dx + dy*dy)
  }

  def checkSqr(points:Array[(Long, Long)]):Boolean = {
    val Array(x,y,w,z) = points
    val yd = distance(x, y)
    val wd = distance(x, w)
    val zd = distance(x, z)
    if(yd == 0.0 || wd == 0.0 || zd == 0.0) {
      return false
    }
    val (s1, s2, s3) = if(yd == zd) {
      (y,z,w)
    } else if (wd == zd) {
      (w, z, y)
    } else if (yd == wd) {
      (y, w, z)
    } else {
      return false
    }
    if(distance(s2, s3) != distance(s1, s3)) {
      return false
    }

    distance(x, s2) == distance(s2, s3) && distance(x, s3) == distance(s1, s2)
    //distance(x, s3) == distance(s1, s2) &&
  }

  val Array(n) = readInts(1)
  for(testcase <- 0 until n) {
    val moles = (1 to 4).toArray.map(_ => {
      val Array(x, y, a, b) = readLongs(4)
      val mole = Mole(x, y, a, b)
      mole
    })

    var sol = -1
    for(r1 <- 0 to 3; r2 <- 0 to 3; r3 <- 0 to 3; r4 <- 0 to 3) {
      val sum = r1 + r2 + r3 + r4
      if((sol >= 0 && sum < sol) || sol == -1){
        val points = Array(
          rotateMole(moles(0), r1),
          rotateMole(moles(1), r2),
          rotateMole(moles(2), r3),
          rotateMole(moles(3), r4)
        )
        if(checkSqr(points)){
          //println(r1,r2,r3,r4)
          sol = sum
        }
      }
    }
    println(sol)
  }

}

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) {
    def ccw: Coord = Coord(-y, x)
    def +(o: Coord) = Coord(x + o.x, y + o.y)
  }
  case class Mole(v: Coord, center: Coord) {
    def relative: Coord = Coord(v.x - center.x, v.y - center.y) // 中心からの相対位置
  }

  def solve(): Unit = {

    def readMole(): Mole = {
      Mole(Coord(ni(), ni()), Coord(ni(), ni()))
    }

    def ccw(m: Mole): Seq[(Coord, Int)] = {
      val res = Array.ofDim[(Coord, Int)](4)
      var cur = m.relative
      var i = 0
      while (i < 4) {
        res(i) = (cur + m.center, i)
        cur = cur.ccw
        i += 1
      }
      res
    }

    def isSquare(p1: Coord, p2: Coord, p3: Coord, p4: Coord): Boolean = {
      val nums = mutable.Map[Int, Int]().withDefaultValue(0)
      val ps = Array(p1, p2, p3, p4)
      REP(4) { i =>
        i + 1 until 4 foreach { j =>
          val a = ps(i)
          val b = ps(j)
          val x = abs(a.x - b.x)
          val y = abs(a.y - b.y)
          val dist = x * x + y * y // ２乗の形を維持する
          nums(dist) = nums(dist) + 1
        }
      }
      nums.size == 2 && nums.toSeq.sortBy(_._1).map(_._2) == Seq(4, 2)
    }

    REP(ni()) { _ =>
      val m1, m2, m3, m4 = readMole()
      val ans = for {
        (p1, c1) <- ccw(m1)
        (p2, c2) <- ccw(m2)
        (p3, c3) <- ccw(m3)
        (p4, c4) <- ccw(m4)
        if isSquare(p1, p2,p3, p4)
      } yield c1 + c2 + c3 + c4

      if (ans.isEmpty) out.println(-1)
      else out.println(ans.min)
    }
  }

  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) {
    def ccw: Coord = Coord(-y, x) // todo

    def +(o: Coord) = Coord(x + o.x, y + o.y)
  }
  case class Mole(v: Coord, center: Coord) {
    def relative: Coord = Coord(v.x - center.x, v.y - center.y) // 中心からの相対位置
  }

  def solve(): Unit = {

    def readMole(): Mole = {
      Mole(Coord(ni(), ni()), Coord(ni(), ni()))
    }

    def ccw(m: Mole): Seq[(Coord, Int)] = {
      val res = Array.ofDim[(Coord, Int)](4)
      var cur = m.relative
      var i = 0
      while (i < 4) {
        res(i) = (cur + m.center, i)
        cur = cur.ccw
        i += 1
      }
      res
    }

    def isSquare(p1: Coord, p2: Coord, p3: Coord, p4: Coord): Boolean = {
      val nums = mutable.Map[Int, Int]().withDefaultValue(0)
      val ps = Array(p1, p2, p3, p4)
      REP(4) { i =>
        i + 1 until 4 foreach { j =>
          val a = ps(i)
          val b = ps(j)
          val x = abs(a.x - b.x)
          val y = abs(a.y - b.y)
          val dist = x * x + y * y // ２乗の形を維持する
          nums(dist) = nums(dist) + 1
        }
      }
      nums.size == 2 && nums.values.toSet == Set(2, 4)
    }

    REP(ni()) { _ =>
      val m1, m2, m3, m4 = readMole()
      val ans = for {
        (p1, c1) <- ccw(m1)
        (p2, c2) <- ccw(m2)
        (p3, c3) <- ccw(m3)
        (p4, c4) <- ccw(m4)
        if isSquare(p1, p2,p3, p4)
      } yield c1 + c2 + c3 + c4

      if (ans.isEmpty) out.println(-1)
      else out.println(ans.min)
    }
  }

  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.annotation.tailrec

object    CF271_3 extends App {
  case class Mole(x:Long, y:Long, a:Long, b:Long)

  @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 rotate(x:Long, y:Long, a:Long, b:Long):(Long, Long) = {
    ((a + b - y), (x - a +b))
  }

  def rotateMole(mole:Mole, n:Int):(Long,Long) = {
    var vp = (mole.x, mole.y)
    for(i <- 0 until n) {
      vp = rotate(vp._1, vp._2, mole.a, mole.b)
    }
    vp
  }

  def distance (p1: (Long, Long), p2: (Long, Long)) = {
    val (p1x, p1y) = p1
    val (p2x, p2y) = p2
    val dx = p1x - p2x
    val dy = p1y - p2y
    Math.sqrt(dx*dx + dy*dy)
  }

  def checkSqr(points:Array[(Long, Long)]):Boolean = {
    val Array(x,y,w,z) = points
    val yd = distance(x, y)
    val wd = distance(x, w)
    val zd = distance(x, z)
    if(yd == 0.0 || wd == 0.0 || zd == 0.0) {
      return false
    }
    val (s1, s2, s3) = if(yd == zd) {
      (y,z,w)
    } else if (wd == zd) {
      (w, z, y)
    } else if (yd == wd) {
      (y, w, z)
    } else {
      return false
    }
    if(distance(s2, s3) != distance(s1, s3)) {
      return false
    }
    distance(x, s3) == distance(s1, s2)
  }

  val Array(n) = readInts(1)
  for(testcase <- 0 until n) {
    val moles = (1 to 4).toArray.map(_ => {
      val Array(x, y, a, b) = readLongs(4)
      val mole = Mole(x, y, a, b)
      mole
    })

    var sol = -1
    for(r1 <- 0 to 3; r2 <- 0 to 3; r3 <- 0 to 3; r4 <- 0 to 3) {
      val sum = r1 + r2 + r3 + r4
      if((sol >= 0 && sum < sol) || sol == -1){
        val points = Array(
          rotateMole(moles(0), r1),
          rotateMole(moles(1), r2),
          rotateMole(moles(2), r3),
          rotateMole(moles(3), r4)
        )
        if(checkSqr(points)){
          sol = sum
        }
      }
    }
    println(sol)
  }

}

import scala.annotation.tailrec

object    CF271_3 extends App {
  case class Mole(x:Int, y:Int, a:Int, b:Int)

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

  def rotate(x:Int, y:Int, a:Int, b:Int):(Int, Int) = {
    ((a + b - y), (x - a +b))
  }

  def rotateMole(mole:Mole, n:Int):(Int,Int) = {
    var vp = (mole.x, mole.y)
    for(i <- 0 until n) {
      vp = rotate(vp._1, vp._2, mole.a, mole.b)
    }
    vp
  }

  def distance (p1: (Int, Int), p2: (Int, Int)) = {
    val (p1x, p1y) = p1
    val (p2x, p2y) = p2
    val dx = p1x - p2x
    val dy = p1y - p2y
    Math.sqrt(dx*dx + dy*dy)
  }

  def checkSqr(points:Array[(Int, Int)]):Boolean = {
    val Array(x,y,w,z) = points
    val yd = distance(x, y)
    val wd = distance(x, w)
    val zd = distance(x, z)
    if(yd == 0.0 || wd == 0.0 || zd == 0.0) {
      return false
    }
    val (s1, s2, s3) = if(yd == zd) {
      (y,z,w)
    } else if (wd == zd) {
      (w, z, y)
    } else if (yd == wd) {
      (y, w, z)
    } else {
      return false
    }
    if(distance(s2, s3) != distance(s1, s3)) {
      return false
    }
    distance(x, s3) == distance(s1, s2)
  }

  val Array(n) = readInts(1)
  for(testcase <- 0 until n) {
    val moles = (1 to 4).toArray.map(_ => {
      val Array(x, y, a, b) = readInts(4)
      val mole = Mole(x, y, a, b)
      mole
    })

    var sol = -1
    for(r1 <- 0 to 3; r2 <- 0 to 3; r3 <- 0 to 3; r4 <- 0 to 3) {
      val sum = r1 + r2 + r3 + r4
      if((sol >= 0 && sum < sol) || sol == -1){
        val points = Array(
          rotateMole(moles(0), r1),
          rotateMole(moles(1), r2),
          rotateMole(moles(2), r3),
          rotateMole(moles(3), r4)
        )
        if(checkSqr(points)){
          sol = sum
        }
      }
    }
    println(sol)
  }

}


object A extends App {
  val n = readInt()
  val in = Array.fill(n)(readLine())
  val ls = for (i <- 1 to 12; j <- 1 to 12; if (i*j==12)) yield (j, i)
  for (s <- in) {
    var q = List[(Int,Int)]()
    for (c <- ls) {
      var yes = false;
      for (i <- 0 until c._2) {
        val t = for (j <- 0 until c._1) yield s(i + j*c._2);
        if (!yes) yes = !t.contains('O')
      }
      if (yes) q ::= c
    }

    print(q.length)
    for (t <- q) {
      print(" " + t._1 + "x" + t._2)
    }
    println();
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val res = (1 to n).map { _ =>
    val str = in.next()
    val res = List(1, 2, 3, 4, 6, 12).filter{i =>
      (0 until i).exists{j =>
        (j until 12 by i).forall{k => str(k ) == 'X'}
      }
    }.map(i => s"${12 / i}x$i").reverse
    s"${res.length} ${res.mkString(" ")}"

  }
  println(res.mkString("\n"))
}

object A400 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.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))
  }

  def main(args: Array[String]): Unit = {
    val Array(tests) = readInts(1)
    for (_ <- 0 until tests) {
      val input = scala.io.StdIn.readLine
      val size = input.length
      var res = 0
      var arr = Array[String]()
      for(i <- 1 to size) {
        if(size % i == 0 && solve(input, i)) {
          res += 1
          arr ++= Array(s"${i}x${size/i}")
        }
      }
      println(res + arr.mkString(" ", " ", ""))
    }
  }
  def solve(input: String, rows: Int): Boolean = {
    val col = input.length / rows
    val in = input.toArray.grouped(col).toArray
    (0 until col).exists(c => (0 until rows).forall(r => in(r)(c) == 'X'))
  }
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P400A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  sc.nextLine

  // 1x12, 2x6, 3x4, 4x3, 6x2, 12x1

  class TestCase {
    val cards = sc.nextLine.toList

    def solve(): List[String] = {

      @inline
      def isWinning(hand: List[Char], rowSize: Int): Boolean = {
        hand.sliding(rowSize, rowSize).toList.
          transpose.find(row => row.forall(_ == 'X')) match {
          case Some(_) => true
          case None    => false
        }
      }
      val rowSize = List(12, 6, 4, 3, 2, 1)
      rowSize.filter(isWinning(cards, _)).map { i =>
        (12 / i).toString + "x" + i.toString
      }
    }
  }

  for (_ <- 0 until N) {
    val answer = (new TestCase).solve
    val line = answer.size :: answer
    out.println(line.mkString(" "))
  }
  out.close
}

/**
 * Created by hama_du on 2014/02/24.
 */
object ProblemA extends App {
  val T = readInt
  (0 until T).foreach(i => {
    println(solve(readLine.toCharArray))
  })

  def solve(in: Array[Char]): String = {
    val answers = Array((1,12),(2,6),(3,4),(4,3),(6,2),(12,1)).filter(p => {
      (0 until p._2).foldLeft(false)((a, b) => a || isAllX(p, in, b))
    })
    if (answers.size == 0) {
      return "0"
    }
    return answers.size + " " +  answers.map(p => p._1 + "x" + p._2).mkString(" ")
  }

  def isAllX(p: (Int,Int), in: Array[Char], b: Int) = {
    (b until in.size by p._2).filter(idx => in(idx) == 'X').size == p._1
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val res = (1 to n).map { _ =>
    val str = in.next()
    val res = List(1, 2, 3, 4, 6, 12).filter{i =>
      (0 until i).exists{j =>
        (j until 12 by i).forall{k => str(k ) == 'X'}
      }
    }.map(i => s"${i}x${12 / i}")
    s"${res.length} ${res.mkString(" ")}"

  }
  println(res.mkString("\n"))
}


import scala.Console.in

object App extends App {
  val cases: Long = Console.in.readLine().toInt
  val data = Vector.range(0, cases).map(_ => {
    in.readLine()
    in.readLine().split(" ").toVector.map(_.toLong)
  })

  def f(arr: Vector[Long]): Long = {
    val zipped = arr.zipWithIndex
    val pMin = zipped.last._2

    def max(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) <= arr.apply(pos - 1)) pos - 1 else pos
    }

    def min(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) >= arr.apply(pos - 1)) pos - 1 else pos
    }

    val pMax = (zipped foldRight pMin) ((_, pos) => max(pos))
    (zipped foldRight pMax) ((_, pos) => min(pos))
  }

  val answer = for (datum <- data) yield f(datum)

  answer map(a => print(a.toString ++ "\n"))
}

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

  val t = readInt()

  (0 until t).foreach { _ =>
    val n  = readInt()
    val an = readLine().split(" ").map(_.toInt)

    val i = an.zipWithIndex.lastIndexWhere {
      case (_, 0) => true
      case (a, i) => a > an(i - 1)
    }

    val j = an.zipWithIndex.lastIndexWhere({
      case (_, 0) => true
      case (a, j) => a < an(j - 1)
    }, i)

    println(j)
  }
}

import scala.Console.in

object App extends App {
  val cases: Long = Console.in.readLine().toInt
  val data = Vector.range(0, cases).map(_ => {
    in.readLine()
    in.readLine().split(" ").toVector.map(_.toLong)
  })

  def f(arr: Vector[Long]): Long = {
    val pMin = arr.length - 1

    def max(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) <= arr.apply(pos - 1)) pos - 1 else pos
    }

    def min(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) >= arr.apply(pos - 1)) pos - 1 else pos
    }

    (arr foldRight (arr foldRight pMin) ((_, pos) => max(pos))) ((_, pos) => min(pos))
  }

  val answer = for (datum <- data) yield f(datum)

  answer map(a => {
    print(a.toString)
    print("\n")
  })
}

import scala.Console.in

object App extends App {
  val cases: Long = Console.in.readLine().toInt
  val data = Vector.range(0, cases).map(_ => {
    in.readLine()
    in.readLine().split(" ").toVector.map(_.toLong)
  })

  def f(arr: Vector[Long]): Long = {
    val pMin = arr.length - 1

    def max(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) <= arr.apply(pos - 1)) pos - 1 else pos
    }

    def min(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) >= arr.apply(pos - 1)) pos - 1 else pos
    }

    val pMax = (arr foldRight pMin) ((_, pos) => max(pos))
    (arr foldRight pMax) ((_, pos) => min(pos))
  }

  val answer = for (datum <- data) yield f(datum)

  answer map(a => print(a.toString ++ "\n"))
}

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

  val t = readInt()

  (0 until t).foreach { _ =>
    val n  = readInt()
    val an = readLine().split(" ").map(_.toInt)

    println()
    println(an.mkString(" "))

    val i = an.zipWithIndex.lastIndexWhere {
      case (_, 0) => true
      case (a, i) => a > an(i - 1)
    }

    val j = an.zipWithIndex.lastIndexWhere({
      case (_, 0) => true
      case (a, j) => a < an(j - 1)
    }, i)

    println(j)
  }
}

import scala.Console.in

object App extends App {
  val cases: Long = Console.in.readLine().toInt
  val data = Vector.range(0, cases).map(_ => {
    in.readLine()
    in.readLine().split(" ").toVector.map(_.toLong)
  })

  def f(arr: Vector[Long]): Long = {
    val zipped = arr.zipWithIndex
    val pMin = zipped.last._2

    def max(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) <= arr.apply(pos - 1)) pos - 1 else pos
    }

    def min(pos: Int): Int = pos match {
      case pos => if (pos > 0 && arr.apply(pos) >= arr.apply(pos - 1)) pos - 1 else pos
    }

    val pMax = (zipped foldRight pMin) ( (_, pos) => max(pos))
    print(pMax)
    (zipped foldRight pMax) ((_, pos) => min(pos))
  }

  val answer = for (datum <- data) yield f(datum)

  print(answer)
}


import scala.annotation.tailrec
import scala.io.StdIn

object SortMe {
  def main(args: Array[String]): Unit = {
    val t = StdIn.readInt()
    for (_ <- 1 to t) {
      val _ = StdIn.readInt()
      val nums = StdIn.readLine().trim.split(" ").map(_.toLong).sorted.toList
      val result = build(nums)
      println(result.mkString(" "))
    }

    def build(items: List[Long]): List[Long] = {
      @tailrec
      def go(accum: List[Long], xs: List[Long], ys: List[Long]): List[Long] = {
        (xs.headOption, ys.headOption) match {
          case (_, None) => accum
          case (None, Some(y)) => y +: accum
          case (Some(x), Some(y)) => go(x +: y +: accum, xs.tail, ys.tail)
        }
      }
      val (xs, ys) = items.splitAt(items.length / 2)
      go(List.empty[Long], xs, ys.reverse)
    }
  }
}

import scala.annotation.tailrec
import scala.io.StdIn

object SortMe {
  def main(args: Array[String]): Unit = {
    val t = StdIn.readInt()
    for (_ <- 1 to t) {
      val _ = StdIn.readInt()
      val nums = StdIn.readLine().trim.split(" ").map(_.toLong).sorted.toList
      val result = build(nums)
      println(result.mkString(" "))
    }

    def build(items: List[Long]): List[Long] = {
      @tailrec
      def go(accum: List[Long], xs: List[Long], ys: List[Long]): List[Long] = {
        (xs, ys) match {
          case (_, Nil) => accum
          case (Nil, y :: Nil) => y +: accum
          case (x :: tailx, y :: taily) => go(x +: y +: accum, tailx, taily)
        }
      }
      val (xs, ys) = items.splitAt(items.length / 2)
      go(List.empty[Long], xs, ys.reverse)
    }
  }
}

object B extends App {

  val t = scala.io.StdIn.readInt()

  (0 until t).foreach { _ =>
    val n  = scala.io.StdIn.readInt()
    val m  = (n - 1) / 2
    val an = scala.io.StdIn.readLine().split(" ").map(_.toInt).sorted
    val bn = List(m, m + (n + 1) % 2).distinct.map(an) ::: (0 until m).foldLeft(List.empty[Int])((bn, i) =>
      an(i) :: an(n - i - 1) :: bn
    )

    println(bn.mkString(" "))
  }
}

object B extends App {
  val t = scala.io.StdIn.readInt()

  (0 until t).foreach { _ =>
    val n  = scala.io.StdIn.readInt()
    val an = scala.io.StdIn.readLine().split(" ").map(_.toInt).sorted

    val b  = an(an.length / 2)
    val bn = an.sortBy(a => (a - b).abs)

    println(bn.mkString(" "))
  }
}

import scala.io.StdIn
object SortMe {
  def main(args: Array[String]): Unit = {
    val t = StdIn.readInt()
    for (_ <- 1 to t) {
      val _ = StdIn.readInt()
      val nums = StdIn.readLine().trim.split(" ").map(_.toLong)
      val sorted = nums.indices
        .sortBy { index =>
          if (index == nums.length - 1) math.abs(nums(index))
          else math.abs(nums(index+1) - nums(index))
        }
        .map(nums(_))

      System.out.println(sorted.mkString(" "))
    }
  }
}


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

object B extends App {

  def solve(in: InputReader): Unit = {
    val n = in.nextInt
    val primes = listPrimes()
    var initialDivisors: Seq[Int] = {
      val l = in.nextInt
      val r = in.nextInt
      (divisors(l, primes) ::: divisors(r, primes)).distinct
    }
    for (_ <- 2 to n) {
//      println(initialDivisors)
      val l = in.nextInt
      val r = in.nextInt
      initialDivisors = initialDivisors.filter(div => (l % div == 0) || (r % div == 0))
    }
    println(initialDivisors.headOption.getOrElse(-1))
  }

  def divisors(n: Int, knownPrimes: Vector[Int]): List[Int] = {
    var remaining: Int = n
    var result: List[Int] = Nil
    var i = 0
    var cur = 0
    while (remaining > 1 && i < knownPrimes.length) {
      cur = knownPrimes(i)
      if (remaining % cur == 0) {
        result = cur :: result
        while (remaining % cur == 0) {
          remaining = remaining / cur
        }
      } else {
        i = i + 1
      }
    }
    if (remaining > 1) {
      result = remaining :: result
    }
    result
  }

  def listPrimes(): Vector[Int] = {
    val m = Math.sqrt(2 * 1000 * 1000 * 1000).ceil.toInt
    val isPrime = primesAux(m)
    var res = List.empty[Int]
    for (i <- isPrime.indices.reverse) {
      if (isPrime(i)) {
        res = i :: res
      }
    }
    res.toVector
  }

  def primesAux(n: Int): Array[Boolean] = {
    val p = Array.fill(n + 1)(true)
    val nl = n.toLong
    p.update(0, false)
    p.update(1, false)
    for (i <- 2 to n) {
      if (p(i) && i.toLong * i.toLong <= nl) {
        for (j <- (i * i).to(n, i)) {
          p.update(j, false)
        }
      }
    }
    p
  }

  solve(new InputReader(System.in))

  private class InputReader(val in: InputStream) {

    private val reader: BufferedReader = new BufferedReader(new InputStreamReader(in))
    private var tokenizer: StringTokenizer = _

    def nextInt: Int = {
      next.toInt
    }

    def nextLong: Long = {
      next.toLong
    }

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

    def readLine: String = {
      try {
        reader.readLine()
      } catch {
        case e: IOException => throw new RuntimeException(e)
      }
    }
  }

}

object B extends App {

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

  val Array(n) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    var addA, addB = 0
    var aComposite, bComposite = false

    val pit = primes.iterator
    while (pit.hasNext) {

      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else if (i == 0) {
        if (a % p == 0) {
          aComposite = true
          var d = a / p
          for (p <- isPrime.indices) if (isPrime(p)) {
            while (d % p == 0) d /= p
          }
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          bComposite = true
          var d = b / p
          for (p <- isPrime.indices) if (isPrime(p)) {
            while (d % p == 0) d /= p
          }
          if (d > LIMIT) addB = d
        }
      }
    }

    if (i == 0) {
      if (!aComposite) primes.add(a)
      if (!bComposite) primes.add(b)
      if (addA > 0) primes.add(addA)
      if (addB > 0) primes.add(addB)
    }

  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

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

object B extends App {

  def solve(in: InputReader): Unit = {
    val n = in.nextInt
    val primes = listPrimes()
    var initialDivisors: Seq[Int] = {
      val l = in.nextInt
      val r = in.nextInt
      (divisors(l, primes) ::: divisors(r, primes)).distinct
    }
    for (_ <- 2 to n) {
//      println(initialDivisors)
      val l = in.nextInt
      val r = in.nextInt
      initialDivisors = initialDivisors.filter(div => (l % div == 0) || (r % div == 0))
    }
    println(initialDivisors.headOption.getOrElse(-1))
  }

  def divisors(n: Int, knownPrimes: Vector[Int]): List[Int] = {
    var remaining: Int = n
    var result: List[Int] = Nil
    var i = 0
    var cur = 0
    while (remaining >= 1 && i < knownPrimes.length) {
      cur = knownPrimes(i)
      if (remaining % cur == 0) {
        result = cur :: result
        while (remaining % cur == 0) {
          remaining = remaining / cur
        }
      } else {
        i = i + 1
      }
    }
    if (result.nonEmpty) result else List(n)
  }

  def listPrimes(): Vector[Int] = {
    val m = Math.sqrt(2 * 1000 * 1000 * 1000).ceil.toInt
    val ids = primesAux(m)
    var res = List.empty[Int]
    for (i <- ids.indices.reverse) {
      if (ids(i)) {
        res = i :: res
      }
    }
    res.toVector
  }

  def primesAux(n: Int): Array[Boolean] = {
    val p = Array.fill(n + 1)(true)
    val nl = n.toLong
    p.update(0, false)
    p.update(1, false)
    for (i <- 2 to n) {
      if (p(i) && i.toLong * i.toLong <= nl) {
        for (j <- (i * i).to(n, i)) {
          p.update(j, false)
        }
      }
    }
    p
  }

  /*
int n;
vector<char> prime (n+1, true);
prime[0] = prime[1] = false;
for (int i=2; i<=n; ++i)
	if (prime[i])
		if (i * 1ll * i <= n)
			for (int j=i*i; j<=n; j+=i)
				prime[j] = false;
				*/

  solve(new InputReader(System.in))

  private class InputReader(val in: InputStream) {

    private val reader: BufferedReader = new BufferedReader(new InputStreamReader(in))
    private var tokenizer: StringTokenizer = _

    def nextInt: Int = {
      next.toInt
    }

    def nextLong: Long = {
      next.toLong
    }

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

    def readLine: String = {
      try {
        reader.readLine()
      } catch {
        case e: IOException => throw new RuntimeException(e)
      }
    }
  }

}

object B extends App {

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

  val Array(n) = readInts(1)

  val LIMIT = 50000
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    var addA, addB = 0
    var aComposite, bComposite = false

    val pit = primes.iterator
    while (pit.hasNext) {

      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else if (i == 0) {
        if (a % p == 0) {
          aComposite = true
          val d = a / p
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          bComposite = true
          val d = b / p
          if (d > LIMIT) addB = d
        }
      }
    }

    if (i == 0) {
      if (!aComposite) primes.add(a)
      if (!bComposite) primes.add(b)
      if (addA > 0) primes.add(addA)
      if (addB > 0) primes.add(addB)
    }

  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

object B extends App {

  @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) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    val pit = primes.iterator
    var addA, addB = 0
    while (pit.hasNext) {
      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else {
        if (a % p == 0) {
          val d = a / p
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          val d = b / p
          if (d > LIMIT) addB = d
        }
      }
    }
    if (addA > 0) primes.add(addA)
    if (addB > 0) primes.add(addB)
    if (i == 0 && primes.isEmpty) {
      primes.add(a)
      primes.add(b)
    }
  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

object B extends App {

  @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) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    val pit = primes.iterator
    var addA, addB = 0
    while (pit.hasNext) {
      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else if (i == 0) {
        if (a % p == 0) {
          val d = a / p
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          val d = b / p
          if (d > LIMIT) addB = d
        }
      }
    }
    if (i == 0) {
      if (primes.isEmpty) {
        primes.add(a)
        primes.add(b)
      }
      if (addA > 0) primes.add(addA)
      if (addB > 0) primes.add(addB)
    }
  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

object B extends App {

  @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) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    val pit = primes.iterator
    while (pit.hasNext) {
      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
    }
    if (i == 0 && primes.isEmpty) {
      primes.add(a)
      primes.add(b)
    }
  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

object B extends App {

  @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) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    val pit = primes.iterator
    var addA, addB = 0
    while (pit.hasNext) {
      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else {
        if (a % p == 0) {
          val d = a / p
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          val d = b / p
          if (d > LIMIT) addB = d
        }
      }
    }
    if (addA > 0) primes.add(a)
    if (addB > 0) primes.add(b)
    if (i == 0 && primes.isEmpty) {
      primes.add(a)
      primes.add(b)
    }
  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

object B extends App {

  @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) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    val pit = primes.iterator
    var addA, addB = 0
    var aComposite, bComposite = false
    while (pit.hasNext) {
      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else if (i == 0) {
        if (a % p == 0) {
          aComposite = true
          val d = a / p
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          bComposite = true
          val d = b / p
          if (d > LIMIT) addB = d
        }
      }
    }
    if (i == 0) {
      if (!aComposite) primes.add(a)
      if (!bComposite) primes.add(b)
      if (addA > 0) primes.add(addA)
      if (addB > 0) primes.add(addB)
    }
  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}

object B extends App {

  @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) = readInts(1)

  val LIMIT = 44725
  val isPrime = Array.fill(LIMIT)(true)
  isPrime(0) = false
  isPrime(1) = false

  var i = 2
  while (i * i < LIMIT) {
    var j = i * i
    if (isPrime(j)) {
      while (j < LIMIT) {
        isPrime(j) = false
        j += i
      }
    }
    i += 1
  }

  val primes = new java.util.TreeSet[Int]
  for (p <- isPrime.indices) if (isPrime(p)) primes.add(p)

  for (i <- 0 until n) {
    val Array(a, b) = readInts(2)
    val pit = primes.iterator
    var addA, addB = 0
    while (pit.hasNext) {
      val p = pit.next()
      if (a % p != 0 && b % p != 0) pit.remove()
      else if (i == 0) {
        if (a % p == 0) {
          val d = a / p
          if (d > LIMIT) addA = d
        }
        if (b % p == 0) {
          val d = b / p
          if (d > LIMIT) addB = d
        }
      }
    }
    if (i == 0) {
      if (primes.isEmpty) {
        primes.add(a)
        primes.add(b)
      } else {
        if (addA > 0) primes.add(addA)
        if (addB > 0) primes.add(addB)
      }
    }
  }

  println(if (!primes.isEmpty) primes.iterator.next() else -1)
}


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

import scala.language.implicitConversions

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

  def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    val bw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)))

    val A = br.readLine()
//    val range = 0.until(200000/3);
//    val A = range.map(_ => '0').mkString("") + range.map(_ => '1').mkString("") + range.map(_ => '0').mkString("");
//    val t0 = System.nanoTime()

    case class Ret(valid: Boolean, seqs: List[List[Int]])
    def rec(ind: Int, ez: List[List[Int]], eo: List[List[Int]]): Ret = {
      if (ind == A.size) if (eo.size == 0) Ret(true, ez) else Ret(false, null)
      else {
        if (A(ind) == '0') {
          if (eo.isEmpty) rec(ind + 1, List(ind + 1) :: ez, eo)
          else {
            val first = eo.head
            rec(ind + 1, ((ind + 1) :: first) :: ez, eo.tail)
          }
        } else {
          if (ez.isEmpty) Ret(false, null)
          else {
            val first = ez.head
            rec(ind + 1, ez.tail, ((ind + 1) :: first) :: eo)
          }
        }
      }
    }

    val result = rec(0, List[List[Int]](), List[List[Int]]())

    val t1 = System.nanoTime()
    if (result.valid) {
      bw.println(result.seqs.size)
      result.seqs.foreach(seq => {bw.print(seq.size); bw.print(" "); seq.reverse.foreach(x => {bw.print(x); bw.print(" ")}); bw.println()})
    } else {
      bw.println("-1")
    }

//    bw.println(t1 - t0 + "")
    bw.close()
    br.close()
  }
}

object cTask3 {

  import scala.io.StdIn.readLine
  import scala.collection.mutable
  import scala.collection.mutable.ArrayBuffer

  def zebra(s: List[Char]):Option[List[List[Int]]] = {

    def helper(lst: List[Char], acc0: List[List[Int]], acc1: List[List[Int]], num: Int): Option[List[List[Int]]] = {
      lst match {
        case Nil if acc1.isEmpty => Some(acc0)
        case Nil => None
        case '0'::ls => acc1 match {
          case a::as1 =>helper(ls, (num::a)::acc0, as1, num + 1)
          case _ => helper(ls, List(num)::acc0, acc1, num + 1)
        }
        case '1'::ls => acc0 match {
          case a::as0 => helper(ls, as0, (num::a)::acc1, num + 1)
          case _ => None
        }
      }
    }
    helper(s, List(), List(), 1)
  }

  def zebra1(s: Array[Char]):Option[List[mutable.ArrayBuffer[Int]]] = {

    var all: ArrayBuffer[mutable.ArrayBuffer[Int]]=ArrayBuffer()
    var idx = 0
    var cutOff = 0

    for (i <- s.indices) {
      if(s(i) == '0'){
        if(cutOff != 0) {
          cutOff-=1
          all(cutOff)+=(i+1)
        } else {
          idx+=1
          all+=mutable.ArrayBuffer(i + 1)
        }
      } else {
        if(cutOff != idx){
          all(cutOff)+=(i+1)
          cutOff+=1
        } else {
          return None
        }
      }
    }
    if (cutOff != 0) None
    else Some(all.toList)

    }

  def main(args: Array[String]): Unit = {
    val s = readLine.toCharArray.toList
    zebra(s) match {
      case None => println("-1")
      case Some(xs) => println(xs.length + "\n" + xs.map(x => x.length + " " + x.reverse.mkString(" ")).mkString("\n"))
    }

  }
}

object cTask3 {

  import scala.io.StdIn.readLine
  import scala.collection.mutable
  import scala.collection.mutable.ArrayBuffer

  def zebra(s: List[Char]):Option[List[List[Int]]] = {

    def helper(lst: List[Char], acc0: List[List[Int]], acc1: List[List[Int]], num: Int): Option[List[List[Int]]] = {
      lst match {
        case Nil if acc1.isEmpty => Some(acc0)
        case Nil => None
        case '0'::ls => acc1 match {
          case a::as1 =>helper(ls, (num::a)::acc0, as1, num + 1)
          case _ => helper(ls, List(num)::acc0, acc1, num + 1)
        }
        case '1'::ls => acc0 match {
          case a::as0 => helper(ls, as0, (num::a)::acc1, num + 1)
          case _ => None
        }
      }
    }
    helper(s, List(), List(), 1)
  }

  def zebra1(s: Array[Char]):Option[List[mutable.ArrayBuffer[Int]]] = {

    var all: ArrayBuffer[mutable.ArrayBuffer[Int]]=ArrayBuffer()
    var idx = 0
    var cutOff = 0

    for (i <- s.indices) {
      if(s(i) == '0'){
        if(cutOff != 0) {
          cutOff-=1
          all(cutOff)+=(i+1)
        } else {
          idx+=1
          all+=mutable.ArrayBuffer(i + 1)
        }
      } else {
        if(cutOff != idx){
          all(cutOff)+=(i+1)
          cutOff+=1
        } else {
          return None
        }
      }
    }
    if (cutOff != 0) None
    else Some(all.toList)

    }

  def main(args: Array[String]): Unit = {
    val s = readLine.toCharArray
    zebra1(s) match {
      case None => println("-1")
      case Some(xs) => println(xs.length + "\n" + xs.map(x => x.length + " " + x.mkString(" ")).mkString("\n"))
    }

  }
}

object cTask3 {

  import scala.io.StdIn.readLine
  import scala.collection.mutable
  import scala.collection.mutable.ArrayBuffer

  def zebra(s: List[Char]):Option[List[List[Int]]] = {

    def helper(lst: List[Char], acc0: List[List[Int]], acc1: List[List[Int]], num: Int): Option[List[List[Int]]] = {
      lst match {
        case Nil if acc1.isEmpty => Some(acc0)
        case Nil => None
        case '0'::ls => acc1 match {
          case a::as1 =>helper(ls, (num::a)::acc0, as1, num + 1)
          case _ => helper(ls, List(num)::acc0, acc1, num + 1)
        }
        case '1'::ls => acc0 match {
          case a::as0 => helper(ls, as0, (num::a)::acc1, num + 1)
          case _ => None
        }
      }
    }
    helper(s, List(), List(), 1)
  }

  def zebra1(s: Array[Char]):Option[List[mutable.ArrayBuffer[Int]]] = {

    var all: ArrayBuffer[mutable.ArrayBuffer[Int]]=ArrayBuffer()
    var idx = 0
    var cutOff = 0

    for (i <- s.indices) {
      if(s(i) == '0'){
        if(cutOff != 0) {
          cutOff-=1
          all(cutOff)+=(i+1)
        } else {
          idx+=1
          all+=mutable.ArrayBuffer(i + 1)
        }
      } else {
        if(cutOff != idx){
          all(cutOff)+=(i+1)
          cutOff+=1
        } else {
          return None
        }
      }
    }
    if (cutOff != 0) None
    else Some(all.toList)

    }

  def main(args: Array[String]): Unit = {
    val s = readLine.toCharArray.toList
    zebra(s) match {
      case None => println("-1")
      case Some(xs) => println(xs.length + "\n" + xs.map(x => x.length + " " + x.mkString(" ")).mkString("\n"))
    }

  }
}

object cTask3 {

  import scala.io.StdIn.{readLine, readInt}

  def zebra(s: List[Int]):Option[List[List[Int]]] = {

    def helper(lst: List[Int], acc: List[List[(Int, Int)]], num: Int): Option[List[List[(Int, Int)]]] = {
      def helpmat(elem: Int, num: Int, acc1: List[List[(Int, Int)]], acc2: List[List[(Int, Int)]]): Option[List[List[(Int, Int)]]]={
        acc2 match {
          case Nil if elem == 0 => Some(List((0, num))::acc1)
          case Nil  => None
          case x::xs if elem != x.head._1 => Some(((elem, num)::x)::xs ++ acc1)
          case x::xs => helpmat(elem, num, x::acc1, xs)
        }
      }

      lst match {
        case Nil => Some(acc)
        case x::xs => helpmat(x, num, List(), acc) match {
          case None => None
          case Some(newAcc) => helper(xs, newAcc, num + 1)
        }
      }

    }
    helper(s, List(), 1) match {
      case Some(e) if e.forall(_.head._1 == 0) => Some(e.map(y => y.map(_._2)))
      case _ => None
    }
  }

  def main(args: Array[String]): Unit = {
    var s = readLine.toCharArray.map(_.asDigit).toList
    zebra(s) match {
      case None => println("-1")
      case Some(xs) => xs.foreach(x => println(x.length + " " + x.reverse.mkString(" ")))
    }

  }
}

object cTask3 {

  import scala.io.StdIn.readLine
  import scala.collection.mutable

  def zebra(s: List[Char]):Option[List[List[Int]]] = {

    def helper(lst: List[Char], acc0: List[List[Int]], acc1: List[List[Int]], num: Int): Option[List[List[Int]]] = {
      lst match {
        case Nil if acc1.isEmpty => Some(acc0)
        case Nil => None
        case '0'::ls => acc1 match {
          case a::as1 =>helper(ls, (num::a)::acc0, as1, num + 1)
          case _ => helper(ls, List(num)::acc0, acc1, num + 1)
        }
        case '1'::ls => acc0 match {
          case a::as0 => helper(ls, as0, (num::a)::acc1, num + 1)
          case _ => None
        }
      }
    }
    helper(s, List(), List(), 1)
  }

  def zebra1(s: Array[Char]):Option[List[List[Int]]] = {

    val acc0:mutable.ArrayBuffer[List[Int]] = mutable.ArrayBuffer()
    val acc1:mutable.ArrayBuffer[List[Int]] = mutable.ArrayBuffer()

    for (i <- s.indices) {
      if(s(i) == '0'){
        if(acc1.nonEmpty) {
          acc0+=i::acc1.head
          acc1-=acc1(0)
        } else acc0 += List(i)
      } else {
        if(acc0.nonEmpty){
          acc1+=i::acc0.head
          acc0-=acc0(0)
        } else {
          return None
        }
      }
    }
    if (acc1.nonEmpty) None
    else Some(acc0.toList)

    }

  def main(args: Array[String]): Unit = {
    val s = readLine.toCharArray
    zebra1(s) match {
      case None => println("-1")
      case Some(xs) => println(xs.length); xs.foreach(x => println(x.length + " " + x.reverse.mkString(" ")))
    }

  }
}

import java.io.BufferedWriter
import java.io.OutputStreamWriter
import java.io.InputStreamReader
import java.io.BufferedReader
import java.util.Scanner

import scala.language.implicitConversions

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

  def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    val bw = new BufferedWriter(new OutputStreamWriter(System.out))

    val A = br.readLine()
//    val A = 0.until(200000).map(_ => '0').mkString("")
//    val t0 = System.nanoTime()

    case class Ret(valid: Boolean, seqs: List[List[Int]])
    def rec(ind: Int, ez: List[List[Int]], eo: List[List[Int]]): Ret = {
      if (ind == A.size) if (eo.size == 0) Ret(true, ez) else Ret(false, null)
      else {
        if (A(ind) == '0') {
          if (eo.size == 0) rec(ind + 1, List(ind + 1) :: ez, eo)
          else {
            val first = eo.head
            rec(ind + 1, ((ind + 1) :: first) :: ez, eo.tail)
          }
        } else {
          if (ez.size == 0) Ret(false, null)
          else {
            val first = ez.head
            rec(ind + 1, ez.tail, ((ind + 1) :: first) :: eo)
          }
        }
      }
    }

    val result = rec(0, List[List[Int]](), List[List[Int]]())

    if (result.valid) {
      bw.write(result.seqs.size + "\n")
      result.seqs.foreach(seq => {bw.write(seq.size + " "); seq.foldRight(0)((x, _) => {bw.write(x + " "); 0}); bw.write("\n")})
    } else {
      bw.write("-1\n")
    }

//    val t1 = System.nanoTime()
//    bw.write(t1 - t0 + "")

//    bw.flush()
  }
}

import java.io.BufferedWriter
import java.io.OutputStreamWriter
import java.util.Scanner

import scala.language.implicitConversions

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

  def main(args: Array[String]): Unit = {
    val bw = new BufferedWriter(new OutputStreamWriter(System.out))

//    val A = sc.next()
    val A = 0.until(200000).map(_ => '0').mkString("")
    val t0 = System.nanoTime()

    case class Ret(valid: Boolean, seqs: List[List[Int]])
    def rec(ind: Int, ez: List[List[Int]], eo: List[List[Int]]): Ret = {
      if (ind == A.size) if (eo.size == 0) Ret(true, ez) else Ret(false, null)
      else {
        if (A(ind) == '0') {
          if (eo.size == 0) rec(ind + 1, List(ind) :: ez, eo)
          else {
            val first = eo.head
            rec(ind + 1, (ind :: first) :: ez, eo.tail)
          }
        } else {
          if (ez.size == 0) Ret(false, null)
          else {
            val first = ez.head
            rec(ind + 1, ez.tail, (ind :: first) :: eo)
          }
        }
      }
    }

    val result = rec(0, List[List[Int]](), List[List[Int]]())

    if (result.valid) {
      bw.write(result.seqs.size + "\n")
      result.seqs.foreach(seq => {bw.write(seq.size + " "); bw.write(seq.reverse.map(_ + 1).mkString(" ")); bw.write("\n")})
    } else {
      bw.write("-1\n")
    }

    val t1 = System.nanoTime()
//    bw.write(t1 - t0 + "")

    bw.flush()
  }
}

import java.io.BufferedWriter
import java.io.OutputStreamWriter
import java.io.InputStreamReader
import java.io.BufferedReader
import java.util.Scanner

import scala.language.implicitConversions

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

  def main(args: Array[String]): Unit = {
    val br = new BufferedReader(new InputStreamReader(System.in))
    val bw = new BufferedWriter(new OutputStreamWriter(System.out))

//    val A = br.readLine()
    val A = 0.until(200000).map(_ => '0').mkString("")
//    val t0 = System.nanoTime()

    case class Ret(valid: Boolean, seqs: List[List[Int]])
    def rec(ind: Int, ez: List[List[Int]], eo: List[List[Int]]): Ret = {
      if (ind == A.size) if (eo.size == 0) Ret(true, ez) else Ret(false, null)
      else {
        if (A(ind) == '0') {
          if (eo.size == 0) rec(ind + 1, List(ind + 1) :: ez, eo)
          else {
            val first = eo.head
            rec(ind + 1, ((ind + 1) :: first) :: ez, eo.tail)
          }
        } else {
          if (ez.size == 0) Ret(false, null)
          else {
            val first = ez.head
            rec(ind + 1, ez.tail, ((ind + 1) :: first) :: eo)
          }
        }
      }
    }

    val result = rec(0, List[List[Int]](), List[List[Int]]())

    if (result.valid) {
      bw.write(result.seqs.size + "\n")
      result.seqs.foreach(seq => {bw.write(seq.size + " "); seq.reverse.foreach(x => bw.write(x + " ")); bw.write("\n")})
    } else {
      bw.write("-1\n")
    }

//    val t1 = System.nanoTime()
//    bw.write(t1 - t0 + "")

    bw.flush()
  }
}