Suppose I have four sides, and I want to make a square out of them. Each of the sides can be decreased or increased, and a price needs to be payed for such an action: increasing or decreasing a side by one unit has a cost of one unit. What is the minimum cost required to get a square out of these sides?

I saw a solution where they sorted the sides in increasing order, $a \leq b \leq c \leq d$, and then to get answer they did $(c - b) + (c - a) + (d - c)$. Why does this work?