eolymp
bolt
Try our new interface for solving problems

Dice

At the checkered field size \textbf{m}×\textbf{n} in the lower left corner is the dice. In one move, it can be rolled to the cage to the right or up. The path cost is the sum of the numbers on the top of the cube in all cells of the path (including the start and end). Find the minimum cost path to the top right corner. \InputFile The first line of two positive integers \textbf{m} and \textbf{n} (\textbf{1} ≤ \textbf{m}, \textbf{n} ≤ \textbf{1000}) - the width and height of the board. The second row has three numbers from \textbf{1} to \textbf{6} - the number on the top, left, and the front face of the cube, respectively. Sum of numbers on opposite faces of the dice is \textbf{7}, all the numbers on the faces of the cube is different. \OutputFile Bring out the shortest possible path cost.
Time limit 1 second
Memory limit 256 MiB
Input example #1
6 1
1 2 3
Output example #1
17
Author Michael Dworkin
Source Winter School, Kharkov, 2011, Day 3