eolymp
bolt
Try our new interface for solving problems
Problems

Two snails

Two snails

There is a rectangular field consisting of \textbf{M} rows and \textbf{N} columns. The snail has traversed this field by spiral clockwise starting from upper left corner. At that all cells has been consecutively numbered in order of traversal by numbers \textbf{1}, \textbf{2}, \textbf{3}, ... . Now the other snail is appearing on this field in a cell (\textbf{i_1}, \textbf{j_1}). It is necessary for him to reach a cell (\textbf{i_2}, \textbf{j_2}). Each second the snail can move into adjacent cell by horizontal or by vertical on condition that the number of this cell differs from the number of previous cell on at most \textbf{k}. Your task is to determine how long the travel of second snail takes. \InputFile The input file contains integer number \textbf{M}, \textbf{N}, \textbf{k}, \textbf{i_1}, \textbf{j_1}, \textbf{i_2}, \textbf{j_2} (\textbf{1} ≤ \textbf{M}, \textbf{N} ≤ \textbf{10^18}, \textbf{1} ≤ \textbf{k} ≤ \textbf{2·10^18}, \textbf{1} ≤ \textbf{i_1}, \textbf{i_2} ≤ \textbf{M}, \textbf{1} ≤ \textbf{j_1}, \textbf{j_\{2 \}}≤ \textbf{N}). \textbf{Output} In the output file write one integer number: the minimal time needed for second snail to reach required cell..
Time limit 0.5 seconds
Memory limit 64 MiB
Input example #1
3 5 1 1 2 3 4
Output example #1
6
Author В.Неспирный
Source Зимние сборы в Харькове 2010 День 1