Back
Close

Rocket engines

Statement

 Goal

It is a public secret that you need a rocket to go to Mars, but only a few know that your rocket engines must be balanced.
Because Mars is fourth planet from the Sun, your rocket has four engines positioned in matrix 2×2. Before launch, each engine has it own default power, but you as engineer and captain of rocket can change power of engines. In one turn, you can increase by 1 every engine in arbitrary row or column, and you can perform multiple actions (there is no limit).

Imbalance of rocket is defined as difference between maximal engine power and minimal engine power.
Your goal is to find what is the minimal possible imbalance of rocket you can obtain using described action.

Example:
Given default power of each engine:
1 1
0 3
Next actions are performed: 1 × first row, 2 × first column:
1 1 -> 2 2 -> 3 2 -> 4 2
0 3 0 3 1 3 2 3
So minimal imbalance of rocket is 4 - 2 = 2.
Input
Two lines: Matrix 2×2 which represents the default power of each rocket engine.
Output
A single line: Number which represents the minimal imbalance of rocket.
Constraints
a[i][j] - Element of matrix
0 ≤ a[i][j] ≤ 10⁹
Example
Input
1 1
0 3
Output
2

Tags
MathematicsLogic

Difficulty
Medium

Test cases
Test 1 Test
Input
1 1 0 3
Output
2

Validator 1 Validator
Input
9 8 7 0
Output
3

Test 2 Test
Input
6 6 6 6
Output
0

Validator 2 Validator
Input
0 0 0 0
Output
0

Test 3 Test
Input
3 12 12 3
Output
9

Validator 3 Validator
Input
7 19 7 18
Output
1

Test 4 Test
Input
83 104 84 64
Output
21

Validator 4 Validator
Input
81 6 106 89
Output
29

Test 5 Test
Input
1120 1193 1185 1096
Output
81

Validator 5 Validator
Input
640 520 640 382
Output
69

Test 6 Test
Input
2593648 2164630 3695065 5851421
Output
1292687

Validator 6 Validator
Input
1973577 6675192 9961385 3042054
Output
5810473

Test 7 Test
Input
466643937 459483554 462812334 456924340
Output
636195

Validator 7 Validator
Input
236663937 161103138 151141275 103777855
Output
14098690

Test 8 Test
Input
257096859 257096859 45867751 257096859
Output
105614554

Validator 8 Validator
Input
329249334 363706684 341679234 39992214
Output
168072185

Test 9 Test
Input
32095790 173465731 71866326 424205669
Output
105484701

Validator 9 Validator
Input
225352867 225352867 460683533 225352867
Output
117665333

Test 10 Test
Input
208949331 489817533 208949331 489817533
Output
0

Validator 10 Validator
Input
467765325 497519746 60981056 2717947
Output
44008765

Solution language

Solution

Stub generator input