Grid Maze X75212


Statement
 

pdf   zip

A maze has been drawn on the graph paper. All walls are either horizontal or vertical.

image

Your task is simple: find the shortest route from the red dot to the green dot. You can only move horizontally or vertically.

NOTE: We assume that you can move very close to the walls (at distance 0).

Input

The first line contains five numbers: N,X1,Y1,X2,Y2N, X_1, Y_1, X_2, Y_2. Here NN (1N5001 \leq N \leq 500) is the number of lines in the maze, (X1,Y1)(X_1, Y_1) are the coordinates of the red dot, and (X2,Y2)(X_2, Y_2) are the coordinates of the green dot.

Each of the following NN lines describe one wall of the maze. A wall is described as four numbers x1,y1,x2,y2x_1, y_1, x_2, y_2, where either y1=y2y_1 = y_2 or x1=x2x_1 = x_2.

All coordinates are in range [2000000000,+2000000000][-2000000000, +2000000000].

Output

Output the length of the shortest route between the two dots. You should output IMPOSSIBLE if there is no route.

You obtain a route of length 17 by moving very close to the endings of both inner walls.

Public test cases
  • Input

    6 1 6 9 7
    0 0 10 0
    0 0 0 10
    10 0 10 10
    0 10 10 10
    2 3 2 10
    8 0 8 8
    

    Output

    17
    
  • Information
    Author
    Eryk Kopczynski
    Language
    English
    Official solutions
    Unknown.
    User solutions
    C++