PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000018. Nghĩa trang ma ám (Haunted Graveyard)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nghĩa trang ma ám (Haunted Graveyard)

Haunted Graveyard

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A graveyard is a WW-by-HH grid. Enter at (0,0)(0,0) and leave at (W−1,H−1)(W-1,H-1). A normal move to a four-neighboring cell takes one second, but gravestone cells are blocked. Haunted holes force an immediate directed teleport with a signed time change TT; you cannot walk out of a hole cell. Reaching the exit ends the walk immediately. If a negative-time cycle can be exploited from the entrance before reaching the exit, print Never. Otherwise print Impossible if the exit is unreachable, or the minimum arrival time.

Input

First line: W,HW,H. Then GG and GG gravestone coordinates x,yx,y. Then EE and EE lines of x1,y1,x2,y2,Tx_1,y_1,x_2,y_2,T for haunted holes. Entry and exit contain neither stones nor holes. Hole origins are unique; neither endpoint contains a gravestone.

Output

Print Never, Impossible, or the minimum time as an integer.

Subtasks

  • Subtask 1 (20 points): 1≤W,H≤61\le W,H\le6; 0≤G,E0\le G,E; G+E≤WH−2G+E\le WH-2 for WH≥2WH\ge2; ∣T∣≤100|T|\le100; no blocked or hole entry/exit, no duplicate hole origins.

  • Subtask 2 (30 points): 1≤W,H≤151\le W,H\le15; 0≤G,E0\le G,E; G+E≤WH−2G+E\le WH-2 for WH≥2WH\ge2; ∣T∣≤1000|T|\le1000; no blocked or hole entry/exit, no duplicate hole origins.

  • Subtask 3 (50 points): 1≤W,H≤301\le W,H\le30; 0≤G,E0\le G,E; G+E≤WH−2G+E\le WH-2 for WH≥2WH\ge2; ∣T∣≤10000|T|\le10000; no blocked or hole entry/exit, no duplicate hole origins.

Examples

Input

2 2
0
0

Output

2

Explanation

With no gravestones or holes, two ordinary moves reach the exit, so the minimum time is 22.