atcoder#ARC073D. [ARC073F] Many Moves

[ARC073F] Many Moves

Score : 900900 points

Problem Statement

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

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

  • Given an integer xix_i, move one of the two pieces of your choice to square xix_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 XX to YY is XY|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

  • 1N,Q200,0001 \leq N, Q \leq 200,000
  • 1A,BN1 \leq A, B \leq N
  • 1xiN1 \leq x_i \leq N

Input

Input is given from Standard Input in the following format:

NN QQ AA BB

x1x_1 x2x_2 ... xQx_Q

Output

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

8 3 1 8
3 5 1
7

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

  • moving the piece at square 11 to 33
  • moving the piece at square 88 to 55
  • moving the piece at square 33 to 11
9 2 1 9
5 1
4

The piece at square 99 should be moved first.

9 2 1 9
5 9
4

The piece at square 11 should be moved first.

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