#K44757. Minimum Teleportation Hops

    ID: 27602 Type: Default 1000ms 256MiB

Minimum Teleportation Hops

Minimum Teleportation Hops

You are given n teleportation portals. Each portal is defined by four integers \(x_i, y_i, x'_i, y'_i\) that indicate that the portal at coordinate \((x_i, y_i)\) teleports you to coordinate \((x'_i, y'_i)\).

You are also given a starting coordinate \((x_s, y_s)\) and a target coordinate \((x_t, y_t)\). Your task is to determine the minimum number of teleportation hops needed to travel from the starting coordinate to the target coordinate using the available portals. If it is impossible to reach the target coordinate, output \(-1\).

Note: Teleportation only works in the given direction (from \((x_i, y_i)\) to \((x'_i, y'_i)\)), and you may use any portal any number of times if possible.

inputFormat

The input is read from stdin and has the following format:

<n>
<x1> <y1> <x1'> <y1'>
<x2> <y2> <x2'> <y2'>
... (n lines in total for portals)
<x_s> <y_s>
<x_t> <y_t>

Where:

  • n is the number of portals.
  • Each of the next n lines contains four integers representing a portal.
  • The next line contains the starting coordinate \((x_s, y_s)\).
  • The final line contains the target coordinate \((x_t, y_t)\).

outputFormat

Output a single integer to stdout that represents the minimum number of teleportation hops required to reach the target coordinate from the starting coordinate. If the target cannot be reached, output \(-1\).

## sample
3
1 1 2 2
2 2 3 3
3 3 4 4
1 1
3 3
2