cho.sh
Notes
Loading...

Cloth Moth Shortest Path

Time limit

2s

Memory limit

128 MB

Problem

A moth is on a very large piece of cloth. The cloth is considered infinite for the purpose of movement. There are N patched regions made from a different material, and each region is a convex polygon. These convex polygons may touch or overlap one another.

The moth wants to move from its current position to a target position while staying only on the cloth. It cannot pass through the interior of any patched region. However, the thread along the border of each patch is cloth, so the moth may move along the edges of the convex polygons. If edges of two convex polygons touch, the shared edge segment is also passable.

Find the shortest distance the moth must travel to reach the target position.

Input

The first line contains five integers N, X, Y, U, and V. The moth starts at (X, Y), and the target position is (U, V).

Each of the next N lines describes one convex polygon representing a patched region. A line starts with the number of vertices M, followed by the x- and y-coordinates of the M vertices in order.

The total number of vertices over all convex polygons is at most 300. Every coordinate is an integer between -10,000 and 10,000, inclusive.

Output

Print the shortest distance on the first line. An absolute or relative error of at most 10^-3 is accepted. If the target cannot be reached, print -1.