Problem:
Score : 900 points

Problem Statement

There are N squares in a row. The squares are numbered 1, 2, ..., N from left to right.

You have two pieces, initially placed on square A and B, respectively.
You will be asked to process Q queries of the following kind, in the order received:

Given an integer x_i, move one of the two pieces of your choice to square x_i.

Here, it takes you one second to move a piece one square.
That is, the time it takes to move a piece from square X to Y is |X-Y| seconds.

Your objective is to process all the queries in the shortest possible time.

You may only move the pieces in response to queries, and you may not move both pieces at the same time.
Also, it is not allowed to rearrange the order in which queries are given.
It is, however, allowed to have both pieces in the same square at the same time.

Constraints

1 ≤ N, Q ≤ 200,000

1 ≤ A, B ≤ N

1 ≤ x_i ≤ N

Input

Input is given from Standard Input in the following format:

N Q A B
x_1 x_2 ... x_Q

Output

Let the shortest possible time to process all the queries be X seconds. Print X.

Sample Input 1

8 3 1 8
3 5 1

Sample Output 1

7

All the queries can be processed in seven seconds, by:

moving the piece at square 1 to 3

moving the piece at square 8 to 5

moving the piece at square 3 to 1

Sample Input 2

9 2 1 9
5 1

Sample Output 2

4

The piece at square 9 should be moved first.

Sample Input 3

9 2 1 9
5 9

Sample Output 3

4

The piece at square 1 should be moved first.

Sample Input 4

11 16 8 1
1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7

Sample Output 4

21

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B, C, D);

// ===

trait Monoid {
    fn mul(&self, Self) -> Self;

    fn one() -> Self;
}

struct SegmentTree<T> {
    n: usize,
    data: Vec<T>
}

impl<T: Monoid + Clone> SegmentTree<T> {
    fn new(n: usize, initial: T) -> Self {
        let vs = (n-1).next_power_of_two() << 1;
        SegmentTree { n: n, data: vec![initial; vs] }
    }

    fn new_with(v: &Vec<T>) -> Self {
        let vs = max(4, (v.len()-1).next_power_of_two() << 1);
        let n = v.len();
        let mut data: Vec<T> = vec![T::one(); vs];

        let bottom = vs/2-1;
        for i in 0..n {
            data[bottom+i] = v[i].clone();
        }
        for i in (0..bottom).rev() {
            data[i] = data[i*2+1].mul(data[i*2+2].clone());
        }
        SegmentTree { n: v.len(), data: data }
    }

    fn change(&mut self, idx: usize, new_value: T) {
        let mut pos = self.data.len() / 2 - 1 + idx;
        self.data[pos] = new_value;
        while pos >= 1 {
            let to = (pos - 1) / 2;
            self.data[to] = self.data[to*2+1].mul(self.data[to*2+2].clone());
            pos = to;
        }
    }

    fn range(&self, l: usize, r: usize) -> T {
        self.range2(l, r, 0, 0, self.data.len() / 2)
    }

    fn range2(&self, l: usize, r: usize, idx: usize, segl: usize, segr: usize) -> T {
        if r <= segl || segr <= l {
            return T::one()
        }
        if l <= segl && segr <= r {
            return self.data[idx].clone()
        }
        let med = (segl + segr) / 2;
        self.range2(l, r, idx*2+1, segl, med).mul(self.range2(l, r, idx*2+2, med, segr))
    }
}


// ===

#[derive(Clone, Debug, Copy)]
struct I64(i64);

impl Monoid for I64 {
    fn mul(&self, other: Self) -> Self {
        if self.0 < other.0 { I64(self.0) } else { I64(other.0) }
    }

    fn one() -> Self { I64(1e15 as i64) }
}

// ===

fn abs(a: usize, b: usize) -> i64 {
    (if a < b {
        b - a
    } else {
        a - b
    }) as i64
}

fn main() {
    let (n, q, mut a, mut b): (usize, usize, usize, usize) = read();
    a -= 1;
    b -= 1;
    let mut x: Vec<usize> = readnc();
    for i in 0..q {
        x[i] -= 1;
    }
    let mut last = a;

    let mut left = SegmentTree::new(n+10, I64::one());
    let mut right = SegmentTree::new(n+10, I64::one());
    for i in 0..n {
        let lcost = i as i64;
        let rcost = (n - 1 - i) as i64;
        left.change(i, I64(abs(b, i) + lcost));
        right.change(i, I64(abs(b, i) + rcost));
    }

    let mut add_all = 0;
    let mut dp: Vec<i64> = vec![0; n];
    for i in 0..n {
        dp[i] = abs(b, i);
    }

    let mut add_all = 0;
    for i in 0..q {
        let add = abs(last, x[i]);
        let tlast = dp[last] + add_all + add;
        let mut tt = I64::one().0;

        let lcost = x[i] as i64;
        let rcost = (n - 1 - x[i]) as i64;
        tt = min(tt, left.range(x[i], n).0 - lcost + add_all);
        tt = min(tt, right.range(0, x[i]+1).0 - rcost + add_all);
        add_all += add;
        dp[last] = min(tt, tlast) - add_all;


        let lcost = last as i64;
        let rcost = (n - 1 - last) as i64;
        left.change(last, I64(dp[last] + lcost));
        right.change(last, I64(dp[last] + rcost));

        // println!("{:?} {}", dp, add_all);

        last = x[i];
    }

    let mut best = I64::one().0;
    for i in 0..n {
        best = min(best, dp[i] + add_all);
    }
    println!("{}", best);
}