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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskF solver = new TaskF();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskF {
		final long infinity = (long) (1e12);

		public void solve(int testNumber, FastScanner in, PrintWriter out) {
			int n = in.nextInt();
			int numQueries = in.nextInt();
			int a = in.nextInt() - 1;
			int b = in.nextInt() - 1;
			int[] xs = new int[numQueries];
			for (int i = 0; i < xs.length; i++) {
				xs[i] = in.nextInt() - 1;
			}
			long[] da = new long[n];
			long[] db = new long[n];
			Arrays.fill(da, infinity);
			Arrays.fill(db, infinity);
			da[a] = Math.abs(b - xs[0]);
			db[b] = Math.abs(a - xs[0]);

//		// Slow version:
//		for (int it = 1; it < xs.length; it++) {
//			int x = xs[it];
//			int p = xs[it - 1];
//			long dx = Math.abs(x - p);
//
//			long dap = infinity;
//			long dbp = infinity;
//			for (int i = 0; i <= x; i++) {
//				dap = Math.min(dap, db[i] + (x - i));
//				dbp = Math.min(dbp, da[i] + (x - i));
//			}
//			for (int i = x; i < n; i++) {
//				dap = Math.min(dap, db[i] + (i - x));
//				dbp = Math.min(dbp, da[i] + (i - x));
//			}
//
//			for (int i = 0; i < n; i++) {
//				da[i] += dx;
//				db[i] += dx;
//			}
//
//			out.println(Arrays.toString(da));
//
//			da[p] = Math.min(da[p], dap);
//			db[p] = Math.min(db[p], dbp);
//
//			out.println(Arrays.toString(da));
//		}
//		long ans = infinity;
//		for (int i = 0; i < n; i++) {
//			ans = Math.min(ans, da[i]);
//			ans = Math.min(ans, db[i]);
//		}
//		out.println(ans);

			Tree ta1 = new Tree(n);
			Tree ta2 = new Tree(n);
			Tree tb1 = new Tree(n);
			Tree tb2 = new Tree(n);
			for (int i = 0; i < n; i++) {
				ta1.set(i, da[i] + i);
				ta2.set(i, da[i] - i);
				tb1.set(i, db[i] + i);
				tb2.set(i, db[i] - i);
			}

			long sdx = 0;

			for (int it = 1; it < xs.length; it++) {
				int x = xs[it];
				int p = xs[it - 1];
				long dx = Math.abs(x - p);

				ta1.add(0, n - 1, dx);
				ta2.add(0, n - 1, dx);
				tb1.add(0, n - 1, dx);
				tb2.add(0, n - 1, dx);

				long dap = infinity;
				dap = Math.min(dap, tb2.getMin(0, x) + x - dx);
				dap = Math.min(dap, tb1.getMin(x, n - 1) - x - dx);

				long dbp = infinity;
				dbp = Math.min(dbp, ta2.getMin(0, x) + x - dx);
				dbp = Math.min(dbp, ta1.getMin(x, n - 1) - x - dx);

				dap = Math.min(dap, ta1.get(p) - p);
				dbp = Math.min(dbp, tb1.get(p) - p);

				ta1.set(p, dap + p);
				ta2.set(p, dap - p);
				tb1.set(p, dbp + p);
				tb2.set(p, dbp - p);

				sdx += dx;
			}

			long ans = infinity;
			for (int i = 0; i < n; i++) {
				ans = Math.min(ans, ta1.get(i) - i);
				ans = Math.min(ans, tb1.get(i) - i);
			}
			out.println(ans);
		}

		class Tree {
			int n;
			long[] min;
			long[] add;

			Tree(int n) {
				this.n = n;
				add = new long[4 * n];
				min = new long[4 * n];
			}

			void set(int i, long val) {
				add(i, i, val - get(i));
			}

			void add(int l, int r, long val) {
				add(0, l, r, 0, n - 1, val);
			}

			long get(int p) {
				return get(0, 0, n - 1, p);
			}

			long get(int root, int L, int R, int pos) {
				if (L == R) {
					return min[root];
				}
				push(root);
				int M = (L + R) / 2;
				if (pos <= M) {
					return get(2 * root + 1, L, M, pos);
				}
				return get(2 * root + 2, M + 1, R, pos);
			}

			long getMin(int l, int r) {
				return getMin(0, l, r, 0, n - 1);
			}

			private long getMin(int root, int l, int r, int L, int R) {
				if (l > r || l > R || L > r || L > R) {
					return infinity;
				}
				if (l == L && r == R) {
					return min[root];
				}
				push(root);
				int M = (L + R) / 2;
				long m1 = getMin(2 * root + 1, l, Math.min(M, r), L, M);
				long m2 = getMin(2 * root + 2, Math.max(M + 1, l), r, M + 1, R);
				return Math.min(m1, m2);
			}

			private void add(int root, int l, int r, int L, int R, long val) {
				if (l > r || l > R || L > r || L > R) {
					return;
				}
				if (l == L && r == R) {
					add[root] += val;
					min[root] += val;
					fix(root);
					return;
				}
				push(root);
				int M = (L + R) / 2;
				add(2 * root + 1, l, Math.min(M, r), L, M, val);
				add(2 * root + 2, Math.max(M + 1, l), r, M + 1, R, val);
				min[root] = Math.min(min[2 * root + 1], min[2 * root + 2]);
			}

			void push(int root) {
				add[2 * root + 1] += add[root];
				add[2 * root + 2] += add[root];
				min[2 * root + 1] += add[root];
				min[2 * root + 2] += add[root];
				fix(2 * root + 1);
				fix(2 * root + 2);
				add[root] = 0;
			}

			void fix(int i) {
				add[i] = Math.min(add[i], infinity);
				min[i] = Math.min(min[i], infinity);
			}

		}

	}

	static class FastScanner {
		private BufferedReader in;
		private StringTokenizer st;

		public FastScanner(InputStream stream) {
			in = new BufferedReader(new InputStreamReader(stream));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String rl = in.readLine();
					if (rl == null) {
						return null;
					}
					st = new StringTokenizer(rl);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

	}
}