object CF928A extends App {
    val sc = new java.util.Scanner(System.in)
    var n: Int = sc.nextInt()
    var o: Int = 0
    var e: Int = 0
    while (n != 0) {
        val x: Int = sc.nextInt()
        if (x % 2 == 0) e = e + 1
        else o = o + 1
        n = n - 1
    }
    var ans: Int = 0
    if (o > e) ans = e
    else ans = o
    e -= ans
    o -= ans
    ans += o / 3
    println(ans)
  }

object Main extends App {
	var i = 0
    var o = 0
    var e = 0
    var n = readLine().toLong
    val arr = readLine().split(" ").map(_.toLong)
    var k = 0

    while (i < n) {
        k = arr(i).toInt
        if (k % 2 == 1) {o += 1}
        else {e += 1}
        i += 1
    }
    if (o <= e) {print(o)}
    else {print(e + ((o-e) / 3))}
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
     var no = 0;
    for (i <- 1 to n){
        if (sc.nextInt() % 2 == 1){
            no= no + 1;
        }
    }

    var a = Math.min(no,  n /2)
    var done = false
    while(a>0 && !done){
        var nl = (no - a)/2 + (n - no);
        if (nl < a)
            a = a -1;
        else{

            done = true
        }
    }

    print(a)
}
}

import scala.io.StdIn._

object A extends App {
  readInt()

  val pairs = readLine().split(" ").map(_.toInt).dropWhile(_ == 0).reverse.dropWhile(_ == 0)

  var odd = 0
  var even = 0
  for (p <- pairs) {
    if (p % 2 == 0) {
      even += 1
    } else {
        odd += 1
    }
  }

  if (even > odd) {
      println(odd)
  } else {
      println(even + (odd - even) / 3)
  }

}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
    var z = new Array[Integer](n)
    var fr = new Array[Integer](2)
    for(i <- 0 until z.length) {
        z(i) = sc.nextInt()
        fr(z(i)%2) += 1
    }
    var res = 0
    var min = math.min(fr(0), fr(1))
    res += min
    fr(0) -= min
    fr(1) -= min
    res += fr(1)/3
    println(res)
  }
}

object Main {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
    var a1 = 0
    var a0 = 0
    for (a <- 1 to n) {
        val x = sc.nextInt()
        if (x % 2 == 0) a0 = a0 + 1
        else a1 = a1 + 1
    }
    var x = math.min(a1,a0)
    a1 = a1 - x
    x = x + a1 / 3

    println(x)
  }
}

object VK {
   def main(args: Array[String]) = {
      val sc = new java.util.Scanner(System.in)
      val n = sc.nextInt()

      var d = 0
      var e = 0
      for( i <- 1 to n ) {
         val a = sc.nextInt()
         if(a % 2 == 0) d = d + 1; else e = e + 1;
      }

      var res = 0
      if(e < d) {
         res = e
      } else {
         res = d + (e - d) / 3
      }

      println(res)
  }
}

import scala.io.StdIn._

object Hello extends App {
  val n = readInt()
  val xs = readLine().split(" ").map(_.toInt)
  var o = 0;
  var e = 0;
  var i = 0
  for ( i <- xs) {
    if (i %2 == 0) {
      e = e+1
    } else {
      o = o+1
    }
  }
  var ans = 0
  if (o < e) {
    ans = o
  } else {
    ans = e
  }
  e-=ans
  o-=ans
  ans += o/3
  println(ans)
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
    var a = 0
    var b = 0
    for ( i <- 1 to n)  {
        val k = sc.nextInt()
        if(k % 2 == 0) a += 1
        else b += 1
  }
  var ans = 0;

  if(a > b) {ans += b
  a -= b;
  b = 0;
  }

  else{ ans += a
  b -= a;
  a = 0;

    ans += b / 3
  }
  println(ans)
}
}

import java.util.Scanner

object Main extends App {
  val scanner = new Scanner(System.in)
  val n = scanner.nextInt()
  val arr = new Array[Int](n)
  var c = 0
  var c2 = 0

  for (i <- 0 until n) {
    arr.update(i, scanner.nextInt())
    if (arr(i) % 2 == 0)
      c += 1
    else
      c2 += 1
  }

  val mn = Integer.min(c, c2)
  c2 -= mn

  println(mn + c2 / 3)

}

object Main extends App {
  val n = readInt
  val s = readLine
  val a = s.split(" ").map(i => i.toInt)
  var n1 = 0
  for(i <- 0 until n) {
    if (a(i) % 2 == 0)
      n1 += 1
  }
  var n2 = n - n1
  if (n2 <= n1)
    println(n2)
  else
      println(n1 + (n2 - n1) / 3)
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()

    var c1 = 0
    for ( i <- 1 to n)
    {
        var x = sc.nextInt()
        if (x % 2 == 1)
        {
            c1 = c1 + 1
        }
    }
    var c2 = n - c1

    var ans = 0

    if (c2 < c1)
    {
        ans = c2 + (c1 - c2) / 3
    }
    else
    {
        ans = c1
    }

    println(ans)
  }
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
    var a = 0
    var b = 0
    for ( i <- 1 to n)  {
        val k = sc.nextInt()
        if(k % 2 == 0) a += 1
        else b += 1
  }
  if(a > b) println(b)
  else println(a)
}
}

object Main extends App {
  val n = readInt
  val s = readLine
  val a = s.split(" ").map(i => i.toInt)
  var n1 = 0
  for(i <- 0 until n) {
    if (a(i) % 2 == 0)
      n1 += 1
  }
  var n2 = n - n1
  if (n2 <= n1)
    println(n2)
  else
    if ((n2 - n1) % 3 == 1)
      println(math.max(0, n1 + (n2 - n1) / 3 - 1))
    else
      println(n1 + (n2 - n1) / 3)
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()

    var c1 = 0
    for ( i <- 1 to n)
    {
        var x = sc.nextInt()
        if (x % 2 == 1)
        {
            c1 = c1 + 1
        }
    }

    val c2 = n - c1

    var ans = c1
    if (c2 < c1)
    {
        ans = c2
    }

    println(ans)
  }
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
    val a: List[Int] = (1 to n).toList.foldLeft(List.empty[Int]){case (list, _) => list :+ sc.nextInt()}

    println(a.count(
      x => x % 2 == 1
    ))
  }
}

object Main extends App {
	var i = 0
    var o = 0
    var e = 0
    var n = readLine().toInt
    val arr = readLine().split(" ").map(_.toInt)
    var k = 0

    while (i < n) {
        k = arr(i)
        if (k % 2 == 1) {o += 1}
        else {e += 1}
        i += 1
    }
    if (o <= e) {print(o)}
    else {print(e + ((o-e) % 3))}
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
     var no = 0;
    for (i <- 1 to n){
        if (sc.nextInt() % 2 == 0){
            no= no + 1;
        }
    }
    print(Math.min(no,  n /2))
}
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
     var no = 0;
    for (i <- 1 to n){
        if (sc.nextInt() % 2 == 1){
            no= no + 1;
        }
    }
    if (n == 2 && no == 2)
        print(0); else
    print(Math.min(no,  n /2))
}
}

object CF158A {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
     var no = 0;
    for (i <- 1 to n){
        if (sc.nextInt() % 2 == 1){
            no= no + 1;
        }
    }
    print(Math.min(no,  n /2))
}
}

object Main {
  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt()
    var a1 = 0
    var a0 = 0
    for (a <- 1 to n) {
        val x = sc.nextInt()
        if (x % 2 == 0) a0 = a0 + 1
        else a1 = a1 + 1
    }
    // val a = Array.fromFunction(_ => sc.nextInt())(n)

    // val a1 = a.count(x => x % 2 == 1)
    // val a0 = a.count(x => x % 2 == 0)

    println(math.min(a1,a0))
  }
}


object Solution extends App {

    val input = scala.io.StdIn.readLine()
    val reverseInput = input.reverse
    val inputSize = input.size
    val enough = input.contains("AB") && input.contains("BA")
    val abFirst = input.indexOf("AB") + 1 < inputSize - reverseInput.indexOf("AB") - 2
    val baFirst = input.indexOf("BA") + 1 < inputSize - reverseInput.indexOf("BA") - 2
    if (enough && (abFirst || baFirst)) {
        println("YES")
    } else {
        println("NO")
    }
}

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

object CF550A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
  val reader=new BufferedReader(new InputStreamReader(stream), 32768);

  def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
            st=new StringTokenizer(currentLine)
      }
      return st.nextToken
  }

  def nextInt():Int = {
      return next.toInt
  }

  def nextLong():Long = {
      return next.toLong
  }
  def nextDouble:Double = {
          return next.toDouble
      }
  }

  def main(args: Array[String]): Unit = {
    val in = new InputReader(System.in)
    var s = in.next()
    val ok = solve(s,"AB","BA") || solve(s,"BA","AB")
    if ( ok ) print("YES")
    else print("NO")
  }

  def solve(ss: String, a: String, b:String ): Boolean =  {
    var s = ss
    if ( s.indexOf(a) != -1)
      s = s.replaceFirst(a, "XX")
    else return false;
    if ( s.indexOf(b) != -1)
      return true;
    else return false
  }
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  def find(): String = {
    var AB = false
    var BA = false

    var i = 0
    while (i < s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }
      else if (!AB && i < s.length - 2 && s(i + 1) == 'A' && s(i + 2) == 'B') {
        AB = true
        i += 2
      }
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') {
        BA = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    AB = false
    BA = false

    i = 0
    while (i < s.length - 1) {
      if (!BA && s(i) == 'B' && s(i + 1) == 'A' ) {
        BA = true
        i += 1
      }
      else if (!BA && i < s.length - 2 && s(i + 1) == 'B' && s(i + 2) == 'A') {
        BA = true
        i += 2
      }
      else if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    "NO"
  }

  println(find())
}

object Solution extends App {

    val input = scala.io.StdIn.readLine()

    val enough = input.contains("AB") && input.contains("BA")
    val abFirst = input.indexOf("AB") + 1 < input.lastIndexOf("BA")
    val baFirst = input.indexOf("BA") + 1 < input.lastIndexOf("AB")

    if (enough && (abFirst || baFirst)) {
        println("YES")
    } else {
        println("NO")
    }
}

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

object CF550A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
  val reader=new BufferedReader(new InputStreamReader(stream), 32768);

  def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
            st=new StringTokenizer(currentLine)
      }
      return st.nextToken
  }

  def nextInt():Int = {
      return next.toInt
  }

  def nextLong():Long = {
      return next.toLong
  }
  def nextDouble:Double = {
          return next.toDouble
      }
  }

  def main(args: Array[String]): Unit = {
    val in = new InputReader(System.in)
    var s = in.next()
    if ( s.indexOf("AB") != -1)
      s = s.replaceFirst("AB", "")
    else return "NO"
    if ( s.indexOf("BA") != -1)
      return "YES"
    else return "NO"

  }
}

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

object CF550A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
  val reader=new BufferedReader(new InputStreamReader(stream), 32768);

  def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
            st=new StringTokenizer(currentLine)
      }
      return st.nextToken
  }

  def nextInt():Int = {
      return next.toInt
  }

  def nextLong():Long = {
      return next.toLong
  }
  def nextDouble:Double = {
          return next.toDouble
      }
  }

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

  def solve : String =  {
    val in = new InputReader(System.in)
    var s = in.next()
    if ( s.indexOf("AB") != -1)
      s = s.replaceFirst("AB", "")
    else return "NO"
    if ( s.indexOf("BA") != -1)
      return "YES"
    else return "NO"
  }
}

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

object CF550A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
  val reader=new BufferedReader(new InputStreamReader(stream), 32768);

  def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
            st=new StringTokenizer(currentLine)
      }
      return st.nextToken
  }

  def nextInt():Int = {
      return next.toInt
  }

  def nextLong():Long = {
      return next.toLong
  }
  def nextDouble:Double = {
          return next.toDouble
      }
  }

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

  def solve : String =  {
    val in = new InputReader(System.in)
    var s = in.next()
    if ( s.indexOf("AB") != -1)
      s = s.replaceFirst("AB", "XX")
    else return "NO"
    if ( s.indexOf("BA") != -1)
      return "YES"
    else return "NO"
  }
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  def find(): String = {
    var AB = false
    var BA = false

    for (i <- 0 until s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') AB = true
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') BA = true
      else if (AB && BA) {
        return "YES"
      }
    }

    AB = false
    BA = false

    for (i <- 0 until s.length - 1) {
      if (!BA && s(i) == 'B' && s(i + 1) == 'A') BA = true
      else if (!BA && s(i) == 'A' && s(i + 1) == 'B') AB = true
      else if (AB && BA) {
        return "YES"
      }
    }

    "NO"
  }

  println(find())
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  def find(): String = {
    var AB = false
    var BA = false

    var i = 0
    while (i < s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') {
        BA = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    AB = false
    BA = false

    i = 0
    while (i < s.length - 1) {
      if (!BA && ((s(i) == 'B' && s(i + 1) == 'A') || (i < s.length - 2 && s(i + 1) == 'B' && s(i + 2) == 'A'))) {
        BA = true
        i += 1
      }
      else if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    "NO"
  }

  println(find())
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  def find(): String = {
    var AB = false
    var BA = false

    var i = 0
    while (i < s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') {
        BA = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    AB = false
    BA = false

    i = 0
    while (i < s.length - 1) {
      if (!BA && s(i) == 'B' && s(i + 1) == 'A') {
        BA = true
        i += 1
      }
      else if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    "NO"
  }

  println(find())
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  def find(): String = {
    var AB = false
    var BA = false

    var i = 0
    while (i < s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') {
        BA = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    AB = false
    BA = false

    i = 0
    while (i < s.length - 1) {
      if (!BA && s(i) == 'B' && s(i + 1) == 'A' ) {
        BA = true
        i += 1
      }
      else if (i < s.length - 2 && s(i + 1) == 'B' && s(i + 2) == 'A') {
        BA = true
        i += 2
      }
      else if (!AB && s(i) == 'A' && s(i + 1) == 'B') {
        AB = true
        i += 1
      }

      if (AB && BA) {
        return "YES"
      }

      i += 1
    }

    "NO"
  }

  println(find())
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  var AB = false
  var BA = false

  def find(): String = {
    for (i <- 0 until s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') AB = true
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') BA = true
      else if (AB && BA) {
        return "YES"
      }
    }

    for (i <- 0 until s.length - 1) {
      if (!BA && s(i) == 'B' && s(i + 1) == 'A') BA = true
      else if (!BA && s(i) == 'A' && s(i + 1) == 'B') AB = true
      else if (AB && BA) {
        return "YES"
      }
    }

    "NO"
  }

  println(find())
}

import scala.io.StdIn._

object A extends App {
  val s = readLine().toCharArray

  def find(): String = {
    var AB = false
    var BA = false

    for (i <- 0 until s.length - 1) {
      if (!AB && s(i) == 'A' && s(i + 1) == 'B') AB = true
      else if (!BA && s(i) == 'B' && s(i + 1) == 'A') BA = true

      if (AB && BA) {
        return "YES"
      }
    }

    AB = false
    BA = false

    for (i <- 0 until s.length - 1) {
      if (!BA && s(i) == 'B' && s(i + 1) == 'A') BA = true
      else if (!AB && s(i) == 'A' && s(i + 1) == 'B') AB = true

      if (AB && BA) {
        return "YES"
      }
    }

    "NO"
  }

  println(find())
}

object Solution extends App {

    val input = scala.io.StdIn.readLine()
    val reverseInput = input.reverse
    val inputSize = input.size

    if (input.indexOf("AB") + 1 < inputSize - reverseInput.indexOf("AB") - 2) {
        println("YES")
    } else if (input.indexOf("BA") + 1 < inputSize - reverseInput.indexOf("BA") - 2) {
        println("YES")
    } else {
        println("NO")
    }
}


import scala.collection._

object B extends App {

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

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1)
  marked ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val _fromA = scala.actors.Futures.future(shortestPaths(edges, a))
  val _fromB = scala.actors.Futures.future(shortestPaths(edges, b))
  val fromA = _fromA()
  val fromB = _fromB()

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)

  Console.flush
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1) ++= readInts0 //(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    /*val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt*/
    val Array(a, b) = readInts0
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1) ++ readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts(2)
    edges(a).append(b)
    edges(b).append(a)
  }

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1) ++ readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int](2))

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts(2)
    edges(a).append(b)
    edges(b).append(a)
  }

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1)
  marked ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1) ++= readInts0 //(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1)
  val st = tokenizeLine
  for (i <- 0 until m) marked += st.nextToken.toInt

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a).append(b)
    edges(b).append(a)
  }

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1)
  marked ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

//  val _fromA = scala.actors.Futures.future(shortestPaths(edges, a))
//  val _fromB = scala.actors.Futures.future(shortestPaths(edges, b))
//  val fromA = _fromA()
//  val fromB = _fromB()

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1)
  marked ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1) ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1) ++ readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    //val Array(a, b) = readInts(2)
    val (a, b) = (st.nextToken.toInt, st.nextToken.toInt)
    edges(a).append(b)
    edges(b).append(a)
  }

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1) ++ readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a).append(b)
    edges(b).append(a)
  }

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1)
  marked ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    //visited += source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    //visited += source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)

  val marked = new mutable.BitSet(n + 1)
  marked ++= readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      for (succ <- graph(next); if !visited(succ)) {
        toVisit += succ
        visited += succ
      }
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size)
    visited += source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) { // BFS
      val next = toVisit.dequeue
      for (succ <- graph(next); if !visited(succ)) {
        toVisit += succ
        visited += succ
        dists(succ) = dists(next) + 1
      }
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  prof(1)
  val marked = new mutable.BitSet(n + 1) ++= readInts(m)
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a).append(b)
    edges(b).append(a)
  }
  prof(4)

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1)
  prof(1)
  val st = tokenizeLine
  for (i <- 0 until m) marked += st.nextToken.toInt
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a).append(b)
    edges(b).append(a)
  }
  prof(4)

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1) ++ readInts(m)

  val edges = Array.fill(n + 1)(mutable.Seq[Int]())
println("read1")

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts(2)
    edges(a) :+= b
    edges(b) :+= a
  }
println("read2")
  def traverseFrom(graph: Array[mutable.Seq[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.Seq[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val Array(n, m, d) = readInts
  val marked = new mutable.BitSet(n + 1)
  readInts.foreach(marked += _)

  val edges = Array.fill(n + 1)(mutable.ArrayBuffer[Int]())

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts
    edges(a) :+= b
    edges(b) :+= a
  }

  if (m == 21456) println("read")

  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size + 1)
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        visited ++= succ
        toVisit ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def sssp(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size + 1)(0)
    val visited = new mutable.BitSet(graph.size + 1)
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def ssspTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        ssspTR()
      }
    }

    ssspTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = sssp(edges, a)
  val fromB = sssp(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val Array(n, m, d) = readInts
  val marked = new mutable.BitSet(n + 1)
  readInts.foreach(marked += _)

  val edges = mutable.ArrayBuffer.fill(n + 1, 0)(0)

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts
    edges(a) :+= b
    edges(b) :+= a
  }

  if (m == 21456) println("read")

  def traverseFrom(graph: mutable.ArrayBuffer[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size + 1)
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        visited ++= succ
        toVisit ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def sssp(graph: mutable.ArrayBuffer[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size + 1)(0)
    val visited = new mutable.BitSet(graph.size + 1)
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def ssspTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        ssspTR()
      }
    }

    ssspTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = sssp(edges, a)
  val fromB = sssp(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val Array(n, m, d) = readInts
  val marked = readInts.toSet

  val edges = Array.fill(n + 1)(mutable.Seq[Int]())

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts
    edges(a) :+= b
    edges(b) :+= a
  }

  @annotation.tailrec
  def traverseTR(graph: Array[mutable.Seq[Int]], toVisit: Seq[Int], visited: Set[Int], accumulator: Option[Int]): Option[Int] = {
    if (toVisit.isEmpty) {
      accumulator
    } else {
      val next = toVisit.head
      val succ = (graph(next).toSet -- visited -- toVisit).toSeq
      // DFS :
      //traverseTR(graph, succ ++ toVisit.tail, visited + next, accumulator :+ next)
      // BFS :
      traverseTR(graph, toVisit.tail ++ succ, visited + next, if (marked(next)) Some(next) else accumulator)
    }
  }

  @annotation.tailrec
  def sssp(graph: Array[mutable.Seq[Int]], toVisit: Seq[Int], visited: Set[Int], curDist: Int, dists: Array[Int]): Unit = {
    if (!toVisit.isEmpty) {
      val next = toVisit.head
      val succ = (graph(next).toSet -- visited -- toVisit).toSeq
      for (s <- succ) dists(s) = curDist
      sssp(graph, toVisit.tail ++ succ, visited + next, curDist + 1, dists)
    }
  }

  def traverseFrom(graph: Array[mutable.Seq[Int]], initial: Int) =
    traverseTR(graph, Seq(initial), Set.empty, None)

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = Array.ofDim[Int](n + 1)
  val fromB = Array.ofDim[Int](n + 1)

  sssp(edges, Seq(a), Set.empty, 1, fromA)
  sssp(edges, Seq(b), Set.empty, 1, fromB)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1) ++ readInts(m)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int](2))
if (m == 21456 ) println("read1")

  for (i <- 0 until n - 1) {
    val Array(a, b) = readInts(2)
    edges(a).append(b)
    edges(b).append(a)
  }
if (m == 21456 ) println("read2")
  def traverseFrom(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def traverseTR(accumulator: Option[Int]): Option[Int] = {
      if (toVisit.isEmpty) {
        accumulator
      } else {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        traverseTR(if (marked(next)) Some(next) else accumulator)
      }
    }

    traverseTR(None)
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val dists = Array.fill(graph.size)(0)
    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)

    @annotation.tailrec
    def shortestPathsTR(): Unit = {
      if (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        for (s <- succ) dists(s) = dists(next) + 1
        shortestPathsTR()
      }
    }

    shortestPathsTR()
    dists
  }

  val a = traverseFrom(edges, 1).get
  val b = traverseFrom(edges, a).getOrElse(a)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  prof(1)
  val marked = new mutable.BitSet(n + 1) ++= readInts(m)
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) += b
    edges(b) += a
  }
  prof(4)

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1)
  prof(1)
  val st = tokenizeLine
  for (i <- 0 until m) marked += st.nextToken.toInt
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a).append(b)
    edges(b).append(a)
  }
  prof(4)

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
        val next = toVisit.dequeue
        val succ = graph(next).filter(!visited(_))
        toVisit ++= succ
        visited ++= succ
        if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  val marked = new mutable.BitSet(n + 1)
  prof(1)
  marked ++= readInts(m)
  //val st = tokenizeLine
  //for (i <- 0 until m) marked += st.nextToken.toInt
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a).append(b)
    edges(b).append(a)
  }
  prof(4)

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  prof(1)
  val marked = new mutable.BitSet(n + 1) ++= readInts(m)
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    /*val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt*/
    val Array(a, b) = readInts(2)
    edges(a).append(b)
    edges(b).append(a)
  }
  prof(4)

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}

import scala.collection._

object B extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)

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

  def prof(i: Int) = if (m == 17585) println(i + ": " + (System.currentTimeMillis() - executionStart) + " ms")

  val Array(n, m, d) = readInts(3)
  prof(1)
  val marked = new mutable.BitSet(n + 1) ++= readInts(m)
  prof(2)

  val edges = Array.fill(n + 1)(new mutable.ArrayBuffer[Int]())
  prof(3)

  for (i <- 0 until n - 1) {
    val st = tokenizeLine
    val a = st.nextToken.toInt
    val b = st.nextToken.toInt
    edges(a) :+= b
    edges(b) :+= a
  }
  prof(4)

  def remoteVertex(graph: Array[mutable.ArrayBuffer[Int]], source: Int) = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    var remote: Option[Int] = None

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      if (marked(next)) remote = Some(next)
    }

    remote
  }

  def shortestPaths(graph: Array[mutable.ArrayBuffer[Int]], source: Int): Array[Int] = {

    val visited = new mutable.BitSet(graph.size) + source
    val toVisit = mutable.Queue(source)
    val dists = Array.fill(graph.size)(0)

    while (!toVisit.isEmpty) {
      val next = toVisit.dequeue
      val succ = graph(next).filter(!visited(_))
      toVisit ++= succ
      visited ++= succ
      for (s <- succ) dists(s) = dists(next) + 1
    }

    dists
  }

  val a = remoteVertex(edges, 1).get
  val b = remoteVertex(edges, a).getOrElse(a)
  prof(5)

  val fromA = shortestPaths(edges, a)
  val fromB = shortestPaths(edges, b)
  prof(6)

  val result = (1 to n).count(i => fromA(i) <= d && fromB(i) <= d)
  prof(7)

  println(result)
}


import java.io.PrintWriter
import util.control.Breaks._

object SolutionA {
    val reader = scala.io.StdIn
    def readInt = reader.readLine().toInt
    def readArrayInt = reader.readLine().split(" ").map(_.toInt)

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

    def solve(): Unit = {
        val n = readInt
        val a = readArrayInt

        var rs = 0
        var sum = 0
        for (i <- 0 until n) {
            if (a(i) == 0) {
                a(i) = 1
                rs += 1

            }
            sum += a(i)
        }

        if (sum == 0)
            rs += 1

        println(rs)
    }

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

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

object Main extends App {

  private val getCount: Array[Int] => Int =
    arr => {
      if (arr.product == 0) {
        val count = arr count (_ == 0)
        if (arr.sum + count == 0) count + 1 else count
      } else if (arr.sum == 0)
        1
      else
        0
    }

  var n = StdIn.readInt()
  var result: ListBuffer[Int] = new ListBuffer

  while (n != 0) {
    StdIn.readLine()
    val line = StdIn.readLine()
    val arr = line split "\\s" map (_.toInt)
    result += getCount(arr)

    n = n - 1
  }

  result foreach println

}


object A549 {

  import IO._

  def main(args: Array[String]): Unit = {
    val face = "acef"
    val Array(n, m) = readInts(2)
    val in = Array.fill(n)(read.toCharArray)
    var res = 0
    for(i <- 0 until n-1; j <- 0 until m-1) {
      var str = ""
      str += in(i)(j)
      if(i+1 < n) str += in(i+1)(j)
      if(j+1 < m) str += in(i)(j+1)
      if(i+1 < n && j +1 < m) str += in(i+1)(j+1)

      if(str.sorted.equalsIgnoreCase(face))
        res += 1
    }
    println(res)
  }

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

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

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

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

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

}

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

import scala.collection.mutable

object A {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }

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

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

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

  def nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???
    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  def solve: Int = {
    val n = nextInt
    val m = nextInt
    val a = new Array[Array[Char]](n)
    for (i <- 0 until n) {
      a(i) = next.toCharArray
    }
    var ans = 0
    val b = Array('f', 'a', 'c', 'e')
    for (i <- 0 until n - 1) {
      for (j <- 0 until m - 1) {
        val s = new mutable.HashSet[Char]()
        var flag = true
        s.add(a(i)(j))
        s.add(a(i + 1)(j))
        s.add(a(i + 1)(j + 1))
        s.add(a(i)(j + 1))
        b.foreach(x => {
          if (!s.contains(x)) {
            flag = false
          }
        })
        if (flag)
          ans += 1

      }
    }
    out.println(ans)
    return 0
  }
}

import scala.collection.immutable.IndexedSeq
import scala.collection.mutable

object A {

  def check(a: IndexedSeq[String], i: Int, j: Int): Boolean = {
    val s=mutable.TreeSet[Char]()
    s+=a(i).charAt(j)
    s+=a(i).charAt(j+1)
    s+=a(i+1).charAt(j)
    s+=a(i+1).charAt(j+1)
    s.mkString("") == "acef"
  }

  def main(args: Array[String]) {
    val Array(n,m)=io.StdIn.readLine().split(" ").map(_.toInt)
    val a = for (i<-1 to n) yield io.StdIn.readLine()
    var k=0
    for (i<-0 until n-1)
      for (j<-0 until m-1){
        if (check(a,i,j))
          k+=1
      }
    println(k)
  }
}

object First extends App{
  /* Created by bfattahov on 06.06.15. */

  val input = io.Source.stdin.getLines().toSeq
  val photo = input.drop(1)

  val firstLine = Seq.apply(input.head.split(' '): _*).map(_.trim).filterNot(_.isEmpty).take(2).map(_.toInt)
  val rows = firstLine(0)
  val cols = firstLine(1)

  //input.foreach(println)

  if (rows < 2 || cols < 2) {
    println(0)
  } else {
    val faces = (0 to cols - 2).foldLeft(0) { (res, c) => ; res + (0 to rows - 2).count(r => isItFace(c, r))}

    println(faces)
  }

  def isItFace(c: Int, r: Int): Boolean = {
    Seq(
      photo(r).charAt(c),
      photo(r).charAt(c + 1),
      photo(r + 1).charAt(c),
      photo(r + 1).charAt(c + 1)
    ).foldLeft(0) {
      (res, curr) => {
        res + (curr match {
          case 'a' => 1
          case 'e' => 2
          case 'c' => 4
          case 'f' => 8
          case _ => 0
        })
      }
    } == 15
  }
}

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

object CF549A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
    val reader=new BufferedReader(new InputStreamReader(stream), 32768);

    def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
        st=new StringTokenizer(currentLine)
      }
      return st.nextToken
    }

    def nextInt():Int = {
      return next.toInt
    }

    def nextLong():Long = {
      return next.toLong
    }
    def nextDouble:Double = {
      return next.toDouble
    }
  }

  def main(args: Array[String]): Unit = {
    val in = new InputReader(System.in)
    val m = in.nextInt()
    val n = in.nextInt()
    val board = Array.fill(m) { in.next()}
    var sum = 0;
    for(i <- 0 until m)
      for( j <- 0 until n) {
        sum += countFace(i,j,board)
      }
    print(sum)
  }

  def countFace(i: Int, j: Int, board: Array[String]): Int = {
    val b = new StringBuilder()
    for(ki <- 0 to 1)
      for(kj <-0 to 1)
        if ( i + ki < board.length && j+ kj < board(0).length )
      {
        b.append(board(i+ki).charAt(j+kj))
      }
    val s = b.toString()
    val face ="face"
    for(i <- 0 until face.length) {
      if ( s.indexOf(face.charAt(i)) == -1) return 0
    }
    return 1
  }
}

import java.util.StringTokenizer

object _549A extends App {
  var token = new StringTokenizer("")

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

  val n = next.toInt
  val m = next.toInt
  val a = (1 to n).map(i => next).toArray

  val ans = for {
    i <- 0 until n - 1
    j <- 0 until m - 1
    if Set(a(i)(j), a(i)(j + 1), a(i + 1)(j), a(i + 1)(j + 1)) == Set('f', 'a', 'c', 'e')
  } yield 1

  println(ans.sum)
}

import scala.collection._

object A extends App {

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

  val Array(n, m) = readInts(2)
  val ss = Array.fill(n){ readLine }

  def check(i: Int, j: Int) = {
    val set = Set(ss(i)(j), ss(i + 1)(j), ss(i)(j + 1), ss(i + 1)(j + 1))
    set('f') && set('a') && set('c') && set('e')
  }

  var res = 0
  for (i <- 0 until n - 1)
    for (j <- 0 until m - 1) {
      if (check(i, j)) res += 1
    }
  println(res)
}

import java.util.Scanner

object A {

	def detectFace(c1:Char, c2:Char, c3:Char, c4:Char):Boolean =
		List(c1, c2, c3, c4).sorted.mkString.equals("acef")

	def countFaces(s1:String, s2:String):Int = {
		return (0 until s1.length() - 1)
			.map(i => detectFace(s1.charAt(i), s1.charAt(i + 1), s2.charAt(i), s2.charAt(i + 1)))
			.map(b => if (b) 1 else 0)
			.reduceLeft(_+_)
	}

	def readAndCountFaces(n:Int):Int = {
		var count:Int = 0
		var s1:String = in.nextLine
		for (i <- 2 to n) {
			var s2:String = in.nextLine
			count += countFaces(s1, s2)
			s1 = s2
		}
		count
	}

	val in:Scanner = new Scanner(System.in)

	def main(args: Array[String]) = {
		var n:Int = in.nextInt
		var m:Int = in.nextInt
		in.nextLine

		val count:Int = if (n > 1 && m > 1) readAndCountFaces(n) else 0

		println(count)
	}

}


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

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

  private val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

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

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

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

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

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

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

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

  def solve(): Unit = {
    REP(ni()) { _ =>
      val S = ns()
      val N = S.length
      val cnt1 = S.count(_ == '1')
      var cnt2 = 0
      REP(N - 1) { i =>
        if (S(i) == '1' && S(i + 1) == '0') cnt2 += 1
      }

      var cnt3 = 0L
      var c = 0 // 連続した0の個数
      REP(N) { i =>
        if (S(i) == '0') c += 1
        else {
          var x = 0
          REP(min(20, N - i)) { j =>
            if (S(i + j) == '1') x += 1
            if (x > 2 && x <= c + j + 1) cnt3 += 1
            x *= 2
          }
          c = 0
        }
      }

      debug(s"$cnt1 $cnt2 $cnt3")

      val ans = cnt1 + cnt2 + cnt3
      out.println(ans)
    }
  }
}

object C 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(t) = readInts(1)

  for (_ <- 1 to t) {
    val s = readLine.map(_ - '0')
    var res = 0L

    val prevZeros = Array.fill(s.length)(0)
    var z = 0
    for (i <- s.indices) {
      if (s(i) == 0) {
        z += 1
      } else z = 0
      prevZeros(i) = z
    }

    for (r <- s.indices) {
      var len = 0
      var x = 0L
      while (1L << len <= r + 1 && len <= r) {
        x = x + (s(r - len) << len)
        len += 1
        if (x == len) res += 1
        //println(len, x, r)
      }
      //if (len <= r) println("!", len, x, prevZeros(r - len))
      if (len <= r && prevZeros(r - len) + len >= x && x > len) res += 1
    }

    println(res)
  }
}


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

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

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Int]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }
  private def isDebug[A](debug: => A, online: => A): A = {
    if (oj) online else debug
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 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
  }
}

object Workspace {
  import Main._
}

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

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

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    REP(ni()) { _ =>
      val n = ni()
      var zeros, ones = 0
      var odds = 0
      var bitOdds = 0
      REP(n) { _ =>
        val s = ns()
        val z = s.count(_ == '0')
        val o = s.length - z
        zeros += z
        ones += o
        if (s.length % 2 == 1) odds += 1
      }

      debug(s"$zeros $ones $odds")

      if (zeros % 2 == 1) bitOdds += 1
      if (ones % 2 == 1) bitOdds += 1

      val ok = if (bitOdds == 0) {
        odds % 2 == 0
      } else if (bitOdds == 1) {
        odds % 2 == 1
      } else {
        odds >= 2 && odds % 2 == 0
      }

      if (ok) {
        out.println(n)
      } else {
        out.println(n - 1)
      }
    }
  }
}

object BinaryPalindrome extends App {

  val array = readFromTerminal()
  for (row <- array)
    println(compute(row))

  def readFromTerminal(): Seq[Seq[String]] = {
    val q = readInt()
//    val q = 4
//    val list = List(
//      List("0"),
//      List("1110","100110","010101"),
//      List("11111","000001"),
//      List("001","11100111"),
//    )
//1
//2
//2
//2
    val list: Seq[Seq[String]] = (1 to q).toList.map { j => {
      val n = readInt()
      val t: Seq[String] = for (i <- 1 to n) yield readLine()
      t
    }}
    list
  }

  def compute(rows: Seq[String]): Int = {
    val (zeros, ones): (Int, Int) = rows.map{ row => (
      row.count{ c => c == '0'},
      row.count{ c => c =='1'}
    )}.fold((0,0))((acc, pair) => (acc._1 + pair._1,acc._2 + pair._2))
    //все длины
    val lengths = rows.map{row => row.length }
    //сколько есть пар
    val possiblePairs = (zeros / 2)  + (ones / 2)
    // сколько нужно пар
    val needPairs = lengths.fold(0){(acc, length) => acc + length/2}

    val unpairedNumbers = lengths.sum - possiblePairs*2
    if ((possiblePairs - needPairs) <0 )
      lengths.size+(possiblePairs - needPairs)
    else
      lengths.size
  }
}