import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](n, LOGN)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(i)(0) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(i)(j) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(i)(j - 1) != -1)
            dp(i)(j) = dp(dp(i)(j - 1))(j - 1)
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, d: Int) = {
      var x = u
      var rd = d
      var i = LOGN
      while (i >= 0) {
        if (rd - (1 << i) >= 0 && dp(x)(i) != -1) {
          rd -= (1 << i)
          x = dp(x)(i)
        }
        i -= 1
      }
      x
    }

    def lca(pp: Int, qq: Int) = {
      var tmp, log, i = 0
      var p = pp
      var q = qq

      if (depth(p) < depth(q)) {
        tmp = p
        p = q
        q = tmp
      }

      log = 1
      while ((1 << log) <= depth(p)) log += 1
      log -= 1

      i = log
      while (i >= 0) {
        if (depth(p) - (1 << i) >= depth(q)) p = dp(p)(i)
        i -= 1
      }

      if (p == q) {
        p
      } else {
        i = log
        while (i >= 0) {
          if (dp(p)(i) != -1 && dp(p)(i) != dp(q)(i)) {
            p = dp(p)(i)
            q = dp(q)(i)
          }
          i -= 1
        }
        parent(p)
      }
    }

  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

import scala.annotation.tailrec

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  object TraverseUtil {

    def FOR(from: Int, to: Int)(f: Int => Unit): Unit = {
      var i = from
      while (i <= to) {
        f(i)
        i += 1
      }
    }

  }

  import TraverseUtil._

  class UndirectedGraph(val n: Int) {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: UndirectedGraph =>

    val lgn = (1 to 32).find(x => (1 << x) >= n).get
    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](lgn, n)

    private def computeLCA(): Unit = {

      FOR(0, n - 1) { i =>
        dp(0)(i) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(j)(i) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        FOR(0, n - 1) { i =>
          if (dp(j - 1)(i) != -1)
            dp(j)(i) = dp(j - 1)(dp(j - 1)(i))
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = (0 until lgn).foldLeft(u) {
      (v, i) => if (((count >> i) & 1) == 1) dp(i)(v) else v
    }

    @tailrec final def lca(u: Int, v: Int): Int = {
      if (depth(u) > depth(v)) lca(ancestor(u, depth(u) - depth(v)), v)
      else if (depth(u) < depth(v)) lca(u, ancestor(v, depth(v) - depth(u)))
      else if (u == v) u
      else parent(((lgn - 1) to 0 by -1).foldLeft((u, v)) {
        (t, i) => if (dp(i)(t._1) != dp(i)(t._2)) (dp(i)(t._1), dp(i)(t._2)) else t
      }._1)
    }
  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    FOR(1, n-1) { _ =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt - 1)
      t.addEdge(u, v)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    FOR(1,m) { _ =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt - 1)

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](n, LOGN)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(i)(0) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(i)(j) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(i)(j - 1) != -1)
            dp(i)(j) = dp(dp(i)(j - 1))(j - 1)
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = {

      (0 until LOGN).foldLeft(u) {
        (v, i) => if (((count >> i) & 1) == 1) dp(v)(i) else v
      }

//      var x = u
//      var rd = count
//      var i = LOGN
//      while (i >= 0) {
//        if (rd - (1 << i) >= 0 && dp(x)(i) != -1) {
//          rd -= (1 << i)
//          x = dp(x)(i)
//        }
//        i -= 1
//      }
//      x
    }

    def lca(pp: Int, qq: Int) = {
      var tmp, log, i = 0
      var p = pp
      var q = qq

      if (depth(p) < depth(q)) {
        tmp = p
        p = q
        q = tmp
      }

      log = 1
      while ((1 << log) <= depth(p)) log += 1
      log -= 1

      i = log
      while (i >= 0) {
        if (depth(p) - (1 << i) >= depth(q)) p = dp(p)(i)
        i -= 1
      }

      if (p == q) {
        p
      } else {
        i = log
        while (i >= 0) {
          if (dp(p)(i) != -1 && dp(p)(i) != dp(q)(i)) {
            p = dp(p)(i)
            q = dp(q)(i)
          }
          i -= 1
        }
        parent(p)
      }
    }

  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  var adj: Array[List[Int]] = null
  var t: Array[Int] = null
  var h: Array[Int] = null
  var sz: Array[Int] = null

  def dfs(u: Int, pu: Int): Unit = {
    sz(u) = 1
    adj(u).foreach { v =>
      if (v != pu) {
        t(v) = u
        h(v) = h(u) + 1
        dfs(v, u)
        sz(u) += sz(v)
      }
    }
  }

  def lca(pp: Int, qq: Int, n: Int, dp: Array[Array[Int]]) = {
    var tmp, log, i = 0
    var p = pp
    var q = qq

    if (h(p) < h(q)) {
      tmp = p
      p = q
      q = tmp
    }

    log = 1
    while ((1 << log) <= h(p)) log += 1
    log -= 1

    i = log
    while (i >= 0) {
      if (h(p) - (1 << i) >= h(q)) p = dp(p)(i)
      i -= 1
    }

    if (p == q) {
      p
    } else {
      i = log
      while (i >= 0) {
        if (dp(p)(i) != -1 && dp(p)(i) != dp(q)(i)) {
          p = dp(p)(i)
          q = dp(q)(i)
        }
        i -= 1
      }
      t(p)
    }
  }

  def ancestor(u: Int, d: Int, dp: Array[Array[Int]]) = {
    var x = u
    var rd = d
    var i = LOGN
    while (i >= 0) {
      if (rd - (1 << i) >= 0 && dp(x)(i) != -1) {
        rd -= (1 << i)
        x = dp(x)(i)
      }
      i -= 1
    }
    x
  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt
    adj = new Array[List[Int]](n)
    t = new Array[Int](n)
    h = new Array[Int](n)
    sz = new Array[Int](n)

    (0 until n).foreach { i => adj(i) = Nil}

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      adj(u - 1) = (v - 1) :: adj(u - 1)
      adj(v - 1) = (u - 1) :: adj(v - 1)
    }

    val m = in.readLine().toInt

    t(0) = -1

    dfs(0, -1)

    val dp = Array.ofDim[Int](n, LOGN)

    (0 until n).foreach { i =>
      dp(i)(0) = t(i)
      var j = 1
      while ((1 << j) < n) {
        dp(i)(j) = -1
        j += 1
      }
    }

    var j = 1
    while ((1 << j) < n) {
      (0 until n).foreach { i =>
        if (dp(i)(j - 1) != -1)
          dp(i)(j) = dp(dp(i)(j - 1))(j - 1)
      }
      j += 1
    }

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {
        val w = lca(u, v, n, dp)
        val d = h(u) + h(v) - 2 * h(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (h(u) == h(v)) {
            val x = ancestor(u, d / 2 - 1, dp)
            val y = ancestor(v, d / 2 - 1, dp)
            out.println(n - sz(x) - sz(y))
          } else {
            val (_, yTmp) = if (h(u) > h(v)) (v, u) else (u, v)
            val y = ancestor(yTmp, d / 2 - 1, dp)
            out.println(sz(t(y)) - sz(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

import scala.annotation.tailrec

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  class UndirectedGraph(val n: Int) {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: UndirectedGraph =>

    val lgn = (1 to 32).find(x => (1 << x) >= n).get
    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](lgn, n)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(0)(i) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(j)(i) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(j - 1)(i) != -1)
            dp(j)(i) = dp(j - 1)(dp(j - 1)(i))
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = (0 until lgn).foldLeft(u) {
      (v, i) => if (((count >> i) & 1) == 1) dp(i)(v) else v
    }

    @tailrec final def lca(u: Int, v: Int): Int = {
      if (depth(u) > depth(v)) lca(ancestor(u, depth(u) - depth(v)), v)
      else if (depth(u) < depth(v)) lca(u, ancestor(v, depth(v) - depth(u)))
      else if (u == v) u
      else parent(((lgn - 1) to 0 by -1).foldLeft((u, v)) {
        (t, i) => if (dp(i)(t._1) != dp(i)(t._2)) (dp(i)(t._1), dp(i)(t._2)) else t
      }._1)
    }
  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    val lgn = (1 to 32).find(x => (1 << x) >= n).get
    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](lgn, n)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(0)(i) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(j)(i) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(j - 1)(i) != -1)
            dp(j)(i) = dp(j - 1)(dp(j - 1)(i))
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = (0 until lgn).foldLeft(u) {
      (v, i) => if (((count >> i) & 1) == 1) dp(i)(v) else v
    }

    def lca(u: Int, v: Int): Int = {
      if (depth(u) > depth(v)) lca(ancestor(u, depth(u) - depth(v)), v)
      else if (depth(u) < depth(v)) lca(u, ancestor(v, depth(v) - depth(u)))
      else if (u == v) u
      else parent(((lgn - 1) to 0 by -1).foldLeft((u, v)) {
        (t, i) => if (dp(i)(t._1) != dp(i)(t._2)) (dp(i)(t._1), dp(i)(t._2)) else t
      }._1)
    }

  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  class UndirectedGraph(n: Int) {
    val adj = Array.fill(n)(List[Int]())

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  var t: Array[Int] = null
  var h: Array[Int] = null
  var sz: Array[Int] = null

  def dfs(u: Int, pu: Int)(implicit g: UndirectedGraph): Unit = {
    sz(u) = 1
    g.adj(u).foreach { v =>
      if (v != pu) {
        t(v) = u
        h(v) = h(u) + 1
        dfs(v, u)
        sz(u) += sz(v)
      }
    }
  }

  def lca(pp: Int, qq: Int, n: Int, dp: Array[Array[Int]]) = {
    var tmp, log, i = 0
    var p = pp
    var q = qq

    if (h(p) < h(q)) {
      tmp = p
      p = q
      q = tmp
    }

    log = 1
    while ((1 << log) <= h(p)) log += 1
    log -= 1

    i = log
    while (i >= 0) {
      if (h(p) - (1 << i) >= h(q)) p = dp(p)(i)
      i -= 1
    }

    if (p == q) {
      p
    } else {
      i = log
      while (i >= 0) {
        if (dp(p)(i) != -1 && dp(p)(i) != dp(q)(i)) {
          p = dp(p)(i)
          q = dp(q)(i)
        }
        i -= 1
      }
      t(p)
    }
  }

  def ancestor(u: Int, d: Int, dp: Array[Array[Int]]) = {
    var x = u
    var rd = d
    var i = LOGN
    while (i >= 0) {
      if (rd - (1 << i) >= 0 && dp(x)(i) != -1) {
        rd -= (1 << i)
        x = dp(x)(i)
      }
      i -= 1
    }
    x
  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt
    implicit val g = new UndirectedGraph(n)
    t = new Array[Int](n)
    h = new Array[Int](n)
    sz = new Array[Int](n)

//    (0 until n).foreach { i => adj(i) = Nil}

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      g.addEdge(u - 1, v - 1)
    }

    val m = in.readLine().toInt

    t(0) = -1

    dfs(0, -1)

    val dp = Array.ofDim[Int](n, LOGN)

    (0 until n).foreach { i =>
      dp(i)(0) = t(i)
      var j = 1
      while ((1 << j) < n) {
        dp(i)(j) = -1
        j += 1
      }
    }

    var j = 1
    while ((1 << j) < n) {
      (0 until n).foreach { i =>
        if (dp(i)(j - 1) != -1)
          dp(i)(j) = dp(dp(i)(j - 1))(j - 1)
      }
      j += 1
    }

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {
        val w = lca(u, v, n, dp)
        val d = h(u) + h(v) - 2 * h(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (h(u) == h(v)) {
            val x = ancestor(u, d / 2 - 1, dp)
            val y = ancestor(v, d / 2 - 1, dp)
            out.println(n - sz(x) - sz(y))
          } else {
            val (_, yTmp) = if (h(u) > h(v)) (v, u) else (u, v)
            val y = ancestor(yTmp, d / 2 - 1, dp)
            out.println(sz(t(y)) - sz(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    val lgn = (1 to 32).find(x => (1 << x) >= n).get
    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](lgn, n)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(0)(i) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(j)(i) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(j - 1)(i) != -1)
            dp(j)(i) = dp(j - 1)(dp(j - 1)(i))
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = (0 until lgn).foldLeft(u) {
      (v, i) => if (((count >> i) & 1) == 1) dp(i)(v) else v
    }

    //    def lca(u: Int, v: Int): Int = {
    //
    //      if (depth(u) > depth(v)) lca(ancestor(u, depth(u) - depth(v)), v)
    //      else if (depth(u) < depth(v)) lca(u, ancestor(v, depth(v) - depth(u)))
    //      else if (u==v) u
    //      else parent(((lgn-1) to 0 by -1).foldLeft((u, v)) {
    //        (t, i) => if (dp(i)(t._1) != dp(i)(t._2)) (dp(i)(t._1), dp(i)(t._2)) else t
    //      }._1)
    //    }

    def lca(pp: Int, qq: Int): Int = {
      var tmp, log, i = 0
      var p = pp
      var q = qq

      if (depth(p) < depth(q)) {
        tmp = p
        p = q
        q = tmp
      }

      log = 1
      while ((1 << log) <= depth(p)) log += 1
      log -= 1

      i = log
      while (i >= 0) {
        if (depth(p) - (1 << i) >= depth(q)) p = dp(i)(p)
        i -= 1
      }

      if (p == q) {
        p
      } else {
        i = log
        while (i >= 0) {
          if (dp(i)(p) != -1 && dp(i)(p) != dp(i)(q)) {
            p = dp(i)(p)
            q = dp(i)(q)
          }
          i -= 1
        }
        parent(p)
      }
    }

  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

import scala.annotation.tailrec

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    val lgn = (1 to 32).find(x => (1 << x) >= n).get
    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](lgn, n)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(0)(i) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(j)(i) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(j - 1)(i) != -1)
            dp(j)(i) = dp(j - 1)(dp(j - 1)(i))
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = (0 until lgn).foldLeft(u) {
      (v, i) => if (((count >> i) & 1) == 1) dp(i)(v) else v
    }

    @tailrec final def lca(u: Int, v: Int): Int = {
      if (depth(u) > depth(v)) lca(ancestor(u, depth(u) - depth(v)), v)
      else if (depth(u) < depth(v)) lca(u, ancestor(v, depth(v) - depth(u)))
      else if (u == v) u
      else parent(((lgn - 1) to 0 by -1).foldLeft((u, v)) {
        (t, i) => if (dp(i)(t._1) != dp(i)(t._2)) (dp(i)(t._1), dp(i)(t._2)) else t
      }._1)
    }

    //    def lca(pp: Int, qq: Int): Int = {
    //      var tmp, log, i = 0
    //      var p = pp
    //      var q = qq
    //
    //      if (depth(p) < depth(q)) {
    //        tmp = p
    //        p = q
    //        q = tmp
    //      }
    //
    //      log = 1
    //      while ((1 << log) <= depth(p)) log += 1
    //      log -= 1
    //
    //      i = log
    //      while (i >= 0) {
    //        if (depth(p) - (1 << i) >= depth(q)) p = dp(i)(p)
    //        i -= 1
    //      }
    //
    //      if (p == q) {
    //        p
    //      } else {
    //        i = log
    //        while (i >= 0) {
    //          if (dp(i)(p) != -1 && dp(i)(p) != dp(i)(q)) {
    //            p = dp(i)(p)
    //            q = dp(i)(q)
    //          }
    //          i -= 1
    //        }
    //        parent(p)
    //      }
    //    }
    //

  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    val parent = new Array[Int](n)
    val depth = new Array[Int](n)
    val subtreeSize = new Array[Int](n)
    val dp = Array.ofDim[Int](LOGN, n)

    private def computeLCA(): Unit = {
      (0 until n).foreach { i =>
        dp(0)(i) = parent(i)
        var j = 1
        while ((1 << j) < n) {
          dp(j)(i) = -1
          j += 1
        }
      }

      var j = 1
      while ((1 << j) < n) {
        (0 until n).foreach { i =>
          if (dp(j - 1)(i) != -1)
            dp(j)(i) = dp(j - 1)(dp(j - 1)(i))
        }
        j += 1
      }
    }

    def computeFeatures(root: Int) = {

      def dfs(u: Int, pu: Int): Unit = {
        subtreeSize(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            parent(v) = u
            depth(v) = depth(u) + 1
            dfs(v, u)
            subtreeSize(u) += subtreeSize(v)
          }
        }
      }

      parent(root) = -1
      dfs(root, -1)

      computeLCA()
    }

    def ancestor(u: Int, count: Int) = (0 until LOGN).foldLeft(u) {
      (v, i) => if (((count >> i) & 1) == 1) dp(i)(v) else v
    }

    def lca(pp: Int, qq: Int) = {
      var tmp, log, i = 0
      var p = pp
      var q = qq

      if (depth(p) < depth(q)) {
        tmp = p
        p = q
        q = tmp
      }

      log = 1
      while ((1 << log) <= depth(p)) log += 1
      log -= 1

      i = log
      while (i >= 0) {
        if (depth(p) - (1 << i) >= depth(q)) p = dp(i)(p)
        i -= 1
      }

      if (p == q) {
        p
      } else {
        i = log
        while (i >= 0) {
          if (dp(i)(p) != -1 && dp(i)(p) != dp(i)(q)) {
            p = dp(i)(p)
            q = dp(i)(q)
          }
          i -= 1
        }
        parent(p)
      }
    }

  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val t = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      t.addEdge(u - 1, v - 1)
    }

    t.computeFeatures(0)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {

        val w = t.lca(u, v)
        val d = t.depth(u) + t.depth(v) - 2 * t.depth(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (t.depth(u) == t.depth(v)) {
            val x = t.ancestor(u, d / 2 - 1)
            val y = t.ancestor(v, d / 2 - 1)
            out.println(n - t.subtreeSize(x) - t.subtreeSize(y))
          } else {
            val (_, yTmp) = if (t.depth(u) > t.depth(v)) (v, u) else (u, v)
            val y = t.ancestor(yTmp, d / 2 - 1)
            out.println(t.subtreeSize(t.parent(y)) - t.subtreeSize(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  trait GraphLike {
    def n: Int

    def adj: Array[List[Int]]
  }

  class UndirectedGraph(val n: Int) extends GraphLike {

    val adj = Array.fill(n)(List[Int]())

    def apply(i: Int) = adj(i)

    def addEdge(u: Int, v: Int): Unit = {
      adj(u) = v :: adj(u)
      adj(v) = u :: adj(v)
    }

  }

  trait TreeFeatures {
    this: GraphLike =>

    def features(root: Int) = {
      val t = new Array[Int](n)
      val h = new Array[Int](n)
      val sz = new Array[Int](n)

      def dfs(u: Int, pu: Int): Unit = {
        sz(u) = 1
        adj(u).foreach { v =>
          if (v != pu) {
            t(v) = u
            h(v) = h(u) + 1
            dfs(v, u)
            sz(u) += sz(v)
          }
        }
      }

      t(root) = -1
      dfs(root, -1)
      (t, h, sz)
    }

  }

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  def lca(pp: Int, qq: Int, n: Int, dp: Array[Array[Int]], h: Array[Int], t: Array[Int]) = {
    var tmp, log, i = 0
    var p = pp
    var q = qq

    if (h(p) < h(q)) {
      tmp = p
      p = q
      q = tmp
    }

    log = 1
    while ((1 << log) <= h(p)) log += 1
    log -= 1

    i = log
    while (i >= 0) {
      if (h(p) - (1 << i) >= h(q)) p = dp(p)(i)
      i -= 1
    }

    if (p == q) {
      p
    } else {
      i = log
      while (i >= 0) {
        if (dp(p)(i) != -1 && dp(p)(i) != dp(q)(i)) {
          p = dp(p)(i)
          q = dp(q)(i)
        }
        i -= 1
      }
      t(p)
    }
  }

  def ancestor(u: Int, d: Int, dp: Array[Array[Int]]) = {
    var x = u
    var rd = d
    var i = LOGN
    while (i >= 0) {
      if (rd - (1 << i) >= 0 && dp(x)(i) != -1) {
        rd -= (1 << i)
        x = dp(x)(i)
      }
      i -= 1
    }
    x
  }

  def getLcaTable(n: Int, t: Array[Int]) = {

    val dp = Array.ofDim[Int](n, LOGN)

    (0 until n).foreach { i =>
      dp(i)(0) = t(i)
      var j = 1
      while ((1 << j) < n) {
        dp(i)(j) = -1
        j += 1
      }
    }

    var j = 1
    while ((1 << j) < n) {
      (0 until n).foreach { i =>
        if (dp(i)(j - 1) != -1)
          dp(i)(j) = dp(dp(i)(j - 1))(j - 1)
      }
      j += 1
    }

    dp
  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt

    val g = new UndirectedGraph(n) with TreeFeatures

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      g.addEdge(u - 1, v - 1)
    }

    val (t, h, sz) = g.features(0)
    val dp = getLcaTable(n, t)

    val m = in.readLine().toInt

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1

      if (u == v) {
        out.println(n)
      } else {
        val w = lca(u, v, n, dp, h, t)
        val d = h(u) + h(v) - 2 * h(w)

        if (d % 2 == 1) {
          out.println(0)
        } else {

          if (h(u) == h(v)) {
            val x = ancestor(u, d / 2 - 1, dp)
            val y = ancestor(v, d / 2 - 1, dp)
            out.println(n - sz(x) - sz(y))
          } else {
            val (_, yTmp) = if (h(u) > h(v)) (v, u) else (u, v)
            val y = ancestor(yTmp, d / 2 - 1, dp)
            out.println(sz(t(y)) - sz(y))
          }

        }
      }
    }

    out.close()
  }

}

import java.io._

object Main {

  val in = new BufferedReader(new InputStreamReader(System.in))
  val out = new PrintWriter(System.out)

  val LOGN = 18

  var adj: Array[List[Int]] = null
  var t: Array[Int] = null
  var h: Array[Int] = null
  var sz: Array[Int] = null

  def dfs(u: Int, pu: Int): Unit = {
    sz(u) = 1
    adj(u).foreach { v =>
      if (v != pu) {
        t(v) = u
        h(v) = h(u) + 1
        dfs(v, u)
        sz(u) += sz(v)
      }
    }
  }

  def lca(pp: Int, qq: Int, n: Int, dp: Array[Array[Int]]) = {
    var tmp, log, i = 0
    var p = pp
    var q = qq

    if (h(p) < h(q)) {
      tmp = p
      p = q
      q = tmp
    }

    log = 1
    while ((1 << log) <= h(p)) log += 1
    log -= 1

    i = log
    while (i >= 0) {
      if (h(p) - (1 << i) >= h(q)) p = dp(p)(i)
      i -= 1
    }

    if (p == q) {
      p
    } else {
      i = log
      while (i >= 0) {
        if (dp(p)(i) != -1 && dp(p)(i) != dp(q)(i)) {
          p = dp(p)(i)
          q = dp(q)(i)
        }
        i -= 1
      }
      t(p)
    }
  }

  def goup(u: Int, d: Int, dp: Array[Array[Int]]) = {
    var x = u
    var rd = d
    var i = LOGN
    while (i >= 0) {
      if (rd - (1 << i) >= 0 && dp(x)(i) != -1) {
        rd -= (1 << i)
        x = dp(x)(i)
      }
      i -= 1
    }
    x
  }

  def main(args: Array[String]) {

    val n = in.readLine().toInt
    adj = new Array[List[Int]](n)
    t = new Array[Int](n)
    h = new Array[Int](n)
    sz = new Array[Int](n)

    (0 until n).foreach { i => adj(i) = Nil}

    (1 until n).foreach { i =>
      val Array(u, v) = in.readLine().split(" +").map(_.toInt)
      adj(u - 1) = (v - 1) :: adj(u - 1)
      adj(v - 1) = (u - 1) :: adj(v - 1)
    }

    val m = in.readLine().toInt

    t(0) = -1

    dfs(0, -1)

    val dp = Array.ofDim[Int](n, LOGN)

    (0 until n).foreach { i =>
      dp(i)(0) = t(i)
      var j = 1
      while ((1 << j) < n) {
        dp(i)(j) = -1
        j += 1
      }
    }

    var j = 1
    while ((1 << j) < n) {
      (0 until n).foreach { i =>
        if (dp(i)(j - 1) != -1)
          dp(i)(j) = dp(dp(i)(j - 1))(j - 1)
      }
      j += 1
    }

    (1 to m).foreach { _ =>
      val Array(uu, vv) = in.readLine().split(" +").map(_.toInt)

      val u = uu - 1
      val v = vv - 1
      val w = lca(u, v, n, dp)
      val d = h(u) + h(v) - 2 * h(w)

      if (u == v) {
        out.println(n)
      } else if (d % 2 == 1) {
        out.println(0)
      } else {

        if (w == v) {
          val stu = sz(u) - 1
          val stv = n - sz(goup(u, d - 1, dp)) - 1
          out.println(n - d - stu - stv)
        } else if (w == u) {
          val stu = n - sz(goup(v, d - 1, dp)) - 1
          val stv = sz(v) - 1
          out.println(n - d - stu - stv)
        } else {
          val stu = sz(u) - 1
          val stv = sz(v) - 1
          out.println(n - d - stu - stv)
        }
      }
    }

    out.close()
  }

}


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

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

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

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

  def solve(): Unit = {
    val N, M, K = ni()
    val A = na(N)
    val D = Array.ofDim[Int](N - 1)
    REP(N - 1) { i =>
      D(i) = A(i + 1) - A(i) - 1
    }
    sort(D)
    val ans = N + D.take(N - K).sum
    out.println(ans)
  }

  def sort(a: Array[Int]): Array[Int] = {
    val n = a.length
    REP(n) { i =>
      val j = scala.util.Random.nextInt(n - i) + i
      val tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
    }
    Sorting.stableSort(a)
    a
  }

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

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

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

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

  def debug(num: Long): Unit = {
    System.err.println(num)
  }

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

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

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

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

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

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

object B 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) }

  val Array(n, m, k) = readInts(3)
  val bs = readInts(n).map(_ - 1)

  val len = if (n == 1) 1
  else {
    val gaps = bs.sliding(2).map { case Array(l, r) => r - l - 1 }.toArray.sorted
    n + gaps.take(n - k).sum
  }

  println(len)
}

object B 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, m, k) = readInts(3)
  val bs = readInts(n).map(_ - 1)

  case class Gap(l: Int, r: Int) {
    def len = r - l
  }
  val gaps = (1 until n).map(i => Gap(bs(i - 1), bs(i))).sortBy(_.len)

  var len = n
  var count = n

  for (gap <- gaps) {
    if (count > k) {
      len += gap.len - 1
      count -= 1
    }
  }

  println(len)
}


import java.util.ArrayList
import java.util.StringTokenizer
//import scala.reflect.io.File
import java.io.BufferedReader
import java.io.BufferedInputStream
import java.io.InputStreamReader
import java.io.ByteArrayInputStream
import java.io.InputStream

object CF_599_B1 {   var br = createBufferedReader(); var debugV = false

    //COMMENT ME !
    //runTest(Test.sa1)
    //debugV = true
//    runFromFile("""C:\work\git\ap-codeforces\Codeforces\src\fb\aprokh\fb2018r2\jacks_candy_shop_2.txt""")

  def main(args: Array[String]): Unit = {
    //---------------------------- parameters reading
    val K = readLine.int

    for(k <- 1 to K) {
      val n = readLine.int
      val s = readLine.string
      val t = readLine.string

      var diffs = List[Int]()
      for (i <- 0 until n) {
        if (s(i) != t(i)) diffs ::= i
      }
      var res =
      if (diffs.length == 2) {
        val i1 = diffs(0)
        val i2 = diffs(1)
        if (s(i1) == s(i2) && t(i1) == t(i2)) {
          "Yes"
        } else {
          "No"
        }
      } else {
        "No"
      }

      outLn(res)
    }

//    debug("n = " + n)

    //---------------------------- parameters reading :end

    debug("================= RESULT ================== ")
    finish
  }

  //============================ service code ======================

//  var file:File = null
  val resultStr = new StringBuilder
  def outLn(str:String) = resultStr.append(str).append("\n")
  def outLn(number:Long) = resultStr.append(number+"").append("\n")
  def finish() {
//    if (file == null || !devEnv) {
      println(resultStr.toString())
      Console.flush()
//    } else {
//      file.writeAll(resultStr.toString())
//    }
  }

  def readLine() = new Line
  class Line {
    val fullLine = br.readLine()
    val tok = new StringTokenizer(fullLine, " ")
    def int = tok.nextToken().toInt
    def long = tok.nextToken().toLong
    def double = tok.nextToken().toDouble
    def string = tok.nextToken()
  }

  def createBufferedReader(inst:InputStream = System.in): BufferedReader = {
    val bis = if (inst == null) new BufferedInputStream(System.in) else new BufferedInputStream(inst);
    new BufferedReader(new InputStreamReader(bis));
  }

  def runTest(str:String) = if (devEnv) {
    br = createBufferedReader(new ByteArrayInputStream(str.trim.getBytes("ISO-8859-1")))
  }

//  def runFromFile(fileName: String) = if (devEnv) {
//    br = createBufferedReader(new FileInputStream(fileName))
//  }

  def debug(x: => String) = if (debugV && devEnv) println(x) // nullary function

  lazy val devEnv = this.getClass.getCanonicalName.contains(".")

//============================================================================================
object Test {

val sa1 = """
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca
"""

val sa2 = """
101
"""

val sa3 = """
10100
"""
 }
}

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

object Main extends App {

    val k = StdIn.readLine.toInt
    for (_ <- 0 until k) {
        val n = StdIn.readLine.toInt
        val st1 = StdIn.readLine
        val st2 = StdIn.readLine

        var diff1 = new ListBuffer[Char]
        var diff2 = new ListBuffer[Char]
        var isDiff = false
        for (
            i <- 0 until n
            if !isDiff
        ) {
            if (st1(i) != st2(i)) {
                if (diff1.length == 2) {
                    isDiff = true
                } else {
                    diff1 += st1(i)
                    diff2 += st2(i)
                }
            }
        }

        if (!isDiff) {
            if (
//                (diff1.length == 2 && diff1.intersect(diff2).length == 2) ||
                (diff1.length == 2 && diff1(0) == diff1(1) && diff2(0) == diff2(1)) ||
                (diff1.length == 0)
            ) {
                println("Yes")
            } else {
                println("No")
            }

        } else {
            println("No")
        }

    }

}

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

  override def solve(io: IO) = io.repeat {
    val _, s, t = io.read[String]

    val diffs = s.zip(t).collect({case (a, b) if a != b => (a, b)}).toList

    val ans = diffs match {
      case Nil => true
      case (a1, b1) :: (a2, b2) :: Nil => a1 == a2 && b1 == b2
      case _ => false
    }

    io.write(ans.toEnglish)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def solve(io: IO): Any
  def main(args: Array[String]): Unit = {
    val io = new IO(System.in, System.out)
    try solve(io) finally io.close()
  }
}
/****************************[Scala Collection Utils]****************************************/
object Utils {
  import scala.collection.mutable

  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]

  def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None

  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)

  implicit class GenericExtensions[A](a: A) {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }

  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](t: C[A]) {
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]

    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)

    def toMultiSet(withDefaultZero: Boolean): Map[A, Int] = {
      val freqTable = t.groupBy(identity).mapValues(_.size)
      if (withDefaultZero) freqTable.withDefaultValue(0) else freqTable
    }
  }

  implicit class PairExtensions[A, B](p: (A, B)) {
    def key = p._1
    def value = p._2
    def toSeq(implicit ev: B =:= A): Seq[A] = Seq(p.key, ev(p.value))
  }

  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C]).withDefaultValue(b().result())
  }

  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](m: C[K, V]) {
    def toMutable: mutable.Map[K, V] = mutable.Map.empty[K, V] ++ m
  }

  implicit class SetExtensions[A, C[A] <: Set[A]](s: C[A]) {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }

  implicit class IterableExtensions[A, C[A] <: Seq[A]](s: C[A]) {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
  }

  implicit class BooleanExtensions(x: Boolean) {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "Yes" else "No"
  }

  implicit class IntExtensions(val x: Int) extends AnyVal {
    @inline def isEven = x%2 == 0
    @inline def isOdd = x%2 == 1
  }

  def desc[A](implicit order: Ordering[A]): Ordering[A] = order.reverse  //e.g. students.sortBy(_.height)(desc)

  def map[K] = new {
    def to[V](default: => V): mutable.Map[K, V] = using(_ => default)
    def using[V](f: K => V): mutable.Map[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }

  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]

  def memoize[A, B](f: A => B): A => B = map[A] using f
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

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

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

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

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: CanBuild[A, C[A]]): C[A] = Iterator.fill(n)(read).to[C]
  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

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

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

  def repeat(f: => Unit): Unit = Utils.repeat(read[Int]){f; writeLine()}

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

object B {

  def main(args: Array[String]): Unit = {
    val k = scala.io.StdIn.readInt()
    val result = for (i <- 1 to k) yield test()
    println(result.mkString("\n"))
  }

  def test(): String = {
    val n = scala.io.StdIn.readInt()
    val s1 = scala.io.StdIn.readLine()
    val s2 = scala.io.StdIn.readLine()
    val diff = for (i <- 0 until n; c1 = s1(i); c2 = s2(i) if c1 != c2) yield (c1, c2)
    if (diff.length == 2 && (diff(0) == diff(1))) "Yes"
    else "No"
  }
}

import scala.io.StdIn

object StringSwap {

  def checkAllSwaps(t: Int, c: Int, s1: String, s2: String): String = {
    if (s1.length != c || s2.length != c) {
      "No"  // mismatched sizes
    } else if (s1 == s2) {
      "Yes"  // identical
    } else {
      var i1 = 0
      while (i1 < c && s1.charAt(i1) == s2.charAt(i1)) i1 = i1 + 1
      var i2 = i1 + 1
      while (i2 < c && s1.charAt(i2) == s2.charAt(i2)) i2 = i2 + 1
      var i3 = i2 + 1
      while (i3 < c && s1.charAt(i3) == s2.charAt(i3)) i3 = i3 + 1
      if (i3 < c) {
        "No"  // more than 2 positions different
      } else if (i1 < c && i2 < c) {
        if (s1.charAt(i1) == s1.charAt(i2) && s2.charAt(i1) == s2.charAt(i2)) {
          "Yes"
        } else {
          "No"
        }
      } else {
        "No" // only 1 position different
      }
    }
  }

  def main(args: Array[String]) {
    var line = StdIn.readLine()
    if (line != null) {
      val testCount = line.toInt
      line = StdIn.readLine()
      for (testNum <- 1 to testCount) {
        if (line != null) {
          val charCount = line.toInt
          line = StdIn.readLine()
          if (line != null) {
            val str1 = line
            line = StdIn.readLine()
            if (line != null) {
              val str2 = line
              val result = checkAllSwaps(testNum, charCount, str1, str2)
              println(result)
              line = StdIn.readLine
            }
          }
        }
      }
    }
  }
}

import scala.io.StdIn

object Main extends App{

    val k = StdIn.readLine.toInt
    for (i <- 0 until k) {
        val n = StdIn.readLine.toInt
        val st1 = StdIn.readLine
        val st2 = StdIn.readLine

        val dif = st1.diff(st2)
        if ( dif.length == 2 || dif.length == 0)
            println("Yes")
        else
            println("No")
    }

}

import scala.io.StdIn

object Main extends App{

    val k = StdIn.readLine.toInt
    for (i <- 0 until k) {
        val n = StdIn.readLine.toInt
        val st1 = StdIn.readLine
        val st2 = StdIn.readLine

        val dif = st1.diff(st2)
        if ( dif.length == 2 )
            println("Yes")
        else
            println("No")
    }

}

import scala.io.StdIn

object StringSwap {

  def checkSingleSwap(s1: StringBuilder, s2: StringBuilder, i: Int, j: Int): Boolean = {
    val c1 = s1.charAt(i)
    val c2 = s2.charAt(j)
    s1.setCharAt(i, c2)
    s2.setCharAt(j, c1)
    val ss1 = s1.mkString
    val ss2 = s2.mkString
    s1.setCharAt(i, c1)
    s2.setCharAt(j, c2)
    //println(s"check: $ss1, $ss2")
    ss1 == ss2
  }

  def checkAllSwaps(t: Int, c: Int, str1: String, str2: String): String = {
    //println(s"$t swap: $c, $str1, $str2")
    if (str1.length == c && str2.length == c) {
      val s1 = new StringBuilder(str1)
      val s2 = new StringBuilder(str2)
      for (i <- 0 until c) {
        for (j <- 0 until c) {
          if (checkSingleSwap(s1, s2, i, j)) {
            return "Yes"
          }
        }
      }
    }
    "No"
  }

  def main(args: Array[String]) {
    var line = StdIn.readLine()
    if (line != null) {
      val testCount = line.toInt
      line = StdIn.readLine()
      for (testNum <- 1 until testCount) {
        if (line != null) {
          val charCount = line.toInt
          line = StdIn.readLine()
          if (line != null) {
            val str1 = line
            line = StdIn.readLine()
            if (line != null) {
              val str2 = line
              val result = checkAllSwaps(testNum, charCount, str1, str2)
              println(result)
              line = StdIn.readLine
            }
          }
        }
      }
    }
  }
}

import scala.io.StdIn

object StringSwap {

  def checkAllSwaps(t: Int, c: Int, s1: String, s2: String): String = {
    if (s1.length != c || s2.length != c) {
      "No"
    } else if (s1 == s2) {
      "Yes"
    } else {
      var i1 = 0
      while (i1 < c && s1.charAt(i1) == s2.charAt(i1)) i1 = i1 + 1
      var i2 = i1 + 1
      while (i2 < c && s1.charAt(i2) == s2.charAt(i2)) i2 = i2 + 1
      if (i1 < c && i2 < c) {
        if (s1.charAt(i1) == s1.charAt(i2) && s2.charAt(i1) == s2.charAt(i2)) {
          "Yes"
        } else {
          "No"
        }
      } else {
        "No"
      }
    }
  }

  def main(args: Array[String]) {
    var line = StdIn.readLine()
    if (line != null) {
      val testCount = line.toInt
      line = StdIn.readLine()
      for (testNum <- 1 to testCount) {
        if (line != null) {
          val charCount = line.toInt
          line = StdIn.readLine()
          if (line != null) {
            val str1 = line
            line = StdIn.readLine()
            if (line != null) {
              val str2 = line
              val result = checkAllSwaps(testNum, charCount, str1, str2)
              println(result)
              line = StdIn.readLine
            }
          }
        }
      }
    }
  }
}

object B {

  def main(args: Array[String]): Unit = {
    val k = scala.io.StdIn.readInt()
    val result = for (i <- 1 to k) yield test()
    println(result.mkString("\n"))
  }

  def test(): String = {
    val n = scala.io.StdIn.readInt()
    val s1 = scala.io.StdIn.readLine()
    val s2 = scala.io.StdIn.readLine()
    val diff = for (i <- 0 until n; c1 = s1(i); c2 = s2(i) if c1 != c2) yield (c1, c2)
    if (diff.length == 2 && (diff(0) == diff(1) || diff(0) == diff(1).swap)) "Yes"
    else "No"
  }
}


object _1733A extends CodeForcesApp {
  override def solve(io: IO) = io.repeat() {
    val n, k = io.nextInt()
    val as = IndexedSeq.fill(n)(io.nextLong())
    val ans = (0 until k).map(i => (i until n by k).map(as).max)
    io.printLine(ans.sum)
  }
}
/**************************** [Ignore Template Below] **************************************/
trait CodeForcesApp {
  def debug(x: => Any): Unit = if (!sys.props.contains("ONLINE_JUDGE")) println(s">>> DEBUG: $x")
  def run(io: IO): Unit = try solve(io) finally io.close()
  def main(args: Array[String]): Unit = run(new IO(in = System.in, out = System.out))
  def solve(io: IO): Any
}
// Java I/O wrapper
import java.io._
class IO(in: BufferedReader, out: PrintWriter) extends AutoCloseable {
  def this(in: InputStream, out: OutputStream) = this(in = new BufferedReader(new InputStreamReader(in)), out = new PrintWriter(out))

  val tokens = Iterator.continually(nextLine()).takeWhile(_ != null).flatMap(_.split(' '))

  def nextLine(): String = in.readLine()
  def nextStr(): String = tokens.next()
  def nextInt(): Int = nextStr().toInt
  def nextLong(): Long = nextStr().toLong
  def nextDouble(): Double = nextStr().toDouble
  def nextN[A](r: => A): Iterator[A] = Iterator.fill(nextInt())(r)
  def repeat[U](n: Int = nextInt())(f: => U): Unit = (1 to n).foreach(_ => f)
  def query[A](query: Any)(res: => A): A = { printLine(query); out.flush(); res }

  def print(xs: Any*): this.type = { out.print(xs.mkString(" ")); this }
  def printLine(xs: Any*): this.type = { out.println(xs.mkString(" ")); this }
  def printLines(xs: Any*): this.type = { if (xs.isEmpty) out.println() else xs.foreach(out.println); this }

  override def close() = out.close()
}

object main {

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

    var t = io.StdIn.readInt()

    def loopts(ts: Int): Unit = {
        if ( ts < t ){
            var Array(n, k) = io.StdIn.readLine().split(" ").map(_.toInt)
            var a:Array[Int] = new Array[Int](n)
            a = io.StdIn.readLine().split(" ").map(_.toInt)

            def loopi( i: Int, acc: Long ):Long = {
                if ( i < k ){
                    var Array(mx, mxp) =
                    {
                        def loopj(pos: Int, mx: Int, mxp: Int):Array[Int] = {
                            if ( pos < n ){
                                if ( mx < a(pos) ){
                                    loopj(pos+k, a(pos), pos)
                                }
                                else
                                    loopj( pos+k, mx, mxp )
                            }
                            else
                                Array( mx, mxp )
                        };loopj( i, 0, i )
                    }
                    loopi(i+1, acc+mx)
                }
                else
                    acc
            }

            println( loopi(0, 0) )
            loopts(ts+1)
        }
    }

    loopts(0)
  }

}


object CF_978D {
	def check(b: Array[Int], d: Int) : Int = {
		var result = 0
		for(i <- 1 to b.length - 1)
		{
			if(math.abs(b(i) - b(0) - i*d) > 1)	return -1
			if(b(i) != b(0) + i*d)	result += 1
		}
		return result
	}
	def main(args: Array[String]) {
		val n = readInt()
		val temp_b = readLine().split(" ")
		if( n == 1 )
		{
			println(0)
			return
		}
		val b = for(x <- temp_b)	yield(x.toInt)
		var ans = n + 1
		for(i <- -1 to 1; j <- -1 to 1)
		{
			b(0) += i
			val res = check(b,b(1) + j - b(0))
			if(res != -1)
				ans = math.min(ans, (if(i == 0) 0 else 1) + res)
			b(0) -= i
		}

		if(ans > n)
			ans = -1
		println(ans)
	}
}

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val A = na(N)

    if (N == 1) {
      out.println(0)
      return
    }

    def count(head: Int, last: Int): Option[Int] = {
      val inc = (last - head) / (N - 1)
      var cnt = 0
      REP(N) { i =>
        abs(A(i) - (head + i * inc)) match {
          case 0 =>
          case 1 => cnt +=1
          case _ => return None
        }
      }
      Some(cnt)
    }

    val ans = for {
      f <- Seq(-1, 0, 1)
      l <- Seq(-1, 0, 1)
      head = A(0) + f
      last = A(N - 1) + l
      if (last - head) % (N - 1) == 0
      res <- count(head, last)
    } yield res

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

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

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

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

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

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

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


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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val cum = cumSum(A)
    var r = N - 1

    var ans = 0L
    REP(N) { l =>
      while(l < r && cum(l + 1) > cum(N) - cum(r)) r-= 1
      if (l < r && cum(l + 1) == cum(N) - cum(r)) ans = max(ans, cum(l + 1))
    }

    out.println(ans)
  }

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

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

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

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

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

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

object CF498C extends App {

  import java.io.PrintWriter
  import java.util.Scanner

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

  def solve = {
    val n = in.nextInt
    val array = new Array[Int](n).map(_ => in.nextInt)

    var a = 0
    var c = 0

    var leftSum = 0: Long
    var rightSum = 0: Long

    var max = 0: Long

    while (a+c <= n) {
      if (leftSum == rightSum)
        max = leftSum

      if (leftSum < rightSum) {
        a += 1
        if (a+c <= n)
          leftSum += array(a-1)
      }
      else {
        c += 1
        if (a + c <= n)
          rightSum += array(array.length - c)
      }
    }

    out.println(max)
  }

  solve
  out.flush
  out.close
}

object CF498C extends App {

  import java.io.PrintWriter
  import java.util.Scanner

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

  def solve = {
    val n = in.nextInt
    val array = new Array[Int](n).map(_ => in.nextInt)

    var a = 0
    var c = 0

    var leftSum = 0
    var rightSum = 0

    var max = 0

    while (a+c <= n) {
      if (leftSum == rightSum)
        max = leftSum

      if (leftSum < rightSum) {
        a += 1
        if (a+c <= n)
          leftSum += array(a-1)
      }
      else {
        c += 1
        if (a + c <= n)
          rightSum += array(array.length - c)
      }
    }

    out.println(max)
  }

  solve
  out.flush
  out.close
}


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

  @annotation.tailrec
  def gcd(a: Int, b: Int): Int =
    if (b == 0) a else gcd(b, a % b)

  val t = readInt()

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

    @annotation.tailrec
    def go(an: Seq[Int], bn: List[Int], c: Int): Seq[Int] = an match {
      case Seq() => bn
      case _ =>
        val (ak, bk, d) = an.foldLeft((List.empty[Int], List.empty[Int], 0)) {
          case ((ak, bk, d), a) =>
            val g = gcd(c, a)

            if (g > d) (bk ::: ak, a :: Nil, g)
            else if (g == d) (ak, a :: bk, d)
            else (a :: ak, bk, d)
        }

        go(ak, bn ::: bk, d)
    }

    val bn = go(an, List.empty[Int], 0)

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

//package codeforces.contests._1407

object BigVova {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt) {

      val n = io.StdIn.readInt()
      val seq = io.StdIn.readLine.split(" ").map(_.toInt)

      @scala.annotation.tailrec
      def gcd(a: Int, b: Int): Int = {
        if (b == 0) a else gcd(b, a % b)
      }

      val maximumIdx = (0 until n).maxBy(seq)

      val done: collection.mutable.Set[Int] = collection.mutable.Set(maximumIdx)

      @scala.annotation.tailrec
      def loop(accGcd: Int = seq(maximumIdx), accRes: List[Int] = seq(maximumIdx) :: Nil): List[Int] = {
        if (done.size == n) accRes.reverse
        else {
          val toRemoveIdx = (0 until n).filter(!done(_)).maxBy(i => gcd(seq(i), accGcd))
          done += toRemoveIdx
          loop(gcd(seq(toRemoveIdx), accGcd), seq(toRemoveIdx) :: accRes)
        }
      }

      println(loop().mkString(" "))
    }
  }
}

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

  def gcd(a: Int, b: Int): Int =
    if (b == 0) a else gcd(b, a % b)

  val t = readInt()

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

    val bn = Array.ofDim[Int](n)
    bn(0) = an.head

    @annotation.tailrec
    def go(i: Int, c: Int): Unit =
      if (i < n) {

        val (j, d) = ((i + 1) until n).foldLeft(i -> gcd(c, an(i))) {
          case ((j, d), k) =>
            val p = gcd(c, an(k))

            if (p > d) k -> p
            else j -> d
        }

        bn(i) = an(j)
        an(j) = an(i)

        go(i + 1, d)
      }

    go(1, an.head)

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

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

  @annotation.tailrec
  def gcd(a: Int, b: Int): Int =
    if (b == 0) a else gcd(b, a % b)

  val t = readInt()

  (0 until t).foreach { _ =>
    val n  = readInt()
    val an = readLine().split(" ").map(_.toInt)
    val bn = Array.ofDim[Int](n)

    val counts = Array.fill(an.max + 1)(0)
    an.foreach(a => counts(a) += 1)

    @annotation.tailrec
    def go(i: Int, c: Int): IndexedSeq[Int] =
      if (i == n) bn
      else {
        val (b, d) = counts.zipWithIndex.foldLeft(0 -> 0) {
          case (state, (0, a)) => state
          case (state @ (_, d), (_, a)) =>
            val g = gcd(c, a)

            if (g >= d) a -> g else state
        }

        bn(i) = b
        counts(b) -= 1

        go(i + 1, d)
      }

    go(0, an.max)

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

//package codeforces.contests._1407

object BigVova {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt) {

      val n = io.StdIn.readInt()
      val seq = io.StdIn.readLine.split(" ").map(_.toInt)

      @scala.annotation.tailrec
      def gcd(a: Int, b: Int): Int = {
        if (b == 0) a else gcd(b, a % b)
      }

      val maximum = seq.max

      @scala.annotation.tailrec
      def loop(elements: Set[Int] = seq.toSet-maximum, accRes: List[Int] = maximum :: Nil): List[Int] = {
        if (elements.isEmpty) accRes.reverse
        else {
          val accGcd = accRes.head
          val toRemove = elements.maxBy(gcd(_, accGcd))
          loop(elements - toRemove, toRemove :: accRes)
        }
      }

      println(loop().mkString(" "))
    }
  }
}

//package codeforces.contests._1407

object BigVova {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt) {

      val n = io.StdIn.readInt()
      val seq = io.StdIn.readLine.split(" ").map(_.toInt)

      @scala.annotation.tailrec
      def gcd(a: Int, b: Int): Int = {
        if (b == 0) a else gcd(b, a % b)
      }

      val maximumIdx = (0 until n).maxBy(seq)

      val done: collection.mutable.Set[Int] = collection.mutable.Set(maximumIdx)

      @scala.annotation.tailrec
      def loop(accRes: List[Int] = seq(maximumIdx) :: Nil): List[Int] = {
        if (done.size == n) accRes.reverse
        else {
          val accGcd = accRes.head
          val toRemoveIdx = (0 until n).filter(!done(_)).maxBy(i => gcd(seq(i), accGcd))
          done += toRemoveIdx
          loop(seq(toRemoveIdx) :: accRes)
        }
      }

      println(loop().mkString(" "))
    }
  }
}


object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val a: Array[Long] = in.next().split(' ').map(_.toLong).sorted
  val x = a.sum / (n - 1) + (if (a.sum % (n - 1) > 0) 1 else 0)
  println(Math.max(x, a.max))
}

import scala.collection._

object A extends App {

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

  val n = readString.toInt
  val as = readLongs(n).sorted

  def can(m: Long): Boolean = {
    var i = 0
    var games = 0L
    while (i < as.size && games < m) {
      games += (m - as(i))
      i += 1
    }
    games >= m
  }

  @annotation.tailrec
  def binSearch(low: Long, hi: Long): Long = {
    if (hi < low) low
    else {
      val mid = (low + hi) >>> 1
      if (can(mid)) binSearch(low, mid - 1)
      else binSearch(mid + 1, hi)
    }
  }

  println(binSearch(as.max, as.sum))
}

import java.util.Scanner

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

object Main {
  val scanner = new Scanner(System.in)
  import scanner._
  def main(args: Array[String]): Unit = {
      val n = readInt()
      val v = readLine().split(" ").map(_.toLong)
      val totSum: Long = v.sum
      val ans: Long =  (totSum/(n-1) + (if(totSum%(n-1) == 0)  0 else 1))
      println(ans.max(v.max))
  }
}

import java.util.Scanner

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

object Main {
  val scanner = new Scanner(System.in)
  import scanner._
  def main(args: Array[String]): Unit = {
      val n = readInt()
      val v = readLine().split(" ").map(_.toInt)
      val totSum = v.sum
      val ans =  (totSum/(n-1) + (if(totSum%(n-1) == 0)  0 else 1))
      println(ans)
  }
}

import java.util.Scanner

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

object Main {
  val scanner = new Scanner(System.in)
  import scanner._
  def main(args: Array[String]): Unit = {
      val n = readInt()
      val v = readLine().split(" ").map(_.toInt)
      val totSum = v.sum
      val ans =  (totSum/(n-1) + (if(totSum%(n-1) == 0)  0 else 1))
      println(ans.max(v.max))
  }
}


object C732 {

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

  def main(args: Array[String]): Unit = {
    val in = readLongs(3).sorted.reverse
    if(in.max - in.min <= 1) {
      println("0")
    } else {
      for(i <- in.indices)
        if(in(i) != in.max)
          in(i) += 1
      println(in.map(a => in.max-a).sum)
    }
  }

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

}

object Solution extends App {
  val in = io.Source.stdin.getLines()
  val visits = in.next().split(' ').map(_.toLong)
  val max = visits.max
  val maxCount = visits.count(_ == max)
  if (maxCount == 3)
    println(0)
  else if (maxCount == 2)
    println(max - visits.min  - 1)
  else
    println(max + max - visits.filter(_ != max).sum - 2)

}

object source{
  def main(args: Array[String]): Unit = {
    val in = io.Source.stdin.getLines()
    val visits = in.next().split(' ').map(_.toLong)
    val max = visits.max
    val maxCnt = visits.count(_ == max)
    if (maxCnt == 3) println(0)
    else if (maxCnt == 2)
      println(max - visits.min  - 1)
    else
      println(max + max - visits.filter(_ != max).sum - 2)
  }
}

import java.io._
import scala.io.Source

import scala.annotation.tailrec

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

  def processStdInOut(): Unit = {
    val src = Source.fromInputStream(System.in)
    processFromSource(src)
  }

  def processFromSource(src: Source): Unit = {
    try {
      val bw = new BufferedWriter(new OutputStreamWriter(System.out));
      try {
        val lines = src.getLines()
        processLines(lines, bw)
      } finally {
        bw.flush()
      }
    } finally {
      src.close();
    }
  }

  // Standard code above, custom code below

  def processLines(lines: Iterator[String], bw: BufferedWriter): Unit = {

    val line = lines.next()
    val strings = line.split(" ")
    val b = strings(0).toLong
    val d = strings(1).toLong
    val s = strings(2).toLong
    val soln = solve(b, d, s)
    bw.write(soln.toString)
    bw.newLine()
  }

  def solve(b: Long, d: Long, s: Long): Long = {
    // rebase time, so that at least as many of first meal as any other, and call that breakfast...
    if (d > b && d >= s) solve(d, s, b)  // Measure from before dinner on the first day
    else if (s > d && s >= b) solve(s, b, d)  // Measure from before supper on the first day
    else math.max(b - d - 1, 0) + math.max(b - s - 1, 0)
        // don't count last dinner and/or supper as missed, but beware that b, d and s could be equal
  }
}

import java.io._
import scala.io.Source

import scala.annotation.tailrec

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

  def processStdInOut(): Unit = {
    val src = Source.fromInputStream(System.in)
    processFromSource(src)
  }

  def processFromSource(src: Source): Unit = {
    try {
      val bw = new BufferedWriter(new OutputStreamWriter(System.out));
      try {
        val lines = src.getLines()
        processLines(lines, bw)
      } finally {
        bw.flush()
      }
    } finally {
      src.close();
    }
  }

  // Standard code above, custom code below

  def processLines(lines: Iterator[String], bw: BufferedWriter): Unit = {

    val line = lines.next()
    val strings = line.split(" ")
    val b = strings(0).toLong
    val d = strings(1).toLong
    val s = strings(2).toLong
    val soln = solve(b, s, d)
    bw.write(soln.toString)
    bw.newLine()
  }

  def solve(b: Long, d: Long, s: Long): Long = {
    // rebase time, so that at least as many of first meal as any other, and call that breakfast...
    if (d > b && d >= s) solve(d, s, b)  // Measure from before dinner on the first day
    else if (s > d && s > b) solve(s, b, d)  // Measure from before supper on the first day
    else {
      if (s == b) b - d  // Stay for full last day
      else math.max(b - d - 1, 0) + b - s - 1
        // don't count last dinner and supper as missed, but beware that d could equal b
    }
  }
}

import java.io._
import scala.io.Source

import scala.annotation.tailrec

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

  def processStdInOut(): Unit = {
    val src = Source.fromInputStream(System.in)
    processFromSource(src)
  }

  def processFromSource(src: Source): Unit = {
    try {
      val bw = new BufferedWriter(new OutputStreamWriter(System.out));
      try {
        val lines = src.getLines()
        processLines(lines, bw)
      } finally {
        bw.flush()
      }
    } finally {
      src.close();
    }
  }

  // Standard code above, custom code below

  def processLines(lines: Iterator[String], bw: BufferedWriter): Unit = {

    val line = lines.next()
    val strings = line.split(" ")
    val b = strings(0).toLong
    val d = strings(1).toLong
    val s = strings(2).toLong
    val soln = solve(b, d, s)
    bw.write(soln.toString)
    bw.newLine()
  }

  def solve(b: Long, d: Long, s: Long): Long = {
    // rebase time, so that at least as many of first meal as any other, and call that breakfast...
    if (d > b && d >= s) solve(d, s, b)  // Measure from before dinner on the first day
    else if (s > d && s > b) solve(s, b, d)  // Measure from before supper on the first day
    else {
      if (s == b) b - d  // Stay for full last day
      else math.max(b - d - 1, 0) + b - s - 1
        // don't count last dinner and supper as missed, but beware that d could equal b
    }
  }
}

object C732 {

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

  def main(args: Array[String]): Unit = {
    val in = readLongs(3).sorted.reverse
    in(0) -= in(2) + 1
    in(1) -= in(2) + 1
    in(2) = 0
    val res = math.max(0, in(0) - (if(in(1) > 0) in(0)-in(1) else 0))
    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))
    }
  }

}

object C732 {

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

  def main(args: Array[String]): Unit = {
    val in = readLongs(3)
    val res = math.max(0, in.max - in.min - 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))
    }
  }

}

object C732 {

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

  def main(args: Array[String]): Unit = {
    var in = readLongs(3).sorted
    in(2) -= in(0)
    in(1) -= in(0)
    val res = math.max(0, 2*in(2) - in(1) - in(0) - (if(in(1) == 0)2 else 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))
    }
  }

}