There are two friends who live in a city. The city consists of \(N\) buildings that are numbered from \(1\) to \(N\) from left to right. One day, another friend starts coming to the city. On each \(i^{th}\) day, this friend is found in a building \(A_i\). It is the duty of two friends to bring home the third friend. Now, one of the friends goes to that building and bring the third friend home. While moving from one building to another building, the two friends lose some power.
For example, if one of these friends moves from building \(b_1\)to building \(b_2\), then the loss of power is equal to \(|b_2 - b_1|\). Here, \(|c - d|\) means the absolute difference between \(c\) and \(d\).
This continued for \(D\) days. Initially, the friends are at building \(X\) and \(Y\) and when a friend brings home another friend at building \(b_i\), then he stays there.
Now, these friends need your help in calculating the total minimum power loss.
Note: You cannot skip searching for the friend on any of the days.
Input format
- The first line contains four integers \(N\), \(D\), \(X\), and \(Y\) where \(N\) is the number of buildings in the city, \(D\) is the number of fays for which the search continues, and \(X\) and \(Y\)are the initial building number of two friends.
- The second line contains \(D\) integers where the \(i^{th}\) integer \(A_i\) is the building number in which the third friend is found on the \(i^{th}\) day.
Output format
Print the single integer denoting the minimum total amount of power loss after all \(D\) days.
Constraints
6 4 2 5 1 2 6 4
5
Initially X = 2 and Y = 5
Now on Day 1 evil is in the building 1 so, the warrior X will go to building 1 with lose of 1 ( 2 -> 1 )
On Day 2 evil is in building 2 so, the warrior X will go to building 2 with lose of 1 (1 -> 2)
On Day 3 evil is in building 6 so the warrior Y will go to building 6 with lose of 1 (5 -> 6)
On Day 3 evil is in building 4 so the warrior Y will go to building 4 with lose of 2 (6 -> 4)
Total power lose is 1 + 1 + 1 + 2 = 5.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor