ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

ICPC UrFU Contest Qualifier 2026

About     Problems     Submit solution     Judge status     Standings
Contest is over

B. Lack of Sleep

Time limit: 0.5 second
Memory limit: 256 MB
The dormitory manager knows about students’ sleep problems and would like the students to get enough sleep and attend classes on time. Before the students move in, he decided to conduct a survey asking what time they go to bed. In the survey, he included only four options: at 22:00, at 23:00, at 00:00, and at 01:00. The manager wants to assign students to rooms so that the total lack of sleep among all students is minimal. Each room will house exactly two students. If a student goes to bed x hours earlier than their roommate, then that student loses x hours of sleep. For example, if two students live in the same room, the first student goes to bed at 22:00 and the second at 01:00, then the first student loses 3 hours of sleep, while the second student gets enough sleep.
Help the manager and find the minimum total lack of sleep among all students if the students are assigned to rooms optimally.

Input

Four integers a22, a23, a00, and a01 are given in four lines (0 ≤ a22, a23, a00, a01 ≤ 1018) — the number of students who go to bed at 22:00, at 23:00, at 00:00, and at 01:00, respectively. The dormitory will be completely filled, so it is guaranteed that the sum a22+a23+a00+a01 is positive and even.

Output

Print the minimum total number of hours of lost sleep among all students.

Samples

inputoutput
1
0
0
1
3
124
264
362
238
0
1
1
1
1
2
Problem Author: Timofey Imaev
Problem Source: The 2026 ICPC UrFU Qualification