object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }

  val Array(a, b, c) = readLongs(3)
  val Array(x1, y1, x2, y2) = readLongs(4)

  val manhattan = Math.abs(x1 - x2) + Math.abs(y1 - y2)

  def dist(x1: Double, y1: Double, x2: Double, y2: Double): Double = {
    val x = x1 - x2
    val y = y1 - y2
    Math.sqrt(x * x + y * y)
  }

  if (a != 0 && b != 0) {
    val dx1 = - (b.toDouble * y1 + c) / a
    val dy1 = - (a.toDouble * x1 + c) / b
    val dx2 = - (b.toDouble * y2 + c) / a
    val dy2 = - (a.toDouble * x2 + c) / b

    val distXX = Math.abs(x1 - dx1) + dist(dx1, y1, dx2, y2) + Math.abs(x2 - dx2)
    val distXY = Math.abs(x1 - dx1) + dist(dx1, y1, x2, dy2) + Math.abs(y2 - dy2)
    val distYX = Math.abs(y1 - dy1) + dist(x1, dy1, dx2, y2) + Math.abs(x2 - dx2)
    val distYY = Math.abs(y1 - dy1) + dist(x1, dy1, x2, dy2) + Math.abs(y2 - dy2)

    val res = manhattan.toDouble min distXX min distXY min distYX min distYY
    println(res)
  } else {
    println(manhattan)
  }
}

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 = {
    import java.awt.geom.Point2D
    val a, b, c = ni()
    val x1, y1, x2, y2 = ni()

    val S = (x1.toDouble, y1.toDouble)
    val T = (x2.toDouble, y2.toDouble)

    def rangeX(x: Double) = min(x1, x2) <= x && x <= max(x1, x2)
    def rangeY(y: Double) = min(y1, y2) <= y && y <= max(y1, y2)

    val py1 = (-(c + b.toDouble * y1) / a, y1.toDouble)
    val py2 = (-(c + b.toDouble * y2) / a, y2.toDouble)

    val px1 = (x1.toDouble, -(c + a.toDouble * x1) / b)
    val px2 = (x2.toDouble, -(c + a.toDouble * x2) / b)

    def dist(p1: (Double, Double), p2: (Double, Double)) = {
      Point2D.distance(p1._1, p1._2, p2._1, p2._2)
    }

    def distM(p1: (Double, Double), p2: (Double, Double)) = {
      abs(p1._1 - p2._1) + abs(p1._2 - p2._2)
    }

    val points = Seq(py1, py2, px1, px2).filter(p => rangeX(p._1) && rangeY(p._2))
    val ans = if (points.length >= 2) {
      val p1 = points(0)
      val p2 = points(1)
      val d1 = distM(S, p1) + dist(p1, p2) + distM(T, p2)
      val d2 = distM(S, p2) + dist(p1, p2) + distM(T, p1)
      min(distM(S, T), min(d1, d2))
    } else {
      distM(S, T)
    }
    out.println(f"$ans%.9f")
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    import java.awt.geom.Point2D
    val a, b, c = ni()
    val x1, y1, x2, y2 = ni()

    val S = (x1.toDouble, y1.toDouble)
    val T = (x2.toDouble, y2.toDouble)

    def rangeX(x: Double) = min(x1, x2) <= x && x <= max(x1, x2)
    def rangeY(y: Double) = min(y1, y2) <= y && y <= max(y1, y2)

    val py1 = (-(c.toDouble + b * y1) / a, y1.toDouble)
    val py2 = (-(c.toDouble + b * y2) / a, y2.toDouble)

    val px1 = (x1.toDouble, -(c.toDouble + a * x1) / b)
    val px2 = (x2.toDouble, -(c.toDouble + a * x2) / b)

    def dist(p1: (Double, Double), p2: (Double, Double)) = {
      Point2D.distance(p1._1, p1._2, p2._1, p2._2)
    }

    def distM(p1: (Double, Double), p2: (Double, Double)) = {
      abs(p1._1 - p2._1) + abs(p1._2 - p2._2)
    }

    val points = Seq(py1, py2, px1, px2).filter(p => rangeX(p._1) && rangeY(p._2))
    val ans = if (points.length >= 2) {
      val p1 = points(0)
      val p2 = points(1)
      min(distM(S, p1) + dist(p1, p2) + distM(T, p2), distM(S, p2) + dist(p1, p2) + distM(T, p1))
    } else {
      distM(S, T)
    }
    out.println(f"$ans%.9f")
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    import java.awt.geom.Point2D
    val a, b, c = ni()
    val x1, y1, x2, y2 = ni()

    val r = math.atan2(b, a)
    val p = (x2 - x1).toLong * (y2 - y1)
    if (p < 0 && r > 0 && r < Math.PI / 2 || p > 0 && r < 0 && r > -Math.PI / 2) {
      // x
      val x_t = - (c.toDouble + b * y1) / a
      val x_b = - (c.toDouble + b * y2) / a

      // y
      val y_l = - (c.toDouble + a * x1) / b
      val y_r = - (c.toDouble + a * x2) / b

      val ans = if (x1 <= x_t && x_t <= x2) {
        abs(x_t - x1) + abs(y_r - y2) + Point2D.distance(x_t, y1, x2, y_r)
      } else {
        abs(y_l - y1) + abs(x2 - x_b) + Point2D.distance(x1, y_l, x_b, y2)
      }
      out.println(f"$ans%.9f")
    } else {
      val ans = abs(x2 - x1) + abs(y2 - y1)
      out.println(ans)
    }
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    import java.awt.geom.Point2D
    val a, b, c = ni()
    val x1, y1, x2, y2 = ni()

    val S = (x1.toDouble, y1.toDouble)
    val T = (x2.toDouble, y2.toDouble)

    def rangeX(x: Double) = min(x1, x2) <= x && x <= max(x1, x2)
    def rangeY(y: Double) = min(y1, y2) <= y && y <= max(y1, y2)

    val py1 = (-(c.toDouble + b * y1) / a, y1.toDouble)
    val py2 = (-(c.toDouble + b * y2) / a, y2.toDouble)

    val px1 = (x1.toDouble, -(c.toDouble + a * x1) / b)
    val px2 = (x2.toDouble, -(c.toDouble + a * x2) / b)

    def dist(p1: (Double, Double), p2: (Double, Double)) = {
      Point2D.distance(p1._1, p1._2, p2._1, p2._2)
    }

    def distM(p1: (Double, Double), p2: (Double, Double)) = {
      abs(p1._1 - p2._1) + abs(p1._2 - p2._2)
    }

    val points = Seq(py1, py2, px1, px2).filter(p => rangeX(p._1) && rangeY(p._2))
    val ans = if (points.length >= 2) {
      val p1 = points(0)
      val p2 = points(1)
      val d1 = distM(S, p1) + dist(p1, p2) + distM(T, p2)
      val d2 = distM(S, p2) + dist(p1, p2) + distM(T, p1)
      min(distM(S, T), min(d1, d2))
    } else {
      distM(S, T)
    }
    out.println(f"$ans%.9f")
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    import java.awt.geom.Point2D
    val a, b, c = ni()
    val x1, y1, x2, y2 = ni()

    val S = (x1.toDouble, y1.toDouble)
    val T = (x2.toDouble, y2.toDouble)

    val rangeX = min(x1, x2) to max(x1, x2)
    val rangeY = min(y1, y2) to max(y1, y2)

    val pt = (-(c.toDouble + b * y1) / a, y1.toDouble)
    val pb = (-(c.toDouble + b * y2) / a, y2.toDouble)

    val pl = (x1.toDouble, -(c.toDouble + a * x1) / b)
    val pr = (x2.toDouble, -(c.toDouble + a * x2) / b)

    def dist(p1: (Double, Double), p2: (Double, Double)) = {
      Point2D.distance(p1._1, p1._2, p2._1, p2._2)
    }

    val points = Seq(pt, pb, pl, pr).filter(p => rangeX.contains(p._1) && rangeY.contains(p._2))
    val ans = if (points.length == 2) {
      val p1 = points(0)
      val p2 = points(1)
      min(dist(S, p1) + dist(p1, p2) + dist(T, p2), dist(S, p2) + dist(p1, p2) + dist(T, p1))
    } else {
      dist(S, T)
    }
    out.println(f"$ans%.9f")
  }

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

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

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

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

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

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


import java.io._
import scala.io.Source
import scala.annotation.tailrec
import scala.reflect.ClassTag

object ProblemC2 {
  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

  case class Point(val x: Int, val y: Int)

  def processLines(lines: Iterator[String], bw: BufferedWriter): Unit = {
    val line = lines.next()
    val strings = line.split(' ')
    val n = strings(0).toInt
    val m = strings(1).toInt
    val k = strings(2).toInt
    val signals = Array.ofDim[Point](k)
    for (i <- 0 until k) {
      val row = lines.next()
      val coords = row.split(' ').map(_.toInt)
      signals(i) = Point(coords(0), coords(1))
    }

    val firstTimeEachSignalIsHit = solve(n, m, k, signals)
    firstTimeEachSignalIsHit.map {
      case Some(tme) => tme.toString
      case _ => "-1"
    }.foreach { str =>
      bw.write(str)
      bw.newLine()
    }
  }

  sealed trait Direction { def inverse: Direction }
  final object NorthWest extends Direction { override val inverse = SouthEast }
  final object SouthWest extends Direction { override val inverse = NorthEast }
  final object NorthEast extends Direction { override val inverse = SouthWest }
  final object SouthEast extends Direction { override val inverse = NorthWest }

  class Border[T:ClassTag](val rowCount: Int, val columnCount: Int, initialValue: T)
  {
    val pointCount = 2 * (rowCount + columnCount)
    private val points: Array[T] = Array.fill[T](pointCount)(initialValue)
    def get(edgePoint: Point): T = points(pointToIndex(edgePoint))
    def set(edgePoint: Point, value: T): Unit = { points(pointToIndex(edgePoint)) = value }
    def pointToIndex(edgePoint: Point): Int = edgePoint match {
      case Point(x, 0) => x
      case Point(x, y) if x == columnCount => columnCount + y
      case Point(x, y) if y == rowCount    => rowCount + 2 * columnCount - x
      case Point(0, y) => 2 * (rowCount + columnCount) - y
      case _ => throw new IllegalArgumentException(s"Point (${edgePoint.x}, ${edgePoint.y}) is not a border point")
    }
    def isCornerPoint(point: Point): Boolean = point match {
      case Point(0, 0) => true
      case Point(0, y) if y == rowCount => true
      case Point(x, 0) if x == columnCount => true
      case Point(x, y) if x == columnCount && y == rowCount => true
      case _ => false
    }
  }
  case class Arrow(position: Point, direction: Direction, timeSteps: Long) {
    def +(other: Arrow): Arrow = Arrow(other.position, other.direction, this.timeSteps + other.timeSteps)
  }
  case class SignalFromEdge(index: Int, arrowToSignal: Arrow)

  def solve(n: Int, m: Int, k: Int, signals: Array[Point]): Array[Option[Long]] = {
    // Take a starting point and direction, and project to an edge of the n x m box.
    // Return a new arrow with its start point as the end point of the last arrow,
    // its direction the reflection of the previous arrow, and with timeSteps
    // based on how long previous arrow travelled for to reach the start of this arrow:
    def projectToBorder(startArrow: Arrow): Arrow = startArrow match {
      case Arrow(Point(x, y), NorthEast, _) if m - y <= n - x => Arrow(Point(x + m - y, m), SouthEast, m-y)
      case Arrow(Point(x, y), NorthEast, _)               => Arrow(Point(n, y + n - x), NorthWest, n - x)
      case Arrow(Point(x, y), NorthWest, _) if x + y <= m => Arrow(Point(0, y + x), NorthEast, x)
      case Arrow(Point(x, y), NorthWest, _)               => Arrow(Point(x + y - m, m), SouthWest, m - y)
      case Arrow(Point(x, y), SouthEast, _) if x + y <= n => Arrow(Point(x + y, 0), NorthEast, y)
      case Arrow(Point(x, y), SouthEast, _)               => Arrow(Point(n, x + y - n), SouthWest, n - x)
      case Arrow(Point(x, y), SouthWest, _) if y <= x     => Arrow(Point(x - y, 0), NorthWest, y)
      case Arrow(Point(x, y), SouthWest, _)               => Arrow(Point(0, y - x), SouthEast, x)
    }

    val signalTimes = Array.fill[Option[Long]](k)(None)
    val diagonals = new Border[List[SignalFromEdge]](m, n, List.empty[SignalFromEdge])

    // Add signals along each diagonal by point on the border:
    val signalsFromEdges = for {
      i <- 0 until k
      dir <- List(NorthEast, NorthWest, SouthEast, SouthWest)
    } yield SignalFromEdge(i, projectToBorder(Arrow(signals(i), dir, 0)).copy(direction = dir.inverse))

    for (sig <- signalsFromEdges) {
      val pos = sig.arrowToSignal.position
      diagonals.set(pos, sig :: diagonals.get(pos))
    }

    @tailrec
    def projectRay(acc: Arrow): Unit = {
      // Update distances to signals along the current diagonal:
      val signalsOnDiagonal = diagonals.get(acc.position).filter(_.arrowToSignal.direction == acc.direction)
      for (sig <- signalsOnDiagonal) {
        if (signalTimes(sig.index) == None) {
          val timeToSignal = (acc + sig.arrowToSignal).timeSteps
          signalTimes(sig.index) = Some(timeToSignal)
        }
      }
      // Determine new position:
      val newAcc = acc + projectToBorder(acc)
      if (!diagonals.isCornerPoint(newAcc.position)) projectRay(newAcc)
    }

    projectRay(Arrow(Point(0, 0), NorthEast, 0))
    signalTimes
  }
}

import java.io._
import scala.io.Source
import scala.annotation.tailrec
import scala.reflect.ClassTag

object ProblemC2 {
  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

  case class Point(val x: Int, val y: Int)

  def processLines(lines: Iterator[String], bw: BufferedWriter): Unit = {
    val line = lines.next()
    val strings = line.split(' ')
    val n = strings(0).toInt
    val m = strings(1).toInt
    val k = strings(2).toInt
    val signals = Array.ofDim[Point](k)
    for (i <- 0 until k) {
      val row = lines.next()
      val coords = row.split(' ').map(_.toInt)
      signals(i) = Point(coords(0), coords(1))
    }

    val firstTimeEachSignalIsHit = solve(n, m, k, signals)
    firstTimeEachSignalIsHit.map {
      case Some(tme) => tme.toString
      case _ => "-1"
    }.foreach { str =>
      bw.write(str)
      bw.newLine()
    }
  }

  sealed trait Direction { def inverse: Direction }
  final object NorthWest extends Direction { override val inverse = SouthEast }
  final object SouthWest extends Direction { override val inverse = NorthEast }
  final object NorthEast extends Direction { override val inverse = SouthWest }
  final object SouthEast extends Direction { override val inverse = NorthWest }

  class Border[T:ClassTag](val rowCount: Int, val columnCount: Int, initialValue: T)
  {
    val pointCount = 2 * (rowCount + columnCount)
    private val points: Array[T] = Array.fill[T](pointCount)(initialValue)
    def get(edgePoint: Point): T = points(pointToIndex(edgePoint))
    def set(edgePoint: Point, value: T): Unit = { points(pointToIndex(edgePoint)) = value }
    def pointToIndex(edgePoint: Point): Int = edgePoint match {
      case Point(x, 0) => x
      case Point(x, y) if x == columnCount => columnCount + y
      case Point(x, y) if y == rowCount    => rowCount + 2 * columnCount - x
      case Point(0, y) => 2 * (rowCount + columnCount) - y
      case _ => throw new IllegalArgumentException(s"Point (${edgePoint.x}, ${edgePoint.y}) is not a border point")
    }
    def isCornerPoint(point: Point): Boolean = point match {
      case Point(0, 0) => true
      case Point(0, y) if y == rowCount => true
      case Point(x, 0) if x == columnCount => true
      case Point(x, y) if x == columnCount && y == rowCount => true
      case _ => false
    }
  }
  case class Arrow(val position: Point, val direction: Direction, val timeSteps: Long) {
    def +(other: Arrow): Arrow = Arrow(other.position, other.direction, this.timeSteps + other.timeSteps)
  }
  case class SignalFromEdge(val index: Int, arrowToSignal: Arrow)

  def solve(n: Int, m: Int, k: Int, signals: Array[Point]): Array[Option[Long]] = {
    // Take a starting point and direction, and project to an edge of the n x m box.
    // Return a new arrow with its start point as the end point of the last arrow,
    // its direction the reflection of the previous arrow, and with timeSteps
    // based on how long previous arrow travelled for to reach the start of this arrow:
    def projectToBorder(startArrow: Arrow): Arrow = startArrow match {
      case Arrow(Point(x, y), NorthEast, _) if m - y <= n - x => Arrow(Point(x + m - y, m), SouthEast, m-y)
      case Arrow(Point(x, y), NorthEast, _)               => Arrow(Point(n, y + n - x), NorthWest, n - x)
      case Arrow(Point(x, y), NorthWest, _) if x + y <= m => Arrow(Point(0, y + x), NorthEast, x)
      case Arrow(Point(x, y), NorthWest, _)               => Arrow(Point(x + y - m, m), SouthWest, m - y)
      case Arrow(Point(x, y), SouthEast, _) if x + y <= n => Arrow(Point(x + y, 0), NorthEast, y)
      case Arrow(Point(x, y), SouthEast, _)               => Arrow(Point(n, x + y - n), SouthWest, n - x)
      case Arrow(Point(x, y), SouthWest, _) if y <= x     => Arrow(Point(x - y, 0), NorthWest, y)
      case Arrow(Point(x, y), SouthWest, _)               => Arrow(Point(0, y - x), SouthEast, x)
    }

    val signalTimes = Array.fill[Option[Long]](k)(None)
    val diagonals = new Border[List[SignalFromEdge]](m, n, List.empty[SignalFromEdge])

    // Add signals along each diagonal by point on the border:
    val signalsFromEdges = for {
      i <- 0 until k
      dir <- List(NorthEast, NorthWest, SouthEast, SouthWest)
    } yield SignalFromEdge(i, projectToBorder(Arrow(signals(i), dir, 0)).copy(direction = dir.inverse))

    for (sig <- signalsFromEdges) {
      val pos = sig.arrowToSignal.position
      diagonals.set(pos, sig :: diagonals.get(pos))
    }

    @tailrec
    def projectRay(acc: Arrow): Unit = {
      // Update distances to signals along the current diagonal:
      val signalsOnDiagonal = diagonals.get(acc.position).filter(_.arrowToSignal.direction == acc.direction)
      for (sig <- signalsOnDiagonal) {
        if (signalTimes(sig.index) == None) {
          val timeToSignal = (acc + sig.arrowToSignal).timeSteps
          signalTimes(sig.index) = Some(timeToSignal)
        }
      }
      // Determine new position:
      val newAcc = acc + projectToBorder(acc)
      if (!diagonals.isCornerPoint(newAcc.position)) projectRay(newAcc)
    }

    projectRay(Arrow(Point(0, 0), NorthEast, 0))
    signalTimes
  }
}

import java.io._
import scala.io.Source
import scala.annotation.tailrec
import scala.reflect.ClassTag

object ProblemC2 {
  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

  case class Point(val x: Int, val y: Int)

  def processLines(lines: Iterator[String], bw: BufferedWriter): Unit = {
    val line = lines.next()
    val strings = line.split(' ')
    val n = strings(0).toInt
    val m = strings(1).toInt
    val k = strings(2).toInt
    val signals = Array.ofDim[Point](k)
    for (i <- 0 until k) {
      val row = lines.next()
      val coords = row.split(' ').map(_.toInt)
      signals(i) = Point(coords(0), coords(1))
    }

    val firstTimeEachSignalIsHit = solve(n, m, k, signals)
    firstTimeEachSignalIsHit.map {
      case Some(tme) => tme.toString
      case _ => "-1"
    }.foreach { str =>
      bw.write(str)
      bw.newLine()
    }
  }

  sealed trait Direction { def inverse: Direction }
  final object NorthWest extends Direction { override val inverse = SouthEast }
  final object SouthWest extends Direction { override val inverse = NorthEast }
  final object NorthEast extends Direction { override val inverse = SouthWest }
  final object SouthEast extends Direction { override val inverse = NorthWest }

  class Border[T:ClassTag](val rowCount: Int, val columnCount: Int, initialValue: T)
  {
    val pointCount = 2 * (rowCount + columnCount)
    private val points: Array[T] = Array.fill[T](pointCount)(initialValue)
    def get(edgePoint: Point): T = points(pointToIndex(edgePoint))
    def set(edgePoint: Point, value: T): Unit = { points(pointToIndex(edgePoint)) = value }
    def pointToIndex(edgePoint: Point): Int = edgePoint match {
      case Point(x, 0) => x
      case Point(x, y) if x == columnCount => rowCount + y
      case Point(x, y) if y == rowCount    => 2 * rowCount + columnCount - x
      case Point(0, y) => 2 * (rowCount + columnCount) - y
      case _ => throw new IllegalArgumentException(s"Point (${edgePoint.x}, ${edgePoint.y}) is not a border point")
    }
    def isCornerPoint(point: Point): Boolean = point match {
      case Point(0, 0) => true
      case Point(0, y) if y == rowCount => true
      case Point(x, 0) if x == columnCount => true
      case Point(x, y) if x == columnCount && y == rowCount => true
      case _ => false
    }
  }
  case class Arrow(val position: Point, val direction: Direction, val timeSteps: Long) {
    def +(other: Arrow): Arrow = Arrow(other.position, other.direction, this.timeSteps + other.timeSteps)
  }
  case class SignalFromEdge(val index: Int, arrowToSignal: Arrow)

  def solve(n: Int, m: Int, k: Int, signals: Array[Point]): Array[Option[Long]] = {
    // Take a starting point and direction, and project to an edge of the n x m box.
    // Return a new arrow with its start point as the end point of the last arrow,
    // its direction the reflection of the previous arrow, and with timeSteps
    // based on how long previous arrow travelled for to reach the start of this arrow:
    def projectToBorder(startArrow: Arrow): Arrow = startArrow match {
      case Arrow(Point(x, y), NorthEast, _) if y >= x     => Arrow(Point(x + m - y, m), SouthEast, m-y)
      case Arrow(Point(x, y), NorthEast, _)               => Arrow(Point(n, y + n - x), NorthWest, n - x)
      case Arrow(Point(x, y), NorthWest, _) if x + y <= m => Arrow(Point(0, y + x), NorthEast, x)
      case Arrow(Point(x, y), NorthWest, _)               => Arrow(Point(x + y - m, m), SouthWest, m - y)
      case Arrow(Point(x, y), SouthEast, _) if x + y <= n => Arrow(Point(x + y, 0), NorthEast, y)
      case Arrow(Point(x, y), SouthEast, _)               => Arrow(Point(n, x + y - n), SouthWest, n - x)
      case Arrow(Point(x, y), SouthWest, _) if y <= x     => Arrow(Point(x - y, 0), NorthWest, y)
      case Arrow(Point(x, y), SouthWest, _)               => Arrow(Point(0, y - x), SouthEast, x)
    }

    val signalTimes = Array.fill[Option[Long]](k)(None)
    val diagonals = new Border[List[SignalFromEdge]](m, n, List.empty[SignalFromEdge])

    // Add signals along each diagonal by point on the border:
    val signalsFromEdges = for {
      i <- 0 until k
      dir <- List(NorthEast, NorthWest, SouthEast, SouthWest)
    } yield SignalFromEdge(i, projectToBorder(Arrow(signals(i), dir, 0)).copy(direction = dir.inverse))

    for (sig <- signalsFromEdges) {
      val pos = sig.arrowToSignal.position
      diagonals.set(pos, sig :: diagonals.get(pos))
    }

    @tailrec
    def projectRay(acc: Arrow): Unit = {
      // Update distances to signals along the current diagonal:
      val signalsOnDiagonal = diagonals.get(acc.position).filter(_.arrowToSignal.direction == acc.direction)
      for (sig <- signalsOnDiagonal) {
        if (signalTimes(sig.index) == None) {
          val timeToSignal = (acc + sig.arrowToSignal).timeSteps
          signalTimes(sig.index) = Some(timeToSignal)
        }
      }
      // Determine new position:
      val newAcc = acc + projectToBorder(acc)
      if (!diagonals.isCornerPoint(newAcc.position)) projectRay(newAcc)
    }

    projectRay(Arrow(Point(0, 0), NorthEast, 0))
    signalTimes
  }
}


object CF501C extends App {
  import scala.io.StdIn

  val Array(n, m) = StdIn.readLine.split(' ').map(_.toInt)// number of songs

  val input = (0 until n).map(_ => StdIn.readLine.split(' ').map(_.toLong)).toArray.transpose

  val as = input(0) // original sizes
  val bs = input(1) // compressed sizes

  val originalSum = as.sum
  val compressedSum = bs.sum

  if (compressedSum > m) {
    println("-1")
  } else {
    val diff = as.zip(bs).map(x => x._1 - x._2).sorted.reverse
    val overflow = originalSum - m
    var sumDiff: Long = 0
    println(diff.takeWhile(x => {
      val b = sumDiff < overflow
      sumDiff += x
      b
    }).length)
  }
}

object CF501C 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() // number of songs
    val m = in.nextInt() // capacity

    val input = (0 until n).map(_ => Array(in.nextLong(), in.nextLong())).toArray.transpose

    val as = input(0) // original sizes
    val bs = input(1) // compressed sizes

    val originalSum = as.sum
    val compressedSum = bs.sum

    if (compressedSum > m) {
      out.println("-1")
    } else {
      val diff = as.zip(bs).map(x => x._1 - x._2).sorted.reverse
      val overflow = originalSum - m
      var sumDiff: Long = 0
      out.println(diff.takeWhile(x => {
        val b = sumDiff < overflow
        sumDiff += x
        b
      }).length)
    }
  }

  solve
  out.flush
  out.close
}

// https://codeforces.com/blog/entry/21074
import java.io._
import java.nio.file.{Files, Path}
import java.util.StringTokenizer

import scala.io.Codec

/**
  * Scala implementation of a faster java.util.Scanner
  * See: http://codeforces.com/blog/entry/7018
  */
class Scanner2(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
  def this(reader: BufferedReader) = this(new LineNumberReader(reader))
  def this(reader: Reader) = this(new BufferedReader(reader))
  def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
  def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
  def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
  def this(str: String) = this(new StringReader(str))

  private[this] val tokenizers = Iterator.continually(reader.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).filter(_.hasMoreTokens)
  private[this] var current: Option[StringTokenizer] = None

  @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
    current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
    current
  }

  /**
    * Unlike Java's scanner which returns till end of current line, this actually returns the next line
    * @see line() if you want the Java behaviour
    */
  def nextLine(): String = {
    current = None   // reset
    reader.readLine()
  }
  def lineNumber: Int = reader.getLineNumber
  def line(): String = tokenizer().get.nextToken("\n\r")
  def nextString(): String = next()
  def nextChar(): Char = next().ensuring(_.length == 1).head
  def nextBoolean(): Boolean = next().toBoolean
  def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
  def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
  def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
  def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
  def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
  def nextFloat(): Float = next().toFloat
  def nextDouble(): Double = next().toDouble
  def nextBigDecimal(): BigDecimal = BigDecimal(next())
  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty
  override def close() = reader.close()
}

object CF501C extends App {

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

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

  def solve = {
    val n = in.nextInt() // number of songs
    val m = in.nextInt() // capacity

    val input = (0 until n).map(_ => Array(in.nextLong(), in.nextLong())).toArray.transpose

    val as = input(0) // original sizes
    val bs = input(1) // compressed sizes

    val originalSum = as.sum
    val compressedSum = bs.sum

    if (compressedSum > m) {
      out.println("-1")
    } else {
      val diff = as.zip(bs).map(x => x._1 - x._2).sorted.reverse
      val overflow = originalSum - m
      var sumDiff: Long = 0
      out.println(diff.takeWhile(x => {
        val b = sumDiff < overflow
        sumDiff += x
        b
      }).length)
    }
  }

  solve
  out.flush
  out.close
}

// https://codeforces.com/blog/entry/21074
import java.io._
import java.nio.file.{Files, Path}
import java.util.StringTokenizer

import scala.io.Codec

/**
  * Scala implementation of a faster java.util.Scanner
  * See: http://codeforces.com/blog/entry/7018
  */
class Scanner2(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
  def this(reader: BufferedReader) = this(new LineNumberReader(reader))
  def this(reader: Reader) = this(new BufferedReader(reader))
  def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
  def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
  def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
  def this(str: String) = this(new StringReader(str))

  private[this] val tokenizers = Iterator.continually(reader.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).filter(_.hasMoreTokens)
  private[this] var current: Option[StringTokenizer] = None

  @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
    current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
    current
  }

  /**
    * Unlike Java's scanner which returns till end of current line, this actually returns the next line
    * @see line() if you want the Java behaviour
    */
  def nextLine(): String = {
    current = None   // reset
    reader.readLine()
  }
  def lineNumber: Int = reader.getLineNumber
  def line(): String = tokenizer().get.nextToken("\n\r")
  def nextString(): String = next()
  def nextChar(): Char = next().ensuring(_.length == 1).head
  def nextBoolean(): Boolean = next().toBoolean
  def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
  def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
  def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
  def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
  def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
  def nextFloat(): Float = next().toFloat
  def nextDouble(): Double = next().toDouble
  def nextBigDecimal(): BigDecimal = BigDecimal(next())
  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty
  override def close() = reader.close()
}

object Main {
  import java.io._

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

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

  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val N, M = sc.nextInt()
    val files = Array.ofDim[(Long, Long)](N)
    rep(N) { i =>
      val a, b = sc.nextLong()
      files(i) = (a, b)
    }

    var SUM = files.sumBy(_._1)
    Sorting.quickSort(files)(Ordering.by[(Long, Long), Long](x => x._2 - x._1))
    var i = 0
    while(SUM > M && i < N) {
      val (a, b) = files(i)
      SUM -= a - b
      i += 1
    }

    if (SUM > M) out.println(-1)
    else out.println(i)
  }

  def main(args: Array[String]): Unit = {
    solve()
    out.flush()
  }

  class InputReader(val stream: InputStream) {

    import java.util.StringTokenizer

    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)
  }
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

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

  implicit class ArrayOpts[A](val as: Array[A]) extends AnyVal {
    // todo Orderingだとboxing発生するので自作Orderを用意したい
    def maxByOpt[B: Ordering](f: A => B): Option[A] = {
      if (as.nonEmpty) Some(as.maxBy(f)) else None
    }

    def grpBy[K](f: A => K): mutable.Map[K, ArrayBuffer[A]] = {
      val map = mutable.Map.empty[K, ArrayBuffer[A]]
      rep(as.length)(i => map.getOrElseUpdate(f(as(i)), ArrayBuffer()) += as(i))
      map
    }

    def sumBy[B](f: A => B)(implicit num: Numeric[B]): B = {
      var sum = num.zero
      rep(as.length)(i => sum = num.plus(sum, f(as(i))))
      sum
    }

    def minByEx[B](f: A => B, ixRange: Range = as.indices)(implicit cmp: Ordering[B]): (A, B) = {
      limit(f, ixRange)(cmp.lt)
    }

    def maxByEx[B](f: A => B, ixRange: Range = as.indices)(implicit cmp: Ordering[B]): (A, B) = {
      limit(f, ixRange)(cmp.gt)
    }

    private def limit[B](f: A => B, ixRange: Range)(cmp: (B, B) => Boolean): (A, B) = {
      var limA = as(ixRange.head)
      var limB = f(limA)

      for (i <- ixRange.tail) {
        val a = as(i)
        val b = f(a)
        if (cmp(b, limB)) {
          limA = a
          limB = b
        }
      }
      (limA, limB)
    }
  }

  implicit class IterableOpts[A](val as: Iterable[A]) extends AnyVal {
    def sumBy[B](f: A => B)(implicit num: Numeric[B]): B = {
      as.foldLeft(num.zero)((acc, a) => num.plus(acc, f(a)))
    }
  }
}

object CF501C 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 // number of songs
    val m = in.nextInt // capacity

    val input = (0 until n).map(_ => Array(in.nextLong, in.nextLong)).toArray.transpose
    val as = input(0) // original sizes
    val bs = input(1) // compressed sizes

    val originalSum = as.sum
    val compressedSum = bs.sum

    if (compressedSum > m) {
      out.println("-1")
    } else {
      val diff = as.zip(bs).map(x => x._1 - x._2).sorted.reverse
      val overflow = originalSum - m
      var sumDiff: Long = 0
      out.println(diff.takeWhile(x => {
        sumDiff += x

        sumDiff < overflow
      }).length)
    }
  }

  solve
  out.flush
  out.close
}

object CF501C 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 // number of songs
    val m = in.nextInt // capacity

    val input = (0 until n).map(_ => Array(in.nextLong, in.nextLong)).toArray.transpose
    val as = input(0) // original sizes
    val bs = input(1) // compressed sizes

    val originalSum = as.sum
    val compressedSum = bs.sum

    if (compressedSum > m) {
      out.println("-1")
    } else {
      val diff = as.zip(bs).map(x => x._1 - x._2).sorted
      val overflow = originalSum - m
      var sumDiff: Long = 0
      out.println(diff.takeWhile(x => {
        sumDiff += x

        sumDiff < overflow
      }).length)
    }
  }

  solve
  out.flush
  out.close
}


 import scala.io.StdIn

 object HelloWorld extends App {
    var t = StdIn.readInt();
    val range = 1 to t
    for (num <- range){
        var ip = StdIn.readLine().split(" ");
        val a = ip(0).toLong;
        val b = ip(1).toLong;
        val c = ip(2).toLong;
        val x:Long = c*b+a
        println(s"$x $b $c")

    }
 }

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

object Main {

  def main(args: Array[String]): Unit = {
    val reader = new InputReader()
    val t = reader.nextInt()
    (1 to t).foreach { i =>
      val a = reader.nextInt()
      val b = reader.nextInt()
      val c = reader.nextInt()

      System.out.println((a + b + c) + " " + (b + c) + " " + c)
    }

  }

  class InputReader() {
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
    var line: Array[String] = null
    var cur: Int = 0

    def nextString(): String = {
      while (line == null || cur >= line.length) {
        cur = 0
        line = reader.readLine().split(""" """)
      }
      cur += 1
      line(cur - 1)
    }

    def nextInt(): Int = {
      nextString().toInt
    }

    def nextLong(): Int = {
      nextString().toInt
    }

  }

}

 import scala.io.StdIn
 object HelloWorld extends App {
    var t = StdIn.readInt();
    val range = 1 to t
    for (num <- range){
        var ip = StdIn.readLine().split(" ");
        val a = ip(0).toInt;
        val b = ip(1).toInt;
        val c = ip(2).toInt;
        val x :Long = c*b+a;
        println(s"${x} ${b} ${c}")

    }
 }

 import scala.io.StdIn
 object HelloWorld extends App {
    var t = StdIn.readInt();
    val range = 1 to t
    for (num <- range){
        var ip = StdIn.readLine().split(" ");
        val a = ip(0).toInt;
        val b = ip(1).toInt;
        val c = ip(2).toInt;
        println(s"${c*b+a} ${b} ${c}")

    }
 }


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

    val a = (1 to m).map(_ => readLine().split(" ").map(_.toInt))

    val s = Set() ++ (1 to n) -- a.flatten
    val u = s.toSeq.apply(0)

    println(n - 1)
    for (i <- 1 to n if i != u) println(i + " " + u)
  }
}

import scala.util.control.Breaks

object RoadConstruction {
  def main(args: Array[String]): Unit = {
    val in = scala.io.StdIn
    var arr = in.readLine().split(" ").map(_.toInt)
    val n = arr(0)
    val m = arr(1)
    val forbiddenPathVertices=Array.fill[Boolean](n)(false)
    for(i<- 0 until m){
      arr = in.readLine().split(" ").map(_.toInt-1)
      forbiddenPathVertices(arr(0))=true
      forbiddenPathVertices(arr(1))=true
    }
    var hubVertex= -1
    Breaks.breakable {
      for (i <- 0 until n) {
        if (!forbiddenPathVertices(i)) {
          hubVertex = i
          Breaks.break()
        }
      }
    }
    println(n-1)
    for (i <- 0 until n if(i!=hubVertex)) {
      println((i+1)+" "+(hubVertex+1))
    }
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, m) = in.next().split(" ").map(_.toInt)
  val data = Range(0, m).flatMap{_ =>
    in.next().split(" ").map(_.toInt)
  }.toSet
  val el = Range(1, n + 1).find(x => !data.contains(x)).get
  println(n - 1)
  Range(1, n + 1).foreach { i =>
    if (i != el)
      println(s"$i $el")
  }
}

object tmp extends App {

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

  class MyScanner(br: BufferedReader) {
    var st = new StringTokenizer("")

    def this(is: InputStream) = this(new BufferedReader(new InputStreamReader(is)))

    private def next() = {

      while (!st.hasMoreElements) {
        st = new StringTokenizer(br.readLine())
      }
      st.nextToken()
    }

    def nextInt() = java.lang.Integer.parseInt(next())

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

    def nextLine() = br.readLine()
  }

  val sc = new MyScanner(System.in)
  val n = sc.nextInt()
  val m = sc.nextInt()
  val vertices = Array.fill(n)(true)
  for (_ <- 0 until m) {
    vertices(sc.nextInt() - 1) = false
    vertices(sc.nextInt() - 1) = false
  }
  val center = vertices.zipWithIndex.find(x => x._1).get._2 + 1
  println(n - 1)
  for (i <- 1 to n) {
    if (i != center) {
      println(s"$i $center")
    }
  }
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Unit = {
    val N, K = sc.nextInt
    out.println(N - 1)
    val E0 = List.fill(N)(new ListBuffer[Int])

    for (_ <- 0 until K) {
      val u, v = sc.nextInt - 1
      E0(u) += v
      E0(v) += u
    }

    var root = -1
    breakable {
      for (i <- 0 until N) {
        if (E0(i).isEmpty) {
          root = i
          break
        }
      }
    }

    0 until N foreach { i =>
      if (i != root) {
        out.println(List(root, i).map(_ + 1).mkString(" "))
      }
    }
  }

  solve
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Unit = {
    val N, K = sc.nextInt
    val E0 = Array.fill(N)(new ListBuffer[Int])

    for (_ <- 0 until K) {
      val u, v = sc.nextInt - 1
      E0(u) += v
      E0(v) += u
    }

    val E: Array[Set[Int]] = E0.map(_.toSet)

    val c0: Set[Int] = List.range(1, N).toSet -- E(0)
    c0.foreach { i =>
      out.println(List(1, i + 1).mkString(" "))
    }
    val c1: Set[Int] = E(0)
    c1 foreach { i =>
      c0.find { j =>
        ! E(i).contains(j)
      }.map { j =>
        val res = List(i, j).map(_ + 1).mkString(" ")
        out.println(res)
      }
    }
  }

  solve
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Int = sc.nextInt - 1

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Unit = {
    val N, K = sc.nextInt
    out.println(N - 1)
    val E0 = Array.fill(N)(new ListBuffer[Int])

    for (_ <- 0 until K) {
      val u, v = sc.nextInt - 1
      E0(u) += v
      E0(v) += u
    }

    val E: Array[Set[Int]] = E0.map(_.toSet)

    val c0: Set[Int] = List.range(1, N).toSet -- E(0)
    c0.foreach { i =>
      out.println(List(1, i + 1).mkString(" "))
    }
    val c1: Set[Int] = E(0)
    c1 foreach { i =>
      c0.find { j =>
        ! E(i).contains(j)
      }.map { j =>
        val res = List(i, j).map(_ + 1).mkString(" ")
        out.println(res)
      }
    }
  }

  solve
  out.close
}

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

    val a = (1 to m).map(_ => readLine().split(" ").map(_.toInt))

    val s = Set() ++ (1 to n) -- a.flatten
    val u = s.toSeq.apply(0)

    for (i <- 1 to n if i != u) println(i + " " + u)
  }
}


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

  for (_ <- 1 to nextInt) {
    val n = nextInt
    val h = n / 2
    if (h % 2 == 1) {
      out.println("NO")
    } else {
      val l = Array.tabulate(h)(i => 2 + 2 * i)
      val r = Array.tabulate(h - 1)(i => 1 + 2 * i)
      val res = l ++ r ++ Array(l.sum - r.sum)
      out.println("YES")
      out.println(res.mkString(" "))
    }
  }

  out.flush()

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

import scala.io.StdIn

object SolutionB extends App {
  val T = StdIn.readInt()
  for (t <- 1 to T) {
    val n = StdIn.readInt();
    val l = n / 2
    if (l % 2 == 1) {
      println("NO")
    } else {
      println("YES")
      val first = Array.ofDim[Int](l)
      for (idx <- 1 to l) {
        first(idx-1) = idx * 4
      }
      val second = Array.ofDim[Int](l)
      for (idx <- 1 to l) {
        second(idx-1) = if (idx % 2 == 0) {
          first(idx-1) + 1
        } else {
          first(idx-1) - 1
        }
      }
      print(first.mkString(" "))
      print(" ")
      println(second.mkString(" "))
    }
  }
}

import scala.io.StdIn._

object Main2 extends App {
  val testCaseNumber = readInt()
  1.to(testCaseNumber).foreach{ _ =>
    val n = readInt()
    if (n/2 % 2 == 1) {
      println("NO")
    } else {
      println("YES")
      val (even, odd) = 1.to(n).partition(_ % 2 == 0)
      println((even ++ odd.take(n/2 - 1) :+ (odd.last + n/2)).mkString(" "))
    }
  }
}

import scala.io.StdIn

object SolutionB extends App {
  val T = StdIn.readInt()
  for (t <- 1 to T) {
    val n = StdIn.readInt();
    val l = n / 2
    if (l % 2 == 1) {
      println("NO")
    } else {
      println("YES")
      val first = Array.ofDim[Int](l)
      for (idx <- 1 to l) {
        first(idx-1) = idx * 4
      }
      val second = Array.ofDim[Int](l)
      for (idx <- 1 to l) {
        second(idx-1) = if (idx % 2 == 0) {
          first(idx-1) + 1
        } else {
          first(idx-1) - 1
        }
      }
      print(first.mkString(" "))
      print(" ")
      print(second.mkString(" "))
    }
  }
}

import scala.io.StdIn._

object Main2 extends App {
  val testCaseNumber = readInt()
  1.to(testCaseNumber).foreach{ _ =>
    val n = readInt()
    if (n/2 % 2 == 1) {
      println("NO")
    } else {
      println("YES")
      val (even, odd) = 1.to(n).partition(_ % 2 == 0)
      println((even ++ odd).mkString(" "))
    }
  }
}


import java.util.Scanner

import scala.io.StdIn

object Ctask557 extends App {

  var line = new Scanner(StdIn.readLine())
  val n = line.nextInt()
  val k = line.nextInt()

  val x = StdIn.readLine().split(' ').map(_.toInt)
  var blocks = Array.fill(n){0}

  var result = 1 + (n - 1) * 3
  for (i <- 0 until k) {
    if (blocks(x(i) - 1) == 0) {
      blocks(x(i) - 1) += 1
      result -= 1
    }
    if (x(i)-2 >= 0 && blocks(x(i) - 2) >= 1 && (blocks(x(i) -1) == 1 || blocks(x(i) -1) == 11)) {
      result -= 1
      blocks(x(i) -1) += 1
    }
    if (x(i) < n && blocks(x(i)) >= 1 && (blocks(x(i) - 1) == 1 || blocks(x(i) - 1) == 2)) {
      result -= 1
      blocks(x(i) -1) += 10
    }
  }
  if (result < 0) {
    result = 0
  }
  println(result)
}

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

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

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, K = ni()
    val A = na(K)
    val C = Array.fill[java.util.TreeSet[Integer]](N + 1)(new java.util.TreeSet)
    REP(K) { i =>
      C(A(i)).add(i)
    }

    var ans = 0
    TO(1, N) { b =>
      val cnt =  if (C(b).isEmpty) {
        var v = 1
        if (b < N) v += 1
        if (b > 1) v += 1
        v
      } else {
        val i = C(b).last
        var v = 0
        if (b < N && C(b + 1).lower(i) == null) v += 1
        if (b > 1 && C(b - 1).lower(i) == null) v += 1
        v
      }
      ans += cnt
    }
    out.println(ans)
  }
}

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

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

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, K = ni()
    val A = na(K)
    val C = Array.fill[java.util.TreeSet[Integer]](N + 1)(new java.util.TreeSet)
    REP(K) { i =>
      C(A(i)).add(i)
    }

    var ans = 0
    TO(1, N) { b =>
      val cnt =  if (C(b).isEmpty) {
        if (b == 1 || b == N) 2 else 3
      } else if (C(b).size == 1) {
        val i = C(b).first()
        var v = 0
        if (b < N && C(b + 1).lower(i) == null) v += 1
        if (b > 1 && C(b - 1).lower(i) == null) v += 1
        v
      } else {
        0
      }
      ans += cnt
    }
    out.println(ans)
  }
}

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

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

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, K = ni()
    val A = na(K)
    val C = Array.fill[java.util.TreeSet[Integer]](N + 1)(new java.util.TreeSet)
    REP(K) { i =>
      C(A(i)).add(i)
    }

    var ans = 0
    TO(1, N) { b =>
      val cnt =  if (C(b).isEmpty) {
        var v = 1
        if (b < N) v += 1
        if (b > 1) v += 1
        v
      } else if (C(b).size == 1) {
        val i = C(b).first()
        var v = 0
        if (b < N && C(b + 1).lower(i) == null) v += 1
        if (b > 1 && C(b - 1).lower(i) == null) v += 1
        v
      } else {
        0
      }
      ans += cnt
    }
    out.println(ans)
  }
}

import java.util.Scanner

import scala.io.StdIn

object Ctask557 extends App {

  var line = new Scanner(StdIn.readLine())
  val n = line.nextInt()
  val k = line.nextInt()

  val x = StdIn.readLine().split(' ').map(_.toInt)
  var blocks = Array.fill(n){0}

  var result = 1 + (n - 1) * 3
  for (i <- 0 until k) {
    blocks(x(i)-1) += 1
    if (blocks(x(i) - 1) == 1) {
      result -= 1
    }
    if (x(i)-2 >= 0 && blocks(x(i) - 2) >= 1 && (blocks(x(i) -1) == 1 || blocks(x(i) -1) == 11)) {
      result -= 1
      blocks(x(i) -1) += 1
    }
    if (x(i) < n && blocks(x(i)) >= 1 && blocks(x(i) - 1) != 11 && blocks(x(i) - 1) != 12) {
      result -= 1
      blocks(x(i) -1) += 10
    }
  }
  if (result < 0) {
    result = 0
  }
  println(result)
}

import java.util.Scanner

import scala.io.StdIn

object Ctask557 extends App {

  var line = new Scanner(StdIn.readLine())
  val n = line.nextInt()
  val k = line.nextInt()

  val x = StdIn.readLine().split(' ').map(_.toInt)
  var blocks = Array.fill(n){0}

  var result = 1 + (n - 1) * 3
  for (i <- 0 until k) {
    blocks(x(i)-1) += 1
    if (blocks(x(i) - 1) == 1) {
      result -= 1
    }
    if (x(i)-2 >= 0 && blocks(x(i) - 2) >= 1 && (blocks(x(i) -1) != 2 || blocks(x(i) -1) == 11)) {
      result -= 1
      blocks(x(i) -1) += 1
    }
    if (x(i) < n && blocks(x(i)) >= 1 && blocks(x(i) - 1) != 11 && blocks(x(i) - 1) != 12) {
      result -= 1
      blocks(x(i) -1) += 10
    }
  }
  if (result < 0) {
    result = 0
  }
  println(result)
}

import java.util.Scanner

import scala.io.StdIn

object Ctask557 extends App {

  var line = new Scanner(StdIn.readLine())
  val n = line.nextInt()
  val k = line.nextInt()

  val x = StdIn.readLine().split(' ').map(_.toInt)
  var blocks = Array.fill(n){0}

  var result = 1 + (n - 1) * 3
  for (i <- 0 until k) {
    if (blocks(x(i) - 1) == 0) {
      blocks(x(i) - 1) += 1
    }
    if (blocks(x(i) - 1) == 1) {
      result -= 1
    }
    if (x(i)-2 >= 0 && blocks(x(i) - 2) >= 1 && (blocks(x(i) -1) == 1 || blocks(x(i) -1) == 11)) {
      result -= 1
      blocks(x(i) -1) += 1
    }
    if (x(i) < n && blocks(x(i)) >= 1 && (blocks(x(i) - 1) == 1 || blocks(x(i) - 1) == 2)) {
      result -= 1
      blocks(x(i) -1) += 10
    }
  }
  if (result < 0) {
    result = 0
  }
  println(result)
}


import scala.io.StdIn

object WorldCup {

  def main(args: Array[String]): Unit = {
    StdIn.readLine()
    val as: Seq[Int] = StdIn.readLine().split(" ").map(_.toInt)
    println(solve(as))
  }

  private def solve(as: Seq[Int]): Int = {
    as.zipWithIndex.map(tupel => {
      val value = tupel._1
      val index = tupel._2
      val round = Math.ceil((value - index) / as.length.toDouble).toInt + 1
      (round * as.length + index, index)
    }).minBy(tupel => tupel._1)._2 + 1
  }

  private def absMod(p: Int, q: Int): Int = {
    val r = p % q
    if (r < 0) r + q
    else r
  }
}

import scala.io.StdIn

object B {

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

    var index = 0

    val counts = people.map {count =>
      val i = count - index
      val a = i / entrances
      val r = i % entrances

      index += 1
      if (r > 0) a+1 else a
    }

    val min = counts.min
    val result = counts.indexOf(min) + 1

    println(result)
  }

}

object Main{
  def main(args: Array[String]) {
    val n = readInt
    val a: Array[Int] = readLine.split(" ").map(_.toInt)
    val min = a.min
    val p = min % n
    for(i <- 0 to (n - 1)){
      if(a((p + i) % n) - min <= i){
        println(((p + i) % n) + 1)
        return
      }
    }
  }
}

import scala.io.StdIn._
import scala.math._
object Task2 extends App {
  def calc(x: Long, y: Long) = x + y.toDouble / pow(10, y.toString.length)
  val n = readLong;
  val qs = readLine.split(' ').map(_.toLong)
  qs.zipWithIndex
    .map { case (q, i) => (ceil((q-i).toDouble / n), i + 1) }
    .minBy(_._1) match { case (_, i) => println (i) }
}

import scala.io.StdIn._

object Task2 extends App {
  val n = readInt
  val qs = readLine.split(' ').map(_.toInt)
  Iterator.continually(qs)
    .flatten.zipWithIndex
    .map { case (q, i) => (q - i, i + 1) }.find(_._1 <= 0) match {
    case Some((_, i)) => println(i % n)
    case None => println(-1)
  }
}


import scala.io.StdIn

object Main {
  def main(args: Array[String]): Unit = {
    val t = StdIn.readInt
    for (_ <- 1 to t) {
      val x = StdIn.readLine().split(' ').map(_.toInt)
      solve(x(0), x(1))
    }
  }

  def solve(l: Int, r: Int): Unit = {
    println(s"${l} ${l * 2}")
  }
}

import scala.io.StdIn

object kimollg extends App {
  //println("Zdorova!")
  val n = scala.io.StdIn.readInt()
  for (i <- 0 until n){
    val inps = StdIn.readLine().split(" ").map(_.toInt)
    println(inps(0)+" "+inps(0)*2)
  }

}

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 = {
    REP(ni()) { _ =>
      val L, R = ni()
      val l = L
      val r = l * 2
      out.println(s"$l $r")
    }

  }

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

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

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

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

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

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

  def debug(as: Array[Int], delimiter: String = " "): Unit = {
    debug(as.mkString(delimiter))
  }

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

import scala.io.StdIn

object FindDivisible extends App {
  val t = StdIn.readLine().toInt

  for (_ <- 1 to t) {
    val lr = StdIn.readLine().split(" ").map(_.toInt)

    println(s"${lr(0)} ${lr(0) * 2}")
  }
}

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 = {
    REP(ni()) { _ =>
      val L, R = ni()
      if (abs(L - R) <= 1) out.println(s"$L $R")
      else {
        val l = ((L - 1) / 2 + 1) * 2
        val r = R / 2 * 2
        if (l >= r) {
          out.println(s"$L $R")
        } else {
          out.println(s"$l $r")
        }
      }
    }

  }

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

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

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

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

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

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

  def debug(as: Array[Int], delimiter: String = " "): Unit = {
    debug(as.mkString(delimiter))
  }

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