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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {
	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int Q = sc.nextInt();
		int A = sc.nextInt();
		int B = sc.nextInt();
		--A;
		--B;
		int[] x = new int[Q];
		for (int i = 0; i < Q; ++i) {
			x[i] = sc.nextInt();
			--x[i];
		}
		long[] f = new long[N];
		Arrays.fill(f, Long.MAX_VALUE / 16);
		Seg sAdd = new Seg(N);
		Seg sSub = new Seg(N);
		f[B] = Math.abs(A - x[0]);
		f[A] = Math.abs(B - x[0]);
		sAdd.setVal(A, f[A] + A);
		sAdd.setVal(B, f[B] + B);
		sSub.setVal(A, f[A] - A);
		sSub.setVal(B, f[B] - B);
		long pending = 0;
		for (int i = 1; i < Q; ++i) {
			long add = Math.abs(x[i] - x[i - 1]);
			pending += add;
			long cur = f[x[i]];// pending分は除く
			cur = Math.min(cur, -x[i] + sAdd.query(x[i], N) - add);
			cur = Math.min(cur, x[i] + sSub.query(0, x[i]) - add);
			f[x[i - 1]] = cur;
			sAdd.setVal(x[i - 1], f[x[i - 1]] + x[i - 1]);
			sSub.setVal(x[i - 1], f[x[i - 1]] - x[i - 1]);
		}
		long ans = Long.MAX_VALUE / 16;
		for (int i = 0; i < N; ++i) {
			ans = Math.min(ans, f[i] + pending);
		}
		System.out.println(ans);
	}

	class Seg {
		int n;
		long[] v;

		public Seg(int n_) {
			n = 1;
			while (n < n_)
				n *= 2;
			v = new long[2 * n - 1];
			Arrays.fill(v, Long.MAX_VALUE / 16);
		}

		void setVal(int k, long val) {
			k += n - 1;
			v[k] = val;
			while (k > 0) {
				k = (k - 1) / 2;
				v[k] = Math.min(v[2 * k + 1], v[2 * k + 2]);
			}
		}

		long query(int a, int b) {
			return query(0, n, a, b, 0);
		}

		long query(int l, int r, int a, int b, int k) {
			if (r <= a || b <= l) {
				return Long.MAX_VALUE / 16;
			} else if (a <= l && r <= b) {
				return v[k];
			} else {
				long vl = query(l, (l + r) / 2, a, b, 2 * k + 1);
				long vr = query((l + r) / 2, r, a, b, 2 * k + 2);
				return Math.min(vl, vr);
			}
		}

	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}