object Main extends App {
  val in = scala.io.Source.stdin.getLines.toIndexedSeq
  val h = in.head.split(' ').map(_.toInt)
  val L = h(0)
  val X = in.tail.map(_.toInt)
  //println(L, X)
  var acc = 0L
  var cl = 0
  var cr = X.size - 1
  var pos = 0
  while(cl <= cr){
    val dl = if(pos < X(cl)) X(cl) - pos else pos - X(cl)
    val dr = if(pos < X(cr)) L - X(cr) + pos else pos - X(cr)
    //println(dl,dr)
    if(dl > dr){
      //println("dl="+dl)
      acc += dl
      pos = X(cl)
      cl += 1
    }else{
      //println("dr="+dr)
      acc += dr
      pos = X(cr)
      cr -= 1
    }
  }
  println(acc)
}
