[Question]
  [
Keeping this challenge short.
You are given 4 numbers: p1, p2, p3 and p4.
The magic sum of the numbers are defined as follows:
```
magic_sum = |p1 - p2| + |p2 - p3| + |p3 - p4| + |p4 - p1|
```
You are only allowed to change one of the above integer values (p1, p2, p3 or p4). You need to change the value such that the magic sum of the values attains its minimum value.
For example:
p1, p2, p3, p4 = 17, -6, 15, 33. The value of the magic sum is 78 in this case.
You can change the -6 here to 16, and the value of the magic sum will become 36, which is the minimum attainable value.
Do keep in mind that numbers can be positive or negative integers.
This is code-golf, so least bytes in code wins. Brownie points for using a Practical Language over a Recreational language. May the 4th be with you.
To reiterate:
### Sample 1
**Input 1**
```
17 -6 15 33
```
**Output 1**
```
36
```
**Explanation 1**
> 
> The -6 can be replaced with 16 and that gives us the minimum attainable
>  magic sum possible.
> 
> 
> 
### Sample 2
**Input 2**
```
10 10 10 10
```
**Output 2**
```
0 or 2
```
either is acceptable
**Explanation 2**
> 
> The minimum attainable magic sum is 0 since the minimum sum of 4 positive
>  integers is 0. If a number has to be changed, then one of the 10's can be
>  changed to a 9 and thus yielding the output 2.
> 
> 
> 
## Sample 3
**Input 3**
```
1 2 3 4
```
**Output 3**
```
4
```
**Explanation 3**
> 
> The input by itself yields 6 as its magic sum. Changing the 4 to 1 and the minimum magic sum is attained, which is 4.
> 
> 
> 
  
[Answer]
# [Python 2](https://docs.python.org/2/), 44 bytes
```
a,b,c,d=sorted(input())
print min(c-a,d-b)*2
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzP1EnSSdZJ8W2OL@oJDVFIzOvoLREQ1OTq6AoM69EITczTyNZN1EnRTdJU8vo//9oQ3MdBV0zHQVDUx0FY@NYAA "Python 2 – Try It Online")
Sorts the input as `a,b,c,d,` in ascending order, takes the smaller of `c-a` and `d-b`, and doubles it. Why does this work?
First, note that when we change an element to maximize to total cyclic sum of distances, it's optimal (or tied for optimal) to change it to equal a neighbor, such as `17, -6, 15, 33 -> 17, 17, 15, 33`. That's because its new total distance to its left and right cyclic neighbors is at least the distance between those neighbors, so making these be equal is the best we can do. 
Now, deleting one of two adjacent copies of a number gives the same cyclic sum of distances. In the example, this is `17, 15, 33`, giving distances `2 + 18 + 16`. So instead of replacing one of the four numbers, it's equivalent to just delete it leaving three numbers, and using the sum of their cyclic distances.
Notice that with 3 numbers, the largest distance is the sum of the two smaller ones. This is because if we sort the numbers to have `a ≤ b ≤ c`, then `|a - c| = |a - b| + |b - c|`. In other words, we travel between the largest and smallest number twice, using the medium number as a pit stop one of the times. So, the sum of the three distances is just twice the distance between the minimum and maximum, so `(c-a)*2`.
So, the question is which number we delete to get the smallest distance between the minimum and maximum of the three numbers that remains. Clearly we delete either the smallest or the largest of the numbers. Calling them `a, b, c, d` in sorted order, deleting `a` leaves `d - b`, and deleting `d` leaves `c - a`, and the final result is double whichever is smaller.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
ṢŒœIṂḤ
```
[Try it online!](https://tio.run/##ASMA3P9qZWxsef//4bmixZLFk0nhuYLhuKT///8xNywtNiwxNSwzMw "Jelly – Try It Online")
A port of my [Python answer](https://codegolf.stackexchange.com/a/185189/20260).
```
     (input)        [17, -6, 15, 33]
Ṣ    sort           [-6, 15, 17, 33]
Œœ   odd-even elts  [[-6, 17], [15, 33]]
I    increments     [23, 18]
M    minimum        18
Ḥ    double         36 
```
[Answer]
# [R](https://www.r-project.org/), ~~66~~ 33 bytes
```
function(x)2*min(diff(sort(x),2))
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP0@jQtNIKzczTyMlMy1Nozi/qAQoomOkqfk/TSNZw9BcR9dMx9BUx9gYKAIA "R – Try It Online")
Much shorter with [xnor's algorithm](https://codegolf.stackexchange.com/a/185189/86301) (go read their explanation and upvote their post!).
Old version:
# [R](https://www.r-project.org/), 66 bytes
```
function(x,m=matrix(x,3,4))min(colSums(abs(diff(rbind(m,m[1,])))))
```
[Try it online!](https://tio.run/##FchJCsAgDEDR60RIF2KHlafosnThQCDQKKgFb2/tXz1@GWQHvSk0zgk6ihXXCvdJg6tSwglCfs5XKjhfITIRFM8pgqBcGm/1NwgC6AOXHfWGxszzAQ "R – Try It Online")
Takes input as a vector of 4 integers.
This works because the minimum can be attained by setting one of the numbers as equal to one of its neighbours (it is not the only way of attaining the minimum). To see why this is true, find a configuration which achieves the minimum; say we have changed \$p\_2\$. Any value of \$p\_2\$ such that \$p\_1\leq p\_2\leq p\_3\$ will give the same sum (\$|p\_1-p\_2|+|p\_2-p\_3|\$ remains constant), so we can choose \$p\_2=p\_1\$.
There are 4 ways of choosing which number we change; for each of these, we only have to compute the sum of 3 absolute differences.
The code sets the values in a \$3\times4\$ matrix, where each column represents a subset of size 3 of the 4 numbers. Copy the first row to a 4th row with `rbind` and compute the absolute differences, then take the minimum of the column sums.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~11~~ 10 bytes
```
I;SASƲ$-ƤṂ
```
[Try it online!](https://tio.run/##y0rNyan8/9/TOtgx@NgmFd1jSx7ubPr//7@huY6umY6hqY6xMQA "Jelly – Try It Online")
A monadic link that takes a list if integers as input. Should work for arbitrary list size. Works on the basis that the minimum sum can be obtained by testing removing each number from the list, calculating the magic sum and taking the minimum.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ṁ-Ƥ⁸IA§Ṃ
```
A monadic Link accepting a list of integers\* which yields an integer
\* can be any number so long as there are more than 1; using same style magic formula summing the neighbours differences wrapping around.
**[Try it online!](https://tio.run/##ASUA2v9qZWxsef//4bmBLcak4oG4SUHCp@G5gv///zE3LC02LDE1LDMz "Jelly – Try It Online")**
### How?
```
ṁ-Ƥ⁸IA§Ṃ - Link: list of integers, X       e.g. [17,-6,15,33]
 -Ƥ      - for overlapping "outfixes" of length length(X)-1:
         -                                      [[-6,15,33],[17,15,33],[17,-6,33],[17,-6,15]]
ṁ  ⁸     -   mould like X                       [[-6,15,33,-6],[17,15,33,17],[17,-6,33,17],[17,-6,15,17]]
    I    - incremental differences              [[21,18,-39],[-2,18,-16],[-23,39,-16],[-23,21,2]]
     A   - absolute (vectorises)                [[21,18,39],[2,18,16],[23,39,16],[23,21,2]]
      §  - sums                                 [78,36,78,46]
       Ṃ - minimum                              36
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-Q`](https://codegolf.meta.stackexchange.com/a/14339/), 11 bytes
```
ñÍó ®r- ÑÃn
```
Uses @xnor's algorithm, which saved me 4 bytes.
Saved 5 bytes thanks to @Shaggy
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVE&code=8c3zIK5yLSDRw24&input=WzE3LCAtNiwgMTUsIDMzXQ)
[Answer]
# [J](http://jsoftware.com/), ~~24~~ ~~20~~ ~~18~~ 17 bytes
alternative version using xnor algorithm:
```
2*[:<./2 2-/@$\:~
```
## how
Two times `2 *` the min of `[:<./` the 2nd row subtracted from the first row `[:-/` of the 2x2 matrix formed by shaping `2 2$` the input sorted down `\:~`
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/jbSirWz09I0UjHT1HVRirOr@a3JxpSZn5CsYmynYKqQpGJorxJspGJoqGBv/BwA "J – Try It Online")
## original answer: [J](http://jsoftware.com/), 24 bytes
```
[:<./1(1#.2|@-/\],{.)\.]
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/o61s9PQNNQyV9YxqHHT1Y2J1qvU0Y/Ri/2tycaUmZ@QrGJsp2CqkKRiaK8SbKRiaKhgb/wcA "J – Try It Online")
Using Nick Kennedy's idea. 
* `1(...)\.]` apply the verb in parens to all outfixes of length 1 (an outfix of length n is a list with n contiguous elements removed, so this produces every possible list with 1 elm removed)
* `(1 #. 2 |@-/\ ] , {.)` this calculates the magic sum by appending the first elm to the input `] , {.` and applying the abs difference `|@-/` to infixes of length 2 `2 ...\`, and summing the result `1 #.`.
* `[:<./` returns the min
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~11~~ 7 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
Port of [*@xnor*'s Jelly answer](https://codegolf.stackexchange.com/a/185190/52210).  
-4 bytes thanks to *@Emigna* and *@Grimy*.
```
{2ô`αß·
```
[Try it online.](https://tio.run/##yy9OTMpM/f@/2ujwloRzGw/PP7T9//9oQ3MdXTMdQ1MdY@NYAA)
**7 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage) alternative** which only works in the legacy version of 05AB1E (would require a `€` before the `¥` in the new version):
```
{2ôø¥W·
```
[Try it online.](https://tio.run/##MzBNTDJM/f@/2ujwlsM7Di0NP7T9//9oQ3MdXTMdQ1MdY@NYAA)
**Explanation:**
```
{        # Sort the (implicit) input-list
         #  i.e. [17,-6,15,33] → [-6,15,17,33]
 2ô      # Split this list into parts of size 2
         #  → [[-6,15],[17,33]]
   `     # Push both separated to the stack
    α    # And take their absolute differences
         #  → [23,18]
     ß   # Pop and push the minimum
         #  → 18
      ·  # Double it (and output implicitly as result)
         #  → 36
{        # Sort the (implicit) input-list
         #  i.e. [17,-6,15,33] → [-6,15,17,33]
 2ô      # Split this list into parts of size 2
         #  → [[-6,15],[17,33]]
   ø     # Zip/transpose, swapping rows/columns
         #  → [[-6,17],[15,33]]
    ¥    # Get the deltas/forward differences of the inner lists
         #  → [[23],[18]]
     W   # Get the flattened minimum (without popping)
         #  → 18
      ·  # Double it (and output implicitly as result)
         #  → 36
```
[Answer]
# [C++ (gcc)](https://gcc.gnu.org/)
## full program: 138 bytes
```
#include<iostream>
#include<regex>
using namespace std;int main(){int a[4];for(int&b:a)cin>>b;sort(a,a+4);cout<<min(a[2]-*a,a[3]-a[1])*2;}
```
[Try it online!](https://tio.run/##PYtLCsIwFAD3PUVAkKaaRX8KpuQiIYvX11gCzYckBUE8e6wbdzMDgyGwFbGUk3G47YuejE85arCi@qeoV/0S1Z6MW4kDq1MA1CTlhRuXiQXjavr@IchB8aeP9SHn@QEUjRNi5snHXMMVLgPl6Pc8TfZ4QHaKNUeWvWIgW0Wbjn9Kae@E3Ug7kn78Ag "C++ (gcc) – Try It Online")
## core function: 84 bytes
```
#include<regex>
int m(int*a){std::sort(a,a+4);return std::min(a[2]-*a,a[3]-a[1])*2;}
```
[Try it online!](https://tio.run/##RU5LCoMwFFybUwQKxdhm4a8FI14kZBFiKg9qIjFCQTy7TbTQzcDMvJl5aprooNS@X8Co99Lr1ulBfzoExuMxDZhJss6@b5rZOp/Ku7xVhDntF2fwoY9gUskLQbNg8lJQyXNBsoJt/1Kws3dajr9eGSIErSiJDMy0eF4JhpKXdXHyiqE5VHIuKDBdB8E/mV1828bf4gVD254/MX3gvMZl/QU "C++ (gcc) – Try It Online")
Also using the algorithm *xnor* explained in his Python 2 post.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 20 bytes
```
Ｉ⌊ＥＥθΦθ⁻κμΣＥι↔⁻λ§ι⊕μ
```
[Try it online!](https://tio.run/##LUs9C8IwEP0rN17gOpRSHZxEEDoUhI6lQ0wDHianJqn472OjffDgfZqbDuahXc6XwJLwpGPCnoX94rHXzx9fBGd2yYai1nKJeCfwSimCYdsxwfEa8d@61aROZvspeScmWG8l2RnLacMh53Gs9wTVjqBuCZpmmnL1dl8 "Charcoal – Try It Online") Link is to verbose version of code. Turns out I'm using @NickKennedy's idea. Explanation:
```
   Ｅθ                   Map over input array
     Φθ                 Filter over input array where
       ⁻κμ              Outer and inner indices differ
  Ｅ                     Map over resulting list of lists
           Ｅι           Map over remaining values in list
                §ι⊕μ    Get the next value in the list
             ↔⁻λ        Compute the absolute difference with the current value
          Σ             Take the sum of absolute differences
 ⌊                      Take the minimum sum
Ｉ                       Cast to string and implicitly print
```
[Answer]
# JavaScript (ES6), 51 bytes
Using [xnor's much more clever method](https://codegolf.stackexchange.com/a/185189/58563):
```
a=>([a,b,c,d]=a.sort((a,b)=>a-b),b+c<a+d?c-a:d-b)*2
```
[Try it online!](https://tio.run/##bYrLDoIwEADvfsUet7IllvpIjMUPIRy2LRgMoYYSf7/uyYPxMJeZefKbc1in16aXFIcyusKuxY7JU6DYO65zWjdEEcq1rL0iX4UbV/EeNF@jiH1TQlpymod6Tg8csTMXAn0mMCcCa3uldr/DQeKXPwMYAmgEKxxlKB8 "JavaScript (Node.js) – Try It Online")
---
# Original answer, 96 bytes
Takes input as an array of 4 integers. ~~Probably~~ Definitely not the shortest approach.
```
a=>a.map(m=x=>a.map((y,i)=>a[m=a.map(v=>s+=Math.abs(p-(p=v)),a[i]=x,p=a[3],s=0)|m<s?m:s,i]=y))|m
```
[Try it online!](https://tio.run/##bYpLCsIwEIb3niLLDE5La3yAOPUEniB0MdZWK00TTCktePcYEF2Iiw/@x3fnkX31aN2Q9PZSh4YCU8GpYScNTZ8oZ2whFm3oPYxU@CWdeLilfPbSJdLRCICs25ImdMRalegpg6c5@KPZe4zHDLGGyvbednXa2atspM53KJItinyDQqkSYPErZPH88kcQOQqxiqjIOgrhBQ "JavaScript (Node.js) – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 29 bytes
A port of @xnor's algorithm 
```
2{#3-#}~Min~{#4-#2}&@@Sort@#&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n27736ha2VhXubbONzOvrlrZRFfZqFbNwSE4v6jEQVntf0BRZl6JgkN6dLWhuY6umY6hqY6xcW0sF5K4gQ4MoYrrGOkY65jUxv7/DwA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Java 8](https://docs.oracle.com/javase/8/docs/), 235 bytes
A port of @xnor's [Python answer and algorithm](https://codegolf.stackexchange.com/questions/185168/make-me-a-minimum-magic-sum/185189#185189 "xnor's answer")
```
import java.util.*;interface M{static void main(String[]A){Scanner I=new Scanner(System.in);int a[]={0,0,0,0};for(int i=0;i<4;a[i++]=I.nextInt());java.util.Arrays.sort(a);System.out.print(2*(a[2]-a[0]>a[3]-a[1]?a[3]-a[1]:a[2]-a[0]));}}
```
[Try it online!](https://repl.it/repls/DelayedBaggyDaemons "Java 8 – Try It Online")
### [Java 10](https://docs.oracle.com/javase/10/), unproven, 222 bytes
With Java 10, I should be able to replace left side of Scanner declaration with `var`, although I could not compile it online and thus I can only add it as a trivia. Sorry.
```
interface M{static void main(String[]A){var I=new java.util.Scanner(System.in);int a[]={0,0,0,0};for(int i=3;i<4;a[i++]=I.nextInt());java.util.Arrays.sort(a);System.out.print(2*(a[2]-a[0]>a[3]-a[1]?a[3]-a[1]:a[2]-a[0]));}}
```
]