Олимпиадный тренинг

Задача 42971. Forest


Задача

Темы:
Misha got lost in the forest and is trying to get out of it. He walks A steps north, then B steps east, then C steps south, D steps west, then repeats his steps (again A  steps north, B steps east, C steps south, D steps to the west, etc.).
It turned out that in order to get out of the forest from its initial point, he needed to go exactly K steps in any of the four directions, that is, initially Misha is in the center of a square with a side of 2K steps. Determine how many steps Misha will do before he leaves the forest (for the first time he will be on the border of the forest).

Input
The first four lines of the input contain one positive integer A, B, C, D — the number of steps Misha takes to the north, east, south, west. The fifth line of the input data contains an integer K — the distance from Misha's initial location to the four sides of the square (the borders of the forest). All input numbers do not exceed 109.

Imprint
The program should output a single integer — the number of steps Misha will take before leaving the forest. It is guaranteed that the input data is such that Misha will ever leave the forest. 
Note that the response value can be larger than the possible value of a 32-bit integer variable, so you must use 64-bit integer data types (int64 type in Pascal, long long type in C and C++,  long type in Java, and C#).
 
Examples
# Input Output
1 1
1
2
3
3
13


Remark
The figure shows an example from the condition. Misha takes 1 step north (up), 1 step east (right), 2 steps south (down), 3 steps west (left). From Misha's initial location to side of the square — 3 steps. Misha's original location and exit point from the forest are marked with blue circles. Misha's path is marked with a yellow line. Misha will walk 13 steps before he reaches the edge of the forest for the first time.