This page is still under construction.

Parts of this page are still being built. What you see may change.

Wire Crossing

Time limit2sMemory limit128 MB

Summary
Find the smallest number of wire segments a path between the two given points must cross without passing through a point where wires meet.
Level

Medium7 of 10

Topics
Shortest path, Graph, Geometry
Solved
No attempts yet

Problem

In a two-dimensional hardware layout, crossing wires need expensive gadgets. You are given mm existing straight wires and a new connection from (x0,y0)(x_0, y_0) to (x1,y1)(x_1, y_1). The new connection need not be straight, but it may not pass through a point where two or more existing wires already meet.

The start and end points do not lie on an existing wire. Each pair of wires meets in at most one point, and wires do not overlap. Find the minimum number of existing wires that must be crossed, and print that number.

Input

A single test case is given.

  • Line 1: mm, x0x_0, y0y_0, x1x_1, y1y_1 (m≤100m \le 100)
  • Next mm lines: one existing wire from (xa,ya)(x_a, y_a) to (xb,yb)(x_b, y_b)

Every coordinate has absolute value less than 10510^5.

Output

Print the minimum number of existing wires that must be crossed to connect the start and end points.

Examples3

  1. Example 1

    Input
    8 3 3 19 3
    0 1 22 1
    0 5 22 5
    1 0 1 6
    5 0 5 6
    9 0 9 6
    13 0 13 6
    17 0 17 6
    21 0 21 6
    
    Expected output
    2
    
  2. Example 2

    Input
    0 1 1 9 1
    
    Expected output
    0
    
  3. Example 3

    Input
    1 0 0 10 0
    0 5 10 5
    
    Expected output
    0