CCHESS - COSTLY CHESS

In the country of Rome, Chess is a royal game. For every move the players had to give some bucks to the Emperor Jurg. The LGMs or Little Green Men, are very good player of chess. But as chess is an expensive game, that's why it is royal, they asked you to help them find the minimum bucks which they had to pay for moving their knight from one position to another. Any number of steps can be used to reach the destination.

Constraints

The chess has a dimension of 8x8, and the index of left bottom cell (0, 0).

Knight move only in a standard way, i.e. 2 row and 1 column or 1 row and 2 column.

If in a step knight move from (a, b) to (c, d), then LGM had to pay a*c + b*d bucks to Emperor Jurg.

0 ≤ a, b, c, d ≤ 7

Input

There are 100-150 test cases. Each test case is composed of four space separated integers. The first two numbers, a, b, are the starting position of the knight and the next two, c, d, are the destination of the knight. Read up to End Of File.

Output

For each test case, print the minimum amount of bucks they had to pay in separate line. If it's impossible to reach the destination then print -1.

Example

Input:
2 5 5 2
4 7 3 2
1 2 3 4

Output:
42
78
18

Explanation for Test Case 1

For moving knight from (2, 5) to (5, 2) in minimum cost, one of the path is (2, 5) → (3, 3) → (5, 2)

Bucks paid:

  • (2, 5) → (3, 3) = (2 * 3 + 5 * 3) = 21
  • (3, 3) → (5, 2) = (3 * 5 + 3 * 2) = 21
  • Total cost = 42

To infinity and beyond...


Added by:abhiranjan
Date:2011-02-18
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:IIITM Local Contest

hide comments
2024-09-07 15:13:48
is it possible to slove it by recursion ?
[Simes]: Yes, I did.

Last edit: 2024-09-13 21:59:11
2024-06-05 09:32:27
how the answer is 18 for 3rd test case my output is 38 for ( 1 2 3 4)
2023-08-04 08:34:50
super ez
2023-06-12 13:46:27
If anyone needs the code! <snip>

Last edit: 2023-06-12 20:29:00
2023-06-12 13:41:05
int a, b, c, d;
while (scanf("%d%d%d%d", &a, &b, &c, &d) != EOF)
{
// your code here
}
2021-05-06 06:42:20
can anyone tell how to check eof condition.i am using cin.eof() in loop condition but it is giving wrong answer.but my code is fine and passing all the edge cases also
2021-04-01 12:12:11
Dijkstra on 2D grid
2021-01-24 05:52:57
AC in one go with djs and priority queue.
2019-06-15 11:20:49
Easy problem with BFS or Dijkstra , just ensure not to use !cin.eof as the loop condition... caused me to get a WA for no reason... put the cin statement as the loop condition and got easy AC
2019-05-25 01:07:48
super easy problem!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.