CCHESS - COSTLY CHESS

no tags 

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 8×8, 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...


hide comments
suraj_13: 2019-01-20 09:55:15

@ahmedgu no one is going to fall in the trap

sagar_june97p: 2019-01-18 14:29:44

Do not use while(!cin.eof()), cost me 2 WA

nidhi_061: 2018-06-22 07:37:42

Last edit: 2018-09-15 13:03:25
v_pp_27: 2018-02-15 06:10:01

solve NAKANJ first
then just use priority_queue instead of queue

ahmedgu: 2017-11-15 13:52:39

simple dp + dijkstra + backtracking + segment tree + binary search and little bit of geometry. Happy Coding :D

supriyanta: 2017-10-10 18:48:57

AC in one go! simple dijkstra :)

prateek: 2017-03-29 14:39:27

Any other solution rather than using priority_queue and bfs .Can we use backtracking?

pvsmpraveen: 2017-03-29 12:45:50

priority_queue :D

vanvinhbk94: 2017-02-22 08:04:13

AC in one go

vengatesh15: 2017-01-23 13:34:06

simple bfs AC in 1 go

Last edit: 2017-01-23 13:34:32

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