import java.io.InputStreamReader
import java.io.BufferedReader

/**
 * @author pvasilyev
 * @since 25 Jun 2016
 */
object A222 extends App {

  def solve() = {
    val reader = new BufferedReader(new InputStreamReader(System.in))
    val Array(n, k) = reader.readLine().split(" ").map(_.toInt)
    val a: Array[Int] = reader.readLine().split(" ").map(_.toInt)

    var c = 0
    var eq = true
    for (i <- k - 1 until n) {
      if (c == 0) {
        c = a(i)
      } else if (c != a(i)) {
        eq = false
      }
    }

    if (!eq) {
      println("-1")
    } else {

      var index = k - 1
      for (i <- (k - 1).until(0, -1)) {

        if (eq) {
          if (a(index - 1) == a(i)) {
            index -= 1
          } else {
            eq = false
          }
        }
      }

      println(index)
    }
  }

  solve()

}

object test5 extends App {
    val Array(n,k)=readLine.split(' ').map(_.toInt)
    val arr=readLine.split(' ').map(_.toInt)
    if(arr.slice(k-1,n).distinct.size>1)
    {
    	println(-1)
    }
    else
    {
    	def find(i:Int):Int=i match{
    		case -1=> 0
    		case _ if arr(i)==arr(k-1)=>find(i-1)
    		case _=>i+1
    	}

        println(find(k-2))
    }
}

object Main {
  def main(args: Array[String]) = {
    val nk = readLine().split(" ").map(_.toInt)
    val a = readLine().split(" ").map(_.toInt)
    val n = nk(0)
    val k = nk(1)

    if (a.drop(k - 1).toSet.size != 1) println("-1")
    else {
      val e = a(k - 1)
      val c = a.take(k - 1).reverse.takeWhile(_ == e).size
      println(k - c - 1)
    }
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, k) = in.next().split(" ").map(_.toInt)
  val data = in.next().split(" ").map(_.toInt)
  val lastIndex = (n - 2 to 0 by -1).find(i => data(i) != data.last)
  if (lastIndex.isEmpty)
    println(0)
  else if (lastIndex.get + 1 >= k)
    println(-1)
  else
    println(lastIndex.get + 1)
}

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

object P222A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)
  val N, K = sc.nextInt
  val a = List.fill(N)(sc.nextInt)

  val n = a(K - 1)
  val i = a.lastIndexWhere(_ != n)
  val answer = if (i > K - 1) -1
               else i + 1

  out.println(answer)
  out.close
}

import java.io._
import java.util.Locale
import scala.annotation.tailrec

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

  val Array(n, k) = readInts
  val a = readInts
  def cnt = a.drop(k - 1).sliding(2).count(x => x.last != x.head)
  def ans = if(cnt == 0) n - a.reverse.takeWhile(_ == a.last).length else -1

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

object test5 extends App {
    val Array(n,k)=readLine.split(' ').map(_.toInt)
    val arr=readLine.split(' ').map(_.toInt)
    if(arr.slice(k-1,n).distinct.size>1)
    {
    	println(-1)
    }
    else
    {
        var i=k-2
        while(i>=0 && arr(i)==arr(i+1))
        	i-=1
        println(i+1)
    }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, k) = in.next().split(" ").map(_.toInt)
  val data = in.next().split(" ").map(_.toInt)
  val lastIndex = (n - 2 to 0 by -1).find(_ != data.last)
  if (lastIndex.isEmpty)
    println(0)
  else if (lastIndex.get + 1 >= k)
    println(-1)
  else
    println(lastIndex.get + 1)
}

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

object P222A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)
  val N, K = sc.nextInt
  val a = List.fill(N)(sc.nextInt)

  def solve1(): Int = if (a.distinct.size == 1) 0
                      else -1

  def solveM(): Int = {

    @tailrec
    def loop(n: Int, as: List[Int]): Int = {
      if (as.distinct.size == 1) n
      else {
        val (f, r) = as splitAt (K - 1)
        loop(n + 1, f.tail ++ r.tail ++ List(r.head))
      }
    }

    loop(0, a)
  }

  def solve(): Int = K match {
      case 1 | `N` => solve1
      case _ => solveM
  }

  out.println(solve)
  out.close
}

import java.io.InputStreamReader
import java.io.BufferedReader

/**
 * @author pvasilyev
 * @since 25 Jun 2016
 */
object A222 extends App {

  def solve() = {
    val reader = new BufferedReader(new InputStreamReader(System.in))
    val Array(n, k) = reader.readLine().split(" ").map(_.toInt)
    val a: Array[Int] = reader.readLine().split(" ").map(_.toInt)

    var c = 0
    var eq = true
    for (i <- k - 1 until n) {
      if (c == 0) {
        c = a(i)
      } else if (c != a(i)) {
        eq = false
      }
    }

    if (!eq) {
      println("-1")
    } else {
      println(k - 1)
    }
  }

  solve()

}

object test5 extends App {
    val Array(n,k)=readLine.split(' ').map(_.toInt)
    val arr=readLine.split(' ').map(_.toInt)
    if(arr.distinct.size==1)
    {
    	println(0)
    }
    else
    {
        arr(0)=arr(k-1)

        if(arr.distinct.size==1)
        	println(1)
        else
        	println(-1)
    }
}

object test5 extends App {
    val Array(n,k)=readLine.split(' ').map(_.toInt)
    val arr=readLine.split(' ').map(_.toInt)
    if(arr.slice(k-1,n).distinct.size>1)
    {
    	println(-1)
    }
    else
    {
    	def find(i:Int):Int=i match{
    		case -1=> 0
    		case _ if i==arr(k-1)=>find(i-1)
    		case _=>i+1
    	}

        println(find(k-2))
    }
}

object test5 extends App {
    val Array(n,k)=readLine.split(' ').map(_.toInt)
    val arr=readLine.split(' ').map(_.toInt)
    arr(0)=arr(k-1)

    if(arr.distinct.size==1)
    	println(1)
    else
    	println(-1)
}


import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val minByLen = Array.ofDim[Int](n + 1)
  val as = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    var i = 0
    while (i < adj(u).length) {
      val v = adj(u)(i)
      if (v != prev) dfs(v, u, depth + 1)
      i += 1
    }

    if (adj(u).length == 1 && depth >= max) {

      var len = 0

      for (i <- 0 to depth) {

        var lo = 1
        var hi = len

        while (lo <= hi) {
          val mid = (lo + hi) >> 1
          if (as(minByLen(mid)) < as(i)) lo = mid + 1
          else hi = mid - 1
        }

        minByLen(lo) = i
        if (lo > len) len = lo
      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val minByLen = Array.ofDim[Int](n + 1)
  val as = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    for (v <- adj(u))
      if (v != prev) dfs(v, u, depth + 1)

    if (adj(u).length == 1 && depth >= max) {

      var len = 0

      for (i <- 0 to depth) {

        var lo = 1
        var hi = len

        while (lo <= hi) {
          val mid = (lo + hi) >> 1
          if (as(minByLen(mid)) < as(i)) lo = mid + 1
          else hi = mid - 1
        }

        minByLen(lo) = i
        if (lo > len) len = lo
      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val minByLen = Array.ofDim[Int](n + 1)
  val as = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    var i = 0
    while (i < adj(u).length) {
      val v = adj(u)(i)
      if (v != prev) dfs(v, u, depth + 1)
      i += 1
    }

    if (adj(u).length == 1 && depth >= max) {

      var i, len = 0

      while (i <= depth) {

        var lo = 1
        var hi = len

        while (lo <= hi) {
          val mid = (lo + hi) >> 1
          if (as(minByLen(mid)) < as(i)) lo = mid + 1
          else hi = mid - 1
        }

        minByLen(lo) = i
        if (lo > len) len = lo

        i += 1
      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val minByLen = Array.ofDim[Int](n + 1)
  val as = Array.ofDim[Int](n)
  var max = 0

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

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

    var len = 0

    for (i <- 0 until limit) {
      val a = as(i)
      val j = binSearch(0, len - 1, a)
      if (j == len) {
        minByLen(j) = a
        len += 1
      } else if (a < minByLen(j)) minByLen(j) = a
    }

    len
  }

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    for (v <- adj(u))
      if (v != prev) dfs(v, u, depth + 1)

    if (adj(u).length == 1 && depth >= max) max = math.max(max, lis(as, depth + 1))
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val minByLen = Array.ofDim[Int](n + 1)
  val as = Array.ofDim[Int](n)
  var max = 0

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

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

    var i, len = 0

    while (i < limit) {
      val a = as(i)
      val j = binSearch(0, len - 1, a)
      if (j == len) {
        minByLen(j) = a
        len += 1
      } else if (a < minByLen(j)) minByLen(j) = a
      i += 1
    }

    len
  }

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    var i = 0
    while (i < adj(u).length) {
      val v = adj(u)(i)
      if (v != prev) dfs(v, u, depth + 1)
      i += 1
    }

    if (adj(u).length == 1 && depth >= max) max = math.max(max, lis(as, depth + 1))
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val m = Array.ofDim[Int](n + 1)
  val population = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    population(depth) = rs(u)

    var i = 0
    while (i < adj(u).length) {
      val v = adj(u)(i)
      if (v != prev) dfs(v, u, depth + 1)
      i += 1
    }

    if (adj(u).length == 1 && depth >= max) {

      var i, len = 0

      while (i <= depth) {

        var lo = 1
        var hi = len

        while (lo <= hi) {
          val mid = (lo + hi) >> 1
          if (population(m(mid)) < population(i)) lo = mid + 1
          else hi = mid - 1
        }

        m(lo) = i
        if (lo > len) len = lo

        i+= 1
      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

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

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    var i = 0
    while (i < adj(u).length) {
      val v = adj(u)(i)
      if (v != prev) dfs(v, u, depth + 1)
      i += 1
    }

    if (adj(u).length == 1 && depth >= max) {

      var i, len = 0

      while (i <= depth) {

        var lo = 0
        var hi = len - 1

        while (lo <= hi) {
          val mid = (lo + hi) >> 1
          if (as(minByLen(mid)) < as(i)) lo = mid + 1
          else hi = mid - 1
        }

        minByLen(lo) = i
        if (lo == len) len += 1

        i += 1
      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val m = Array.ofDim[Int](n + 1)
  val population = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    population(depth) = rs(u)

    var i = 0
    while (i < adj(u).length) {
      val v = adj(u)(i)
      if (v != prev) dfs(v, u, depth + 1)
      i += 1
    }

    if (adj(u).length == 1 && depth > max) {

      var i, len = 0

      while (i <= depth) {

        var lo = 1
        var hi = len

        while (lo <= hi) {
          val mid = (lo + hi) >> 1
          if (population(m(mid)) < population(i)) lo = mid + 1
          else hi = mid - 1
        }

        m(lo) = i
        if (lo > len) len = lo

        i+= 1
      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val m = Array.ofDim[Int](n + 1)
  val as = Array.ofDim[Int](n)
  var max = 0

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

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

    var len = 0
    for (a <- as) {
      val (_, j) = binSearch(0, len - 1, m, a)
      if (j == len) {
        m(j) = a
        len += 1
      } else if (a < m(j)) m(j) = a
    }

    len
  }

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    as(depth) = rs(u)

    for (v <- adj(u))
      if (v != prev) dfs(v, u, depth + 1)

    if (adj(u).length == 1 && depth >= max) max = math.max(max, lis(as))
  }

  for (u <- 0 until n) if (adj(u).size == 1) dfs(u, -1, 0)

  println(max)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val m = Array.ofDim[Int](n + 1)
  val population = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int, len: Int): Unit = {

    population(depth) = rs(u)

    var lo = 0
    var hi = len

    while (lo <= hi) {
      val mid = (lo + hi) / 2
      if (population(m(mid)) < population(depth)) lo = mid + 1
      else hi = mid - 1
    }

    m(lo) = depth
    if (lo > max) max = lo

    for (v <- adj(u)) if (v != prev) {
      dfs(v, u, depth + 1, lo)
    }
  }

  for (u <- 0 until n) dfs(u, -1, 0, 0)

  println(max + 1)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val m = Array.ofDim[Int](n + 1)
  val population = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int, len: Int): Unit = {
    population(depth) = rs(u)

    var lo = 0
    var hi = len

    while (lo <= hi) {
      val mid = (lo + hi) / 2
      if (population(m(mid)) < population(depth)) lo = mid + 1
      else hi = mid - 1
    }

    m(lo) = depth
    if (lo > max) max = lo

    for (v <- adj(u)) if (v != prev) {
      dfs(v, u, depth + 1, lo max len)
    }
  }

  for (u <- 0 until n) dfs(u, -1, 0, 0)

  println(max + 1)
}

import scala.collection._
import scala.collection.mutable.ArrayBuilder

object P279F 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 rs = readInts(n)

  val adjBuilders = Array.fill(n) { new ArrayBuilder.ofInt }

  for (i <- 1 until n) {
    val Array(u, v) = readInts(2)
    adjBuilders(u - 1) += v - 1
    adjBuilders(v - 1) += u - 1
  }

  val adj = adjBuilders.map(_.result)

  val m = Array.ofDim[Int](n + 1)
  val population = Array.ofDim[Int](n)
  var max = 0

  def dfs(u: Int, prev: Int, depth: Int): Unit = {

    population(depth) = rs(u)

    var noChildren = true
    for (v <- adj(u)) if (v != prev) {
      dfs(v, u, depth + 1)
      noChildren = false
    }

    if (noChildren) {
      var len = 0
      for (i <- 0 until depth) {

        var lo = 1
        var hi = len

        while (lo <= hi) {
          val mid = (lo + hi) / 2
          if (population(m(mid)) < population(i)) lo = mid + 1
          else hi = mid - 1
        }

        m(lo) = i
        if (lo > len) len = lo

      }

      if (len > max) max = len
    }
  }

  for (u <- 0 until n) dfs(u, -1, 0)

  println(max + 1)
}


//package codeforces.contests._1374

object ReadingBookEasy {

  def main(args: Array[String]): Unit = {
    val Array(n, k) = io.StdIn.readLine.split(" ").map(_.toInt)

    val (aliceL, bobL, bothL) = (1 to n).foldLeft((List.empty[Int], List.empty[Int], List.empty[Int])) {
      case ((alice, bob, both), _) =>
        val Array(time, a, b) = io.StdIn.readLine.split(" ").map(_.toInt)
        (
          if (a == 1 && b == 0) time :: alice else alice,
          if (a == 0 && b == 1) time :: bob else bob,
          if (a == 1 && b == 1) time :: both else both
        )
    }

    val aliceA = aliceL.toArray
    val bobA = bobL.toArray
    val bothA = bothL.toArray

    println {
      if (aliceA.length + bothA.length >= k && bobA.length + bothA.length >= k) {

        val aliceP = aliceA.sorted.scanLeft(0)(_ + _)
        val bobP = bobA.sorted.scanLeft(0)(_ + _)
        val bothP = bothA.sorted.scanLeft(0)(_ + _)

        val i = bothP.indices.minBy { i =>
          if (i > k) Int.MaxValue
          else {
            val r = k - i
            if (r < aliceP.length && r < bobP.length)
              aliceP(r) + bobP(r) + bothP(i)
            else Int.MaxValue
          }
        }
        val r = k - i
        aliceP(r) + bobP(r) + bothP(i)
      } else -1
    }
  }
}

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

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new CF653A(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

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

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

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

class CF653A(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
      val n = ni()
      val k = ni()

      val b = Array.ofDim[Books](n)
      REP(n) { i =>
        b(i) = Books(ni(), ni(), ni())
      }

      if(b.count(p => p.a == 1) < k || b.count(p => p.b == 1) < k) {
        out.println("-1")
      } else {
        val c = b.sortBy(f => f.t)
        val lstA = c.filter(p => p.a == 1 && p.b == 0)
        val lstB = c.filter(p => p.a == 0 && p.b == 1)
        val lstC = c.filter(p => p.a == 1 && p.b == 1)

        var cst = 0
        var tk = 0
        var idL = 0
        var idT = 0

        while (tk < k) {
          val g = if(idL < lstA.length && idL < lstB.length) {
            lstA(idL).t + lstB(idL).t
          } else Int.MaxValue

          val h = if(idT < lstC.length) lstC(idT).t else Int.MaxValue
//          out.println(g + " " + h)
          if(g < h) {
            cst += g
            idL+=1
          } else {
            cst += h
            idT += 1
          }
          tk += 1
        }
        out.print(cst)
      }
  }

  case class Books(t: Int, a: Int, b: Int)

}


import scala.io.StdIn._
import scala.collection.mutable._
import scala.math._

object D {
    def findLeft(a: Array[Int])(f: (Int, Int)=> Boolean): Array[Int]= {
        val s = ArrayBuffer[Int]()
        val result = Array.fill[Int](a.size)(0)
        for (i <- 0 until a.size) {
            while (!s.isEmpty && !f(a(s.last), a(i)))
                s.remove(s.size - 1)
            if (s.isEmpty)
                result(i) = -1
            else
                result(i) = s.last
            s += i
        }
        result
    }

  def findRight(a: Array[Int])(f: (Int, Int)=> Boolean): Array[Int] = findLeft(a.reverse)(f).map((x) => a.size - 1 - x).reverse

  def main(args: Array[String]): Unit = {
    val n = readInt()
    val a = readLine().split(" ").map(_.toInt)
    val dp = Array.fill[Int](n)(n + 1)
    dp(0) = 0
    val ll = findLeft(a)(Ordering[Int].lteq)
    val lg = findLeft(a)(Ordering[Int].gteq)
    val rl = findRight(a)(Ordering[Int].lteq)
    val rg = findRight(a)(Ordering[Int].gteq)
    for(i <- 0 until a.size) {
        if(ll(i) != -1)
            dp(i) = min(dp(i), dp(ll(i)) + 1)
        if(lg(i) != -1)
            dp(i) = min(dp(i), dp(lg(i)) + 1)
        if(rl(i) != n)
            dp(rl(i)) = min(dp(rl(i)), dp(i) + 1)
        if(rg(i) != n)
            dp(rg(i)) = min(dp(rg(i)), dp(i) + 1)
    }
    println(dp(n - 1))
  }
}

//package codeforces.contests._1407

object DiscreteCentrifugalJumps {

  /**
   * Monotonic Stack, right view
   *
   * @param seq      elements, randomly accessible
   * @param ordering implicit ordering for gt operator
   * @tparam A type of each element
   * @return left view
   */
  def rightView[A](seq: IndexedSeq[A])(implicit ordering: Ordering[A]): IndexedSeq[Int] = {
    val result = new Array[Int](seq.length)

    seq.indices.foldRight[List[Int]](Nil) { (idx, stack) =>
      val uStack = stack.dropWhile(i => ordering.lt(seq(i), seq(idx)))
      result(idx) = uStack.headOption.getOrElse(-1)
      idx :: uStack
    }

    result
  }

  /**
   * Monotonic Stack, left view
   *
   * @param seq      elements, randomly accessible
   * @param ordering implicit ordering for gt operator
   * @tparam A type of each element
   * @return left view
   */
  def leftView[A](seq: IndexedSeq[A])(implicit ordering: Ordering[A]): IndexedSeq[Int] = {
    val result = new Array[Int](seq.length)

    seq.indices.foldLeft[List[Int]](Nil) { (stack, idx) =>
      val uStack = stack.dropWhile(i => ordering.lt(seq(i), seq(idx)))
      result(idx) = uStack.headOption.getOrElse(-1)
      idx :: uStack
    }

    result
  }

  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readInt()
    val arr = io.StdIn.readLine.split(" ").map(_.toInt)

    val rightIncreasingView: IndexedSeq[Int] = rightView(arr) // check for -1
    val rightDecreasingView: IndexedSeq[Int] = rightView(arr)(Ordering.Int.reverse) // check for -1

    val leftIncreasingView = leftView(arr)
    val leftDecreasingView = leftView(arr)(Ordering.Int.reverse)

    val result = {
      val dp: Array[Int] = (0 until n).toArray
      dp.indices.foreach { i =>

        // relax dp(i) first
        if (leftIncreasingView(i) != -1)
          dp(i) = dp(i) min (dp(leftIncreasingView(i)) + 1)

        if (leftDecreasingView(i) != -1)
          dp(i) = dp(i) min (dp(leftDecreasingView(i)) + 1)

        // relax the future
        if (rightIncreasingView(i) != -1)
          dp(rightIncreasingView(i)) = dp(rightIncreasingView(i)) min (dp(i) + 1)

        if (rightDecreasingView(i) != -1)
          dp(rightDecreasingView(i)) = dp(rightDecreasingView(i)) min (dp(i) + 1)

        if (i + 1 < n)
          dp(i + 1) = dp(i + 1) min (dp(i) + 1)
      }
      dp(n - 1)
    }

    println(result)
  }

}

//package codeforces.contests._1407

object DiscreteCentrifugalJumps {

  /**
   * Monotonic Stack, right view
   *
   * @param seq      elements, randomly accessible
   * @param ordering implicit ordering for gt operator
   * @tparam A type of each element
   * @return left view
   */
  def rightView[A](seq: IndexedSeq[A])(implicit ordering: Ordering[A]): IndexedSeq[Int] = {
    val result = new Array[Int](seq.length)

    seq.indices.foldRight[List[Int]](Nil) { (idx, stack) =>
      val uStack = stack.dropWhile(i => ordering.lt(seq(i), seq(idx)))
      result(idx) = uStack.headOption.getOrElse(-1)
      idx :: uStack
    }

    result
  }

  /**
   * Monotonic Stack, left view
   *
   * @param seq      elements, randomly accessible
   * @param ordering implicit ordering for gt operator
   * @tparam A type of each element
   * @return left view
   */
  def leftView[A](seq: IndexedSeq[A])(implicit ordering: Ordering[A]): IndexedSeq[Int] = {
    val result = new Array[Int](seq.length)

    seq.indices.foldLeft[List[Int]](Nil) { (stack, idx) =>
      val uStack = stack.dropWhile(i => ordering.lt(seq(i), seq(idx)))
      result(idx) = uStack.headOption.getOrElse(-1)
      idx :: uStack
    }

    result
  }

  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readInt()
    val arr = io.StdIn.readLine.split(" ").map(_.toInt)

    val rightIncreasingView: IndexedSeq[Int] = rightView(arr) // check for -1
    val rightMaxView: IndexedSeq[Int] = {
      val view = arr.clone()
      view(n - 1) = n - 1
      (0 to n - 2).foldRight(n - 1) { (i, maxi) =>
        if (arr(i) >= arr(maxi)) {
          view(i) = i
          i
        } else {
          view(i) = maxi
          maxi
        }
      }
      view
    }

    val rightDecreasingView: IndexedSeq[Int] = rightView(arr)(Ordering.Int.reverse) // check for -1
    val rightMinView: IndexedSeq[Int] = {
      val view = arr.clone()
      view(n - 1) = n - 1
      (0 to n - 2).foldRight(n - 1) { (i, mini) =>
        if (arr(i) <= arr(mini)) {
          view(i) = i
          i
        } else {
          view(i) = mini
          mini
        }
      }
      view
    }

    val leftIncreasingView = leftView(arr)
    val leftDecreasingView = leftView(arr)(Ordering.Int.reverse)

    val result = {
      val dp: Array[Int] = (0 until n).toArray
      (0 to n - 2).foreach { i =>

        // relax dp(i) first
        if (i > 0) {
          val h = {
            if (arr(i - 1) < arr(i))
              leftIncreasingView(i)
            else if (arr(i - 1) > arr(i - 1))
              leftDecreasingView(i)
            else -1
          }

          if (h != -1)
            dp(i) = dp(i) min (dp(h) + 1)
        }

        // relax the future
        if (arr(i) > arr(i + 1)) {

          val inc = rightIncreasingView(i)
          val j = {
            if (inc == -1) rightMaxView(i + 1)
            else inc
          }
          dp(j) = dp(j) min (dp(i) + 1)

        } else if (arr(i) < arr(i + 1)) {

          val dec = rightDecreasingView(i)
          val j = {
            if (dec == -1) rightMinView(i + 1)
            else dec
          }
          dp(j) = dp(j) min (dp(i) + 1)

        }

        dp(i + 1) = dp(i + 1) min (dp(i) + 1)
      }
      dp(n - 1)
    }

    println(result)
  }

}

//package codeforces.contests._1407

object DiscreteCentrifugalJumps {

  /**
   * Monotonic Stack, right view
   *
   * @param seq      elements, randomly accessible
   * @param ordering implicit ordering for gt operator
   * @tparam A type of each element
   * @return left view
   */
  def rightView[A](seq: IndexedSeq[A])(implicit ordering: Ordering[A]): IndexedSeq[Int] = {
    val result = new Array[Int](seq.length)

    seq.indices.foldRight[List[Int]](Nil) { (idx, stack) =>
      val uStack = stack.dropWhile(i => ordering.lt(seq(i), seq(idx)))
      result(idx) = uStack.headOption.getOrElse(-1)
      idx :: uStack
    }

    result
  }

  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readInt()
    val arr = io.StdIn.readLine.split(" ").map(_.toInt)

    val rightIncreasingView: IndexedSeq[Int] = rightView(arr) // check for -1
    val rightMaxView: IndexedSeq[Int] = {
      val view = arr.clone()
      view(n - 1) = n - 1
      (0 to n - 2).foldRight(n - 1) { (i, maxi) =>
        if (arr(i) >= arr(maxi)) {
          view(i) = i
          i
        } else {
          view(i) = maxi
          maxi
        }
      }
      view
    }

    val rightDecreasingView: IndexedSeq[Int] = rightView(arr)(Ordering.Int.reverse) // check for -1
    val rightMinView: IndexedSeq[Int] = {
      val view = arr.clone()
      view(n - 1) = n - 1
      (0 to n - 2).foldRight(n - 1) { (i, mini) =>
        if (arr(i) <= arr(mini)) {
          view(i) = i
          i
        } else {
          view(i) = mini
          mini
        }
      }
      view
    }

    val result = {
      val dp: Array[Int] = (0 until n).toArray
      (0 to n - 2).foreach { i =>
        if (arr(i) > arr(i + 1)) {

          val inc = rightIncreasingView(i)
          val j = {
            if (inc == -1) rightMaxView(i + 1)
            else inc
          }
          dp(j) = dp(j) min (dp(i) + 1)

        } else if (arr(i) < arr(i + 1)) {

          val dec = rightDecreasingView(i)
          val j = {
            if (dec == -1) rightMinView(i + 1)
            else dec
          }
          dp(j) = dp(j) min (dp(i) + 1)

        }

        dp(i + 1) = dp(i + 1) min (dp(i) + 1)
      }
      dp(n - 1)
    }

    println(result)
  }

}


/**
 * @author pvasilyev
 * @since 18 Jun 2016
 */
object B625 extends App {

  def checkSubString(a: String, start: Int, b: String): Boolean = {
    if (a.length - start < b.length) {
      return false
    } else {
      for (i <- 0 until b.length) {
        if (a.charAt(start + i) != b.charAt(i)) {
          return false
        }
      }
    }
    true
  }

  def solve() = {
    val gogol: String = scala.io.StdIn.readLine()
    val pineapple: String = scala.io.StdIn.readLine()
    var answer = 0
    var i = 0
    while (i < gogol.length) {
      if (checkSubString(gogol, i, pineapple)) {
        answer += 1
        i += pineapple.length
      } else {
        i += 1
      }
    }
    println(answer)
  }

  solve()

}

import scala.io.Source

object round_342_div2_B {
    def main(args: Array[String]){
        val in = Source.stdin.getLines()
        val now = in.next() + "#"
        val old = in.next()

        println(now.split(old).length-1)
    }
}

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

object _625B extends CodeForcesApp {
  override type Result = Int

  override def solve(read: InputReader) = {
    val (t, s) = (read[String], read[String])

    @tailrec
    def f(i: Int, acc: Int): Int = t.indexOf(s, i) match {
      case p if p >= 0 => f(p + s.length, acc + 1)
      case _ => acc
    }

    f(0, 0)
  }

  override def format(result: Result) = result.toString
}
/****************************[Ignore Template Below]****************************************/
abstract class CodeForcesApp {
  val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  type Result
  def solve(scanner: InputReader): Result
  def format(result: Result): String
  def apply(scanner: InputReader): String = format(solve(scanner))
  def main(args: Array[String]): Unit = println(apply(new InputReader(System.in)))
}
/*********************************[Scala Scanner]*******************************************/
import java.io.{BufferedReader, Reader, InputStreamReader, StringReader, InputStream}
import java.util.StringTokenizer

class InputReader(reader: BufferedReader) extends Iterator[String] with AutoCloseable {
  def this(reader: Reader) = this(new BufferedReader(reader))
  def this(inputStream: InputStream) = this(new InputStreamReader(inputStream))
  def this(str: String) = this(new StringReader(str))

  private[this] val tokenizers = Iterator.continually(reader.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered

  @inline private[this] def tokenizer(): Option[StringTokenizer] = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    if (tokenizers.nonEmpty) Some(tokenizers.head) else None
  }

  def apply[A: Parser]: A = implicitly[Parser[A]].apply(next())

  def tillEndOfLine(): String = tokenizer().get.nextToken("\n\r")
  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty
  override def close() = reader.close()
}

trait Parser[A] {
  def apply(s: String): A
}
object Parser {
  def apply[A](f: String => A) = new Parser[A] {
    override def apply(s: String) = f(s)
  }
  implicit val stringParser: Parser[String] = Parser(identity)
  implicit val charParser: Parser[Char] = Parser(s => s.ensuring(_.length == 1, s"Expected Char; found $s").head)
  implicit val booleanParser: Parser[Boolean] = Parser(_.toBoolean)
  implicit val intParser: Parser[Int] = Parser(_.toInt)
  implicit val longParser: Parser[Long] = Parser(_.toLong)
  implicit val bigIntParser: Parser[BigInt] = Parser(BigInt(_))
  implicit val doubleParser: Parser[Double] = Parser(_.toDouble)
  implicit val bigDecimalParser: Parser[BigDecimal] = Parser(BigDecimal(_))
}

/**
  * Created by Tsibin.
  */
object Problemset625 {
  def main(args: Array[String]): Unit = {
    val problem = new ProblemB
    problem.solve()
  }

  private abstract class Problem {
    val stdIn = scala.io.StdIn
    def solve()
  }

  private class ProblemA extends Problem {
    override def solve(): Unit = {
      val Array(n, a, b, c) = Iterator.continually(stdIn.readLine())
        .takeWhile(_ != null)
        .map(BigInt(_))
        .toArray
      var count = BigInt(0)

      if (n < a && n < b) {
        println(0)
        return
      }
      if (a >= b - c && n >= b) {
        count = (n - c) / (b - c)
        val remains = n - (count * (b - c))
        if (remains > 0)
          count += remains / a
      }
      else
        count = n / a
      println(count)
    }
  }

  private class ProblemB extends Problem {
    override def solve() : Unit = {
      val Array(gogolII, pineappleTel) = Iterator.continually(stdIn.readLine())
        .takeWhile(_ != null)
        .toArray
      println(pineappleTel.r.findAllIn(gogolII).length)
    }
  }
}

/**
  * Created by octavian on 07/02/2016.
  */
object War {

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

    //System.setIn(new FileInputStream("./src/war.in"))
    //System.setOut(new PrintStream(new FileOutputStream("./src/war.out")))

    val a: Array[Char] = readLine().toCharArray
    val b: Array[Char] = readLine().toCharArray
    var matches: Vector[Int] = Vector.empty[Int]
    for(i <- 0 until a.length) {
      if(a.slice(i, i + b.length).deep == b.deep) {
        matches :+= i
      }
    }

    var lastPos = -1
    var result = 0
    for(i <- 0 until matches.length) {
      if(lastPos < matches(i)) {
        lastPos = matches(i) + b.length - 1
        result += 1
      }
    }
    println(result)
  }

}

object A {
  def main(args: Array[String]) {
    val phone = scala.io.StdIn.readLine
    val ai = scala.io.StdIn.readLine
    val counts = phone.replaceAll(ai, "#").toCharArray.groupBy(l => l)
    if (counts.isDefinedAt('#')) {
      println(counts('#').length)
    } else {
      println(0)
    }

  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val str1 = in.next()
  val str2 = in.next()
  val replace = str2.init + '#'
  println(str1.replaceAll(str2, replace).count(_ == '#'))
}

object Main {
  import scala.annotation.tailrec
  import java.io.{BufferedReader, PrintWriter, PrintStream, PushbackReader}
  class MyConsole(val in: BufferedReader, val _out: PrintStream,
                  val err: PrintStream) {
    // PrintWriter do not flush automatically
    val out = new PrintWriter(_out,false)
    // If argument is null, there are ambiguous which function will be called
    def print(obj: Any)                  = out.print(if(obj == null) "null" else obj.toString)
    def println()                        = out.println()
    def println(obj: Any)                = out.println(obj)
    def printf(text: String, args: Any*) = out.printf(text.format(args : _*))
    // NOTE: YOU MUST FLUSH BEFORE END OF MAIN
    def flush()                          = out.flush()
    def debugln(obj: Any)                = err.println("\u001b[36m" + obj + "\u001b[0m")

    def readIntVector()    : Vector[Int]    = parseLineToVector(() => nextInt)
    def readLongVector()   : Vector[Long]   = parseLineToVector(() => nextLong)
    def readStringVector() : Vector[String] = parseLineToVector(() => nextString)
    def nextInt()          : Int            = nextLong().toInt
    def nextBigInt()       : BigInt         = BigInt(nextString())
    def nextBigDecimal(  ) : BigDecimal     = BigDecimal(nextString())
    def nextDouble()       : Double         = nextString().toDouble
    def nextLong() : Long = {
      if(!goNextValuable())
        throw new NoSuchElementException("Reading long failed")
      val sgn = if(peek == '-') -1l else 1
      if(sgn == -1l) ignore(read())
      if(peek < '0' || '9' < peek)
        throw new NumberFormatException(s"readLong found only '-' or no number")
      @tailrec
      def readLong(next: Int, cur: Long) : Long =
        if('0' <= next && next <= '9'){
          ignore(read()) // is equal to next
          readLong(peek, cur*10 + next-'0')
        }
        else if(isEnd(next) || isSpaceOrControl(next))
          sgn*cur
        else
          throw new NumberFormatException(s"readLong found strange byte $next")
      val res = readLong(peek,0)
      skipTrailingSpaces()
      res
    }
    def nextString() : String = {
      if(!goNextValuable())
        throw new NoSuchElementException("Reading String failed")
      val builder = new StringBuilder
      @tailrec
      def appendCode(next: Int) : String = {
        if(isEnd(next) || isSpaceOrControl(next)){
          builder.toString
        }else{
          builder.append(read().toChar) // here we skip.
            appendCode(peek)
        }
      }
      val res = appendCode(peek)
      skipTrailingSpaces()
      res
    }
    // TODO: refactoring to marge nextString
    def readLine() : String = {
      if(isEnd(peek))
        throw new NoSuchElementException("Reading Line failed")
      skipNewline()
      val builder = new StringBuilder
      @tailrec
      def appendCode(next: Int) : String = {
        if(isEnd(next) || isNewLine(next)){
          builder.toString
        }else{
          builder.append(next.toChar)
          appendCode(read())
        }
      }
      appendCode(read())
    }
    // helpers
    private def ignore[T](x: => T) : Unit = { x; () }
    private[this] var peeked: Option[Int] = None
    private[this] var is_first = true
    protected def read() = {
      val res = peeked match {
        case None    => in.read()
        case Some(a) => { peeked = None; a }
      }
      is_first = false
      res
    }
    private def peek() =
      peeked match {
        case None    => {
          val res = in.read()
          peeked  = Some(res)
          res
        }
        case Some(a) => a
      }
    private def isEnd(c: Int)     = c == -1
    private def isCR(c: Int)      = c == 13
    private def isLF(c: Int)      = c == 10
    private def isNewLine(c: Int) = isLF(c) || isCR(c)
    private def isThereReadable() = !isEnd(peek)
    private def isSpaceOrControl(c: Int) = (0 <= c && c <= 32) || c == 127
    @tailrec
    final private def goNextNotSpaceNorControl() : Unit =
      if(isSpaceOrControl(peek)){
        ignore(read())
        goNextNotSpaceNorControl()
      }
    final private def skipTrailingSpaces() : Unit = {
      @tailrec
      def skipTrailingSpacesAux() : Unit = {
        if(!isNewLine(peek) && isSpaceOrControl(peek)){
          ignore(read())
          skipTrailingSpacesAux()
        }
      }
      skipTrailingSpacesAux()
    }
    final private def skipNewline() : Unit =
      if(!is_first){
        if(isCR(peek))
          ignore(read())
        if(isLF(peek))
          ignore(read())
      }
    private def goNextValuable() : Boolean = {
      goNextNotSpaceNorControl()
      isThereReadable()
    }

    private def parseLineToVector[X](parser: () => X) : Vector[X] = {
      import scala.collection.immutable.VectorBuilder
      val vb = new VectorBuilder[X]()
      skipNewline()
      @tailrec
      def parseLineToVectorAux() : Vector[X] =
        if(isNewLine(peek) || isEnd(peek)) {
          vb.result
        }else{
          vb += parser()
          parseLineToVectorAux()
        }
      parseLineToVectorAux()
    }
  }
  def main(args: Array[String]) : Unit = {
    val console = new MyConsole(Console.in, Console.out, Console.err)
    val solver  = new Solver(console)
    solver.main()
    console.flush()
  }
  class Solver(val stdio: MyConsole){
    import stdio._ // shadow Console.~
      @tailrec
    final def solve(s: String, t: String, i : Int, a: Int) : Int = {
      val k = s.indexOf(t, i)
      if(k == -1)
        a
      else
        solve(s, t, k + t.length, a+1)
    }
    def main() : Unit = {
      val s = readLine()
      val t = readLine()
      println(solve(s, t, 0, 0))
    }
  }
}

// object Main
import java.io.{IOException, InputStream, PrintWriter}
import java.util.InputMismatchException

object Main {

    def solveCase(caseNo: Int, in: InputReader, out: PrintWriter): Unit = {
        val a = in.next().toCharArray
        val b = in.next()
        var answer = 0
        for (i <- 0 until a.length) {
            if (b.zipWithIndex.forall{p => i + p._2 < a.length && p._1 == a(i + p._2)}) {
                answer += 1
                a(i + b.length - 1) = '#'
            }
        }
        out.println(answer)
    }

    def solve(in: InputReader, out: PrintWriter): Unit = {
        solveCase(1, in, out)
//        val caseCount = in.nextInt()
//        for (caseNo <- 1 to caseCount) {
//            solveCase(caseNo, in, out)
//        }
    }

    def main(args: Array[String]) {
        val in = new InputReader(System.in)
        val out = new PrintWriter(System.out)

        solve(in, out)

        in.close()
        out.close()
    }
}

class InputReader(val inputStream: InputStream) {
    val buf = new Array[Byte](1 << 16)
    var curChar = 0
    var numChars = 0

    def read(): Int = {
        if (numChars == -1) {
            throw new InputMismatchException()
        }
        if (curChar >= numChars) {
            curChar = 0
            try {
                numChars = inputStream.read(buf)
            } catch {
                case _: IOException => throw new InputMismatchException()
            }
            if (numChars <= 0)
                return -1
        }
        curChar += 1
        buf(curChar - 1)
    }

    def peek(): Int = {
        if (numChars == -1) {
            return -1
        }
        if (curChar >= numChars) {
            curChar = 0
            try {
                numChars = inputStream.read(buf)
            } catch {
                case _: IOException => return -1
            }
            if (numChars <= 0)
                return -1
        }
        buf(curChar)
    }

    def isSpaceChar(c: Int): Boolean = c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1

    def nextInt(): Int = {
        var c = read()
        while (isSpaceChar(c)) c = read()
        var sgn = 1
        if (c == '-') {
            sgn = -1
            c = read()
        }
        var res = 0
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException()
            res *= 10
            res += c & 15
            c = read()
        } while (!isSpaceChar(c))
        res * sgn
    }

    def nextLong(): Long = {
        var c = read()
        while (isSpaceChar(c)) c = read()
        var sgn = 1
        if (c == '-') {
            sgn = -1
            c = read()
        }
        var res = 0l
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException()
            res *= 10
            res += c & 15
            c = read()
        } while (!isSpaceChar(c))
        res * sgn
    }

    def next(): String = {
        var c = read()
        while (isSpaceChar(c)) c = read()
        val res = new StringBuilder()
        do {
            res += c.toChar
            c = read()
        } while (!isSpaceChar(c))
        res.toString()
    }

    def nextChar(): Char = {
        var c = read()
        while (isSpaceChar(c)) c = read()
        c.toChar
    }

    //noinspection AccessorLikeMethodIsEmptyParen
    def hasNext(): Boolean = {
        var value = peek()
        do {
            read()
            value = peek()
        } while (isSpaceChar(value) && value != -1)
        value != -1
    }

    def rawNextLine(): String = {
        val builder = new StringBuilder
        var c = read()
        while (c != '\n' && c != -1) {
            if (c != '\r')
                builder += c.toChar
            c = read()
        }
        builder.toString()
    }

    def nextLine(): String = {
        var s = rawNextLine()
        while (s.trim().length == 0) s = rawNextLine()
        s
    }

    def nextDouble(): Double = {
        next().toDouble
    }

    def close(): Unit = {
        inputStream.close()
    }
}

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

import scala.annotation.implicitNotFound
import scala.reflect.ClassTag

/**
  * Created by nimas on 2/4/16.
  */
object B extends App{

  class Template(val delim: String = " ") {

    val in = new BufferedReader(new InputStreamReader(System.in))
    val out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)))
    val it = Iterator.iterate(new StringTokenizer(""))(tok => if (tok.hasMoreTokens) tok else new StringTokenizer(in.readLine(), delim))

    def nextTs[T: ParseAble](n: Int)(delim: String = " ")(implicit ev: ClassTag[T]) = (Array fill n) {
      nextT[T](delim)
    }

    def nextT[T](delim: String = " ")(implicit parser: ParseAble[T]) = {
      val x = it.find(_.hasMoreTokens)
      parser parse (x get)
    }

    def close(): Unit = {
      in.close()
      out.close()
    }

    @implicitNotFound("No member of type class ParseAble in scope for ${T}")
    trait ParseAble[T] {
      def parse(strTok: StringTokenizer): T
    }

    object ParseAble {

      implicit object ParseAbleString extends ParseAble[String] {
        override def parse(strTok: StringTokenizer): String = strTok nextToken()
      }

      implicit object ParseAbleInt extends ParseAble[Int] {
        override def parse(strTok: StringTokenizer): Int = strTok nextToken() toInt
      }

      implicit object ParseAbleLong extends ParseAble[Long] {
        override def parse(strTok: StringTokenizer): Long = strTok nextToken() toLong
      }

      implicit object ParseAbleDouble extends ParseAble[Double] {
        override def parse(strTok: StringTokenizer): Double = strTok nextToken() toDouble
      }

      implicit object ParseAbleBigInt extends ParseAble[BigInt] {
        override def parse(strTok: StringTokenizer): BigInt = BigInt(strTok nextToken())
      }

    }

  }

  val io = new Template()

  import io._

  val ai = nextT[String]()
  val apple = nextT[String]()

  val s1 = apple.r findAllMatchIn ai

  out println (s1 size)

  close()

}

/**
 * @author pvasilyev
 * @since 18 Jun 2016
 */
object B625 extends App {

  def solve() = {
    val gogol: String = scala.io.StdIn.readLine()
    val pineapple: String = scala.io.StdIn.readLine()
    var answer = 0
    var j = 0
    for (i <- 0 until gogol.length) {
      if (gogol.charAt(i) == pineapple.charAt(j)) {
        j += 1
      }
      if (j == pineapple.length) {
        j = 0
        answer += 1
      }
    }
    println(answer)
  }

  solve()

}

/**
 * @author pvasilyev
 * @since 18 Jun 2016
 */
object B625 extends App {

  def checkSubString(a: String, start: Int, b: String): Boolean = {
    if (a.length - start < b.length) {
      return false
    } else {
      for (i <- 0 until b.length) {
        if (a.charAt(start + i) != b.charAt(i)) {
          return false
        }
      }
    }
    true
  }

  def solve() = {
    val gogol: String = scala.io.StdIn.readLine()
    val pineapple: String = scala.io.StdIn.readLine()
    var answer = 0
    var j = 0
    for (i <- 0 until gogol.length) {
      if (checkSubString(gogol, i, pineapple)) {
        answer += 1
      }
    }
    println(answer)
  }

  solve()

}

/**
 * @author pvasilyev
 * @since 18 Jun 2016
 */
object B625 extends App {

  def solve() = {
    val gogol: String = scala.io.StdIn.readLine()
    val pineapple: String = scala.io.StdIn.readLine()
    var answer = 0
    var j = 0
    for (i <- 0 until gogol.length) {
      if (gogol.charAt(i) == pineapple.charAt(j)) {
        j += 1
      } else if (gogol.charAt(i) == pineapple.charAt(0)) {
        j = 1
      } else {
        j = 0
      }
      if (j == pineapple.length) {
        j = 0
        answer += 1
      }
    }
    println(answer)
  }

  solve()

}


import scala.collection.mutable.HashSet

object B368 {
  @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(n, m) = readInts(2)
    val input = readInts(n)
    val output = new Array[Int](n)
    val set = HashSet.empty[Int]
    for(i <- n-1 to 0 by -1) {
      set += input(i)
      output(i) = set.size
    }
    for(_ <- 0 until m) {
      val Array(l) = readInts(1)
      println(output(l-1))
    }
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, m) = in.next().split(" ").map(_.toInt)
  val data = in.next().split(" ").map(_.toInt)
  var set = Set.empty[Int]
  val result = data.reverse.map { el =>
    set += el
    set.size
  }.reverse
  print(Range(0, m).map(_ => result(in.next().toInt - 1)).mkString("\n"))
}


import java.util
import java.util.stream.Collectors

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = new Array[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(util.Arrays.stream(res)
      .mapToObj(_.toString)
      .collect(
        Collectors
          .joining(" ")))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = { val a = new Array[Int](n); java.util.Arrays.setAll(a, _ => nextInt); a }
    def readLongs(n: Int) = { val a = new Array[Long](n); java.util.Arrays.setAll(a, _ => nextLong); a }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = Array.ofDim[Long](n)

  for (i <- xs.indices) res(2 * i + 1) = xs(i)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  for (i <- xs.indices) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    acc = xx + c
  }

  if (!bad) {
    out.println("Yes")
    out.println(res.mkString(" "))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = Array.fill(n) { nextInt }
    def readLongs(n: Int) = Array.fill(n) { nextLong }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = new Array[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).asInstanceOf[Long]
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(res.mkString(" "))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = Array.fill(n) { val a = new Array[Int](n); java.util.Arrays.setAll(a, _ => nextInt); a }
    def readLongs(n: Int) = Array.fill(n) { nextLong }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = Array.ofDim[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(res.mkString(" "))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = Array.fill(n) { nextInt }
    def readLongs(n: Int) = Array.fill(n) { nextLong }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object E extends App {

  @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)) }

  val Array(n) = readInts(1)
  val xs = readLongs(n / 2)

  val res = Array.ofDim[Long](n)

  for (i <- xs.indices) res(2 * i + 1) = xs(i)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  for (i <- xs.indices) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    acc = xx + c
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (!bad) {
    println("Yes")
    println(res.mkString(" "))
  } else println("No")

  Console.flush
}

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = Array.ofDim[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(res.mkString(" "))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = Array.fill(n) { val a = new Array[Int](n); java.util.Arrays.setAll(a, _ => nextInt); a }
    def readLongs(n: Int) = Array.fill(n) { nextLong }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = new Array[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(res.mkString(" "))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = Array.fill(n) { val a = new Array[Int](n); java.util.Arrays.setAll(a, _ => nextInt); a }
    def readLongs(n: Int) = Array.fill(n) { nextLong }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

import java.util
import java.util.stream.Collectors

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = new Array[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).asInstanceOf[Long]
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(util.Arrays.stream(res)
      .mapToObj(_.toString)
      .collect(
        Collectors
          .joining(" ")))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = { val a = new Array[Int](n); java.util.Arrays.setAll(a, _ => nextInt); a }
    def readLongs(n: Int) = { val a = new Array[Long](n); java.util.Arrays.setAll(a, _ => nextLong); a }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object E {

  import InOut._
  def main(args: Array[String]): Unit = {}

  val n = nextInt
  val xs = readLongs(n / 2)
  val res = new Array[Long](n)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).asInstanceOf[Long]
    r * r == x
  }

  val LIM = 10000000000000L
  var acc = 0L

  var x = 1L
  var bad = false
  var i = 0
  while (i < xs.length) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (!bad && !isSquare(xx + c)) {
      x += 1
      xx = x * x
      if (xx - acc > LIM) bad = true
    }
    res(2 * i) = xx - acc
    res(2 * i + 1) = xs(i)
    acc = xx + c
    i += 1
  }

  if (!bad) {
    out.println("Yes")
    out.println(res.mkString(" "))
  } else out.println("No")

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def readInts(n: Int) = { val a = new Array[Int](n); java.util.Arrays.setAll(a, _ => nextInt); a }
    def readLongs(n: Int) = { val a = new Array[Long](n); java.util.Arrays.setAll(a, _ => nextLong); a }
    def readBigs(n: Int) = Array.fill(n) {nextBig }
  }
}

object Main {

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

  import IO._

  val n = nextInt
  val (a, b) = n % 4 match {
    case 0 => (1, 'A')
    case 1 => (0, 'A')
    case 2 => (1, 'B')
    case 3 => (2, 'A')
  }
  out.println(s"$a $b")
  out.flush()

  object IO {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def readInts(n: Int) = Array.fill(n)(Integer.parseInt(nextToken))
  }

}

object E extends App {

  @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)) }

  val Array(n) = readInts(1)
  val xs = readLongs(n / 2)

  val res = Array.ofDim[Long](n)

  for (i <- xs.indices) res(2 * i + 1) = xs(i)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 3162277L
  var acc = 0L

  var x = 1
  for (i <- xs.indices) {
    val c = xs(i)
    while (x * x <= acc) {
      x += 1
    }
    var xx = x * x
    while (x <= LIM && !isSquare(xx + c)) {
      x += 1
      xx = x * x
    }
    res(2 * i) = xx - acc
    acc = xx + c
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (x <= LIM) {
    println("Yes")
    println(res.mkString(" "))
  } else println("No")

  Console.flush
}

object E extends App {

  @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)) }

  val Array(n) = readInts(1)
  val xs = readLongs(n / 2)

  val res = Array.ofDim[Long](n)

  for (i <- xs.indices) res(2 * i + 1) = xs(i)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 3162277L
  var acc = 0L

  var x = 1
  for (i <- xs.indices) {
    val c = xs(i)
    var xx = x * x
    while (x <= LIM && (!isSquare(acc + xx) || !isSquare(acc + xx + c))) {
      x += 1
      xx = x * x
    }
    acc += xx
    acc += c
    res(2 * i) = xx
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (x <= LIM) {
    println("Yes")
    println(res.mkString(" "))
  } else println("No")

  Console.flush
}

object E extends App {

  @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)) }

  val Array(n) = readInts(1)
  val xs = readLongs(n / 2)

  val res = Array.ofDim[Long](n)

  for (i <- xs.indices) res(2 * i + 1) = xs(i)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 3162280L

  var x = 1L
  val b = xs.head
  while (!isSquare(b + x * x) && x < LIM) {
    x += 1
  }
  res(0) = x * x
  var acc = x * x + b

  for (i <- 1 until xs.length) {
    val c = xs(i)
    var xx = x * x
    while (x < LIM && (!isSquare(acc + xx) || !isSquare(acc + xx + c))) {
      x += 1
      xx = x * x
    }
    acc += xx
    acc += c
    res(2 * i) = xx
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (x < LIM) {
    println("Yes")
    println(res.mkString(" "))
  } else println("No")

  Console.flush
}

object E extends App {

  @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)) }

  val Array(n) = readInts(1)
  val xs = readLongs(n / 2)

  val res = Array.ofDim[Long](n)

  for (i <- xs.indices) res(2 * i + 1) = xs(i)

  def isSquare(x: Long): Boolean = {
    val r = Math.sqrt(x).toLong
    r * r == x
  }

  val LIM = 3162277L
  var acc = 0L

  var x = 1
  for (i <- xs.indices) {
    val c = xs(i)
    var xx = x * x
    while (x < LIM && (!isSquare(acc + xx) || !isSquare(acc + xx + c))) {
      x += 1
      xx = x * x
    }
    acc += xx
    acc += c
    res(2 * i) = xx
  }

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  if (x < LIM) {
    println("Yes")
    println(res.mkString(" "))
  } else println("No")

  Console.flush
}


object Main2 extends App {

import java.io._
import java.util
import java.util.{Locale, StringTokenizer, TreeSet}

import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.io.StdIn

  val in = new Reader()
  val out: PrintWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)))

  val MOD = 1000000000 + 7
  val eps = 0.00000001

  val n = in.nextInt

  var points = Vector[(Long, Int)]()

  for (_ <- 0 until n) {
    val l = in.nextLong
    val r = in.nextLong
    points = points :+ (l, 0)
    points = points :+ (r, 1)
  }

  points = points.sorted

  var ans = Map[Int, Long]()
  for (i <- 0 to n) ans += i -> 0

  var cnt = 0
  var prev = (0l, 0)
  for (point <- points) {

    if (prev._1 != point._1)
      ans += cnt -> (ans(cnt) + point._1 - prev._1 - 1)

    if (prev._1 != point._1 && prev._2 == 0)
      ans += cnt -> (ans(cnt) + 1)

    if (point._2 == 0) cnt += 1
    else {
      if (prev._1 == point._1 && prev._2 != point._2)
        ans += cnt -> (ans(cnt) + 1)

      if (prev._1 != point._1)
        ans += cnt -> (ans(cnt) + 1)

        cnt -= 1
    }

    prev = point
  }

  for (i <- 1 to n) out.print(ans(i)+" ")

  out.flush()
  out.close()

  class Reader @throws[IOException]
  () {
    var br: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
    var tok: StringTokenizer = null

    def this(file: String) {
      this()
      br = new BufferedReader(new FileReader(file))
    }

    @throws[IOException]
    def next: String = {
      while (tok == null || !tok.hasMoreElements) tok = new StringTokenizer(br.readLine)
      tok.nextToken
    }

    @throws[NumberFormatException]
    @throws[IOException]
    def nextInt: Int = Integer.valueOf(next)

    @throws[NumberFormatException]
    @throws[IOException]
    def nextLong: Long = next.toLong

    @throws[NumberFormatException]
    @throws[IOException]
    def nextDouble: Double = next.toDouble

    @throws[IOException]
    def nextLine: String = br.readLine
  }

}

object Main2 extends App {

import java.io._
import java.util
import java.util.{Locale, StringTokenizer, TreeSet}

import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.io.StdIn

  val in = new Reader()
  val out: PrintWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)))

  val MOD = 1000000000 + 7
  val eps = 0.00000001

  val n = in.nextInt

  var points = new Array[(Long, Int)](2*n)

  for (i <- 0 until n) {
    val l = in.nextLong
    val r = in.nextLong
    points(i*2) =  (l, 0)
    points(i*2+1) =  (r, 1)
  }

  points = points.sorted

  var ans = Map[Int, Long]()
  for (i <- 0 to n) ans += i -> 0

  var cnt = 0
  var prev = (0l, 0)
  for (point <- points) {

    if (prev._1 != point._1)
      ans += cnt -> (ans(cnt) + point._1 - prev._1 - 1)

    if (prev._1 != point._1 && prev._2 == 0)
      ans += cnt -> (ans(cnt) + 1)

    if (point._2 == 0) cnt += 1
    else {
      if (prev._1 == point._1 && prev._2 != point._2)
        ans += cnt -> (ans(cnt) + 1)

      if (prev._1 != point._1)
        ans += cnt -> (ans(cnt) + 1)

        cnt -= 1
    }

    prev = point
  }

  for (i <- 1 to n) out.print(ans(i)+" ")

  out.flush()
  out.close()

  class Reader @throws[IOException]
  () {
    var br: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
    var tok: StringTokenizer = null

    def this(file: String) {
      this()
      br = new BufferedReader(new FileReader(file))
    }

    @throws[IOException]
    def next: String = {
      while (tok == null || !tok.hasMoreElements) tok = new StringTokenizer(br.readLine)
      tok.nextToken
    }

    @throws[NumberFormatException]
    @throws[IOException]
    def nextInt: Int = Integer.valueOf(next)

    @throws[NumberFormatException]
    @throws[IOException]
    def nextLong: Long = next.toLong

    @throws[NumberFormatException]
    @throws[IOException]
    def nextDouble: Double = next.toDouble

    @throws[IOException]
    def nextLine: String = br.readLine
  }

}


object CF1325B extends App{
  val sc = new java.util.Scanner(System.in)
  val t = sc.nextInt
  for (i <- 1 to t) {
    val n = sc.nextInt
    sc.nextLine()
    val nx = sc.nextLine().split(" ").toSet
    println(Math.min(n, nx.size))
  }
}

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

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new C628B(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

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

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

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

class C628B(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val t = ni()
    REP(t) { _ =>
      val n = ni()
      val a = na(n).toIndexedSeq.sorted
      var count = 1
      REP(n) { i =>
        if(i > 0 && a(i-1) != a(i)) count+=1
      }
      out.println(count)
    }
  }
}

object Main extends App {
    (0 until readInt()).map(_ => {
        readLine()
        readLine().split(" ").map(n => n.toInt).toSet.size
    }).foreach(n => println(s"$n"))
}

object Main {

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

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  def main(args: Array[String]): Unit = {
    solve(System.in,System.out)
  }
  def solve(inputStream: InputStream, out: PrintStream): Unit = {
    val in = new InputReader(inputStream)
    val t = in.nextInt()
    (1 to t).foreach{ _ =>
      val n = in.nextInt()
      val a = (1 to n).map(_ => in.nextInt())
      out.println(a.distinct.length)
    }
  }
  class InputReader(stream: InputStream){
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(stream),32768)
    var tokenizer: StringTokenizer = _
    def next(): String = {
      while(tokenizer == null || !tokenizer.hasMoreTokens){
        try{
          tokenizer = new StringTokenizer(reader.readLine())
        }catch {
          case e: IOException => throw new RuntimeException(e)
        }
      }
      tokenizer.nextToken()
    }
    def nextInt(): Int =  next().toInt
    def nextLong(): Long =  next().toLong
  }
}

import java.io.PrintWriter

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

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

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

    def GCD(a: Int, b: Int): Int = {
        if (a == 0) return b
        GCD(b % a, a)
    }

    def solve(): Unit = {
        val n = readInt()
        val a = readArrayInt()
        println(a.distinct.length)

    }

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


object Main {

  def main(args: Array[String]) = {
    val input = scala.io.Source.stdin.getLines()
    val n = input.next().toInt
    var ns = input.next().split(" ").map(_.toInt).toList
    var s = 0
    var e = ns.length
    var l = List.empty[Int]
    while (ns.nonEmpty) {
      if (ns.head > ns.last) {
        l = ns.head :: l
        ns = ns.drop(1)
      } else {
        l = ns.last :: l
        ns = ns.dropRight(1)
      }
    }
    val as = l.reverse.grouped(2).map(a => if (a.size == 2) (a(0), a(1)) else (a(0), 0)).toList.unzip
    print(as._1.sum + " " + as._2.sum)
  }
}

import java.util.StringTokenizer

object _381A extends App {
  var token = new StringTokenizer("")
  def next = {
    while (!token.hasMoreTokens()) token = new StringTokenizer(readLine)
    token.nextToken()
  }
  val n = next.toInt
  val a = (1 to n).map(i => next.toInt).toArray
  def f(l: Int, r: Int, which: Int, one: Int, two: Int): (Int, Int) =
    if (l > r) (one, two)
    else if (which == 0) {
      if (a(l) > a(r)) f(l + 1, r, 1 - which, one + a(l), two)
      else f(l, r - 1, 1 - which, one + a(r), two)
    } else {
      if (a(l) > a(r)) f(l + 1, r, 1 - which, one, two + a(l))
      else f(l, r - 1, 1 - which, one, two + a(r))
    }
  val ans = f(0, n - 1, 0, 0, 0)
  println(ans._1 + " " + ans._2)
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = in.next().split(" ").map(_.toInt)
  var ser = 0
  var dim = 0
  var left = 0
  var right = data.length - 1
  var dh = false
  while (right >= left) {
    var score = Math.max(data(left), data(right))
    if (data(left) >= data(right))
      left += 1
    else
      right -= 1

    if (dh)
      dim += score
    else
      ser += score
    dh = !dh
  }
  println(ser + " " + dim)
}

import java.util.Scanner

object P381A {
    def main(args: Array[String]) {
        val scanner = new Scanner(System.in)
        val n = scanner.nextInt()
        val cards = for (i <- 0 until n) yield scanner.nextInt()
        val ans = emulate(cards.toList)
        println(ans._1 + " " + ans._2)
    }

    def emulate(list: List[Int]): (Int, Int) = {
        if (list.isEmpty) (0, 0)
        else if (list.head > list.last) {
            val points = emulate(list.tail).swap
            (list.head + points._1, points._2)
        }
        else {
            val points = emulate(list.init).swap
            (list.last + points._1, points._2)
        }
    }
}

import scala.util.Sorting
object Solve{
    def main(args: Array[String]): Unit = {

        val n = readInt()
        val arr = readLine().split(" ").map(_.toInt)

        var dimaSum = 0
        var serejaSum = 0
        var left = 0
        var right = arr.length - 1
        var sTurn = true
        var temp = 0
        while(left <= right){
             if(arr(left) > arr(right))
                 { temp = arr(left); left += 1; }
             else
                { temp = arr(right); right -= 1; }

            if(sTurn)
                serejaSum += temp
            else
                dimaSum += temp

            sTurn = !sTurn
        }

        println(serejaSum + " " + dimaSum)

    }
}

object A381 {

  import IO._

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    var in = readInts(n)
    var ser = 0
    var dima = 0
    while(in.nonEmpty){
      if(in.head > in.last) {
        ser += in.head
        in = in.drop(1)
      } else {
        ser += in.last
        in = in.dropRight(1)
      }
      if(in.nonEmpty) {
        if(in.head > in.last) {
          dima += in.head
          in = in.drop(1)
        } else {
          dima += in.last
          in = in.dropRight(1)
        }
      }

    }
    println(s"$ser $dima")
  }

  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 P381A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)
  val N = sc.nextInt

  type Cards = List[Int]
  val CS: Cards = List.fill(N)(sc.nextInt)

  def solve(): Unit = {

    @tailrec
    def loop(spt: Int, dpt: Int, turn: Int, cs: Cards, rcs: Cards): Unit = {
      if (turn == N) out.println(List(spt, dpt).mkString(" "))
      else (cs, rcs) match {
        case (x :: xs, y :: ys) if x > y => if (turn % 2 == 0) loop(spt + x, dpt, turn + 1, xs, rcs)
                                            else loop(spt, dpt + x, turn + 1, xs, rcs)
        case (_, y :: ys) => if (turn % 2 == 0) loop(spt + y, dpt, turn + 1, cs, ys)
                             else loop(spt, dpt + y, turn + 1, cs, ys)
      }
    }

    loop(0, 0, 0, CS, CS.reverse)
  }

  solve
  out.close
}

import java.util.Scanner

object CF381A {

    def main(args: Array[String]) {
        val in = new Scanner(System.in)
        val n = in.nextInt()
        val cards = for(i <- 0 until n) yield in.nextInt()
        val answers = gao(cards.toList)
        println(answers._1 + " " + answers._2)
    }

    def gao(list: List[Int]): (Int, Int) = {
        if (list.isEmpty) (0, 0)
        else if (list.head > list.last) {
            val scores = gao(list.tail).swap
            (list.head + scores._1, scores._2)
        } else {
            val scores = gao(list.init).swap
            (list.last + scores._1, scores._2)
        }
    }
}

import scala.util.Sorting
object Solve{
    def main(args: Array[String]): Unit = {

        val n = readInt()
        val arr = readLine().split(" ").map(_.toInt)
        val cmp = (x: Int, y: Int) => x > y
        Sorting.stableSort(arr, cmp)
        //arr.foreach(println(_))

        var dimaSum = 0
        var serejaSum = 0
        for (i <- 0 to arr.length - 1)
          if(i % 2 == 0)
            serejaSum += arr(i)
          else
            dimaSum += arr(i)

        println(serejaSum + " " + dimaSum)

    }
}


object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n1, m1, x, y) = in.next().split(' ').map(_.toInt)
  val men = in.next().split(' ').map(_.toInt)
  val vests = in.next().split(' ').map(_.toInt)

  def process(mi: Int = 0, vi: Int = 0, pairs: List[(Int, Int)] = Nil): List[(Int, Int)] =
    if (mi == n1 || vi == m1) pairs
    else {
      if (men(mi) + y >= vests(vi) && men(mi) - x <= vests(vi))
        process(mi + 1, vi + 1, (mi + 1, vi + 1) :: pairs)
      else if (men(mi) < vests(vi)) process(mi + 1, vi, pairs)
      else process(mi, vi + 1, pairs)
  }

  val res = process()
  println(res.length)
  println(res.map(i => s"${i._1} ${i._2}").mkString("\n"))
}

object Main {
    def main(args: Array[String]) {
        val nmxy = readLine.split(" ").map(i => i.toInt)
        val n = nmxy(0)
        val m = nmxy(1)
        val x = nmxy(2)
        val y = nmxy(3)

        val a = readLine.split(" ").map(i => i.toInt)
        val b = readLine.split(" ").map(i => i.toInt)

        var ap = 0
        var bp = 0

        var resa = new Array[Int](n)
        var resb = new Array[Int](n)

        var i = 0

        while(ap < n && bp < m) {
            if(a(ap)-x <= b(bp) && b(bp) <= a(ap)+y) {
                resa(i) = ap
                resb(i) = bp
                i += 1
                ap += 1
                bp += 1
            } else if(a(ap)-x > b(bp)) {
                bp += 1
            } else {
                ap += 1
            }
        }

        println(i)
        (0 until i).foreach(j => println((resa(j)+1)+" "+(resb(j)+1)))
    }
}

import java.util.Scanner

object A {
  val in = new Scanner(System.in)

  def main(args: Array[String]) {
    val n = in.nextInt()
    val m = in.nextInt()
    val x = in.nextInt()
    val y = in.nextInt()
    val a = Array.fill[Int](n)(in.nextInt())
    val b = Array.fill[Int](m)(in.nextInt())
//    a.sortWith(_ < _)
//    b.sortWith(_ < _)
    var j = 0
    var ans = List[(Int,  Int)]()
    for (i <- 0 until a.length) {
      while (j < b.length && b(j) < a(i) - x) {
        j += 1
      }
//      println(i + " " + j + " " + b(j)  + " " + (a(i) + y))
      if (j < b.length && b(j) <= a(i) + y) {
        ans ::= (i + 1, j + 1)
        j += 1
      }
    }
    println(ans.size)
    for (x <- ans) {
      println(x._1 + " " + x._2)
    }

  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n1, m1, x, y) = in.next().split(' ').map(_.toInt)
  val men = in.next().split(' ').map(_.toInt)
  val vests = in.next().split(' ').map(_.toInt)

  def process(mi: Int = 0, vi: Int = 0, pairs: List[(Int, Int)] = Nil): List[(Int, Int)] =
    if (mi == n1 || vi == m1) pairs
    else {
      if (men(mi) + y >= vests(vi) && men(mi) - x <= vests(vi))
        process(mi + 1, vi + 1, (n1 - mi, m1 - vi) :: pairs)
      else if (men(mi) < vests(vi)) process(mi + 1, vi, pairs)
      else process(mi, vi + 1, pairs)
  }

  val res = process()
  println(res.length)
  println(res.map(i => s"${i._1} ${i._2}").mkString("\n"))
}


object Chores169A   extends App {
    import java.io.{InputStream, PrintStream}
    import java.util.Scanner
    solve(System.in,System.out)
    def solve(in: InputStream, out: PrintStream): Unit = {
        val scanner = new Scanner(in)
      val n = scanner.nextInt()
      val a = scanner.nextInt()
      val b = scanner.nextInt()

      val h = (1 to n).map(_ => scanner.nextInt()).sorted.toArray
      val (bChores,aChores) = h.splitAt(b)
      val lastB = bChores.last
      val firstA = aChores.head

      /**
        * todos los valores mayores o iguales que lastB y menores que firstA son validos
        */
      val result = firstA-lastB
      out.println(result)
    }
}

object Solution extends App {
  val data = scala.io.Source.stdin.getLines()
  val Array(n, a, b) = data.next().split(" ").map(_.toInt)
  val tasks = data.next().split(" ").map(_.toInt).sorted
  println(tasks(b) - tasks(b - 1))
}

object Solution extends App {
  val data = scala.io.Source.stdin.getLines()
  val Array(n, a, b) = data.next().split(" ").map(_.toInt)
  val tasks = data.next().split(" ").map(_.toInt).sorted
  if (tasks(b) != tasks(b - 1)) println(tasks(b - 1))
  else println(0)

}

object Solution extends App {
  val data = scala.io.Source.stdin.getLines()
  val Array(n, a, b) = data.next().split(" ").map(_.toInt)
  val tasks = data.next().split(" ").map(_.toInt).sorted
  if (tasks(b) != tasks(b - 1)) println(tasks(b - 1))
  else tasks(0)

}


import scala.io.StdIn._
import scala.collection.mutable.HashSet

object Codeforces extends App {
  val n = readInt()
  val bag = HashSet[String]()
  1 to n foreach(_ => {
    val name = readLine()
    if (bag.contains(name))
      println("YES")
    else {
      bag.add(name)
      println("NO")
    }
  })
}

object ATomRiddlesDiary extends App {
  val n = scala.io.StdIn.readLine.toInt
  (0 until n).foldLeft(Map.empty[String, Boolean]) { (map, _) =>
      val name = scala.io.StdIn.readLine
      if (map contains name) println("YES")
      else {
        println("NO")
      }
      map + (name -> true)
  }
}

import java.util.Scanner
import java.util.HashSet

object TomRiddleDiary {
  def main(args: Array[String]): Unit = {
    val in = new Scanner(System.in)

    val n = in.nextInt

    val s = new HashSet[String]
    val _ = in.nextLine

    (1 to n).map {
      x => {
        val str = in.nextLine
        if (s.contains(str)) {
          println("YES")
        } else {
          println("NO")
        }

        s.add(str)
      }
    }
  }
}