Walls

Time limit5sMemory limit128 MB

Summary
Choose the minimum number of odd-coordinate vertical or horizontal walls so that every segment between two stations is crossed by at least one wall.
Level

Hard8 of 10

Topics
Geometry, Greedy, Brute force, Implementation
Solved
No attempts yet

Problem

Consider a featureless, flat patch of desert as the Cartesian plane. On it stand several research stations, each located at a point (x,y)(x, y) where both xx and yy are even integers. For security, you must build walls that are sufficiently long and high to separate the stations so that no station is visible from any other station.

A wall may be built only along a North-South or an East-West line. A vertical (North-South) wall may be built at an odd xx-coordinate, and a horizontal (East-West) wall may be built at an odd yy-coordinate. Because the stations sit at even coordinates while the walls sit at odd coordinates, no wall ever touches a station. A wall is always long enough to completely separate the stations on one side of it from those on the other side.

Given the locations of the stations, determine the smallest number of walls that must be built. The segment joining any two stations must be crossed by at least one wall.

Input

The input contains several test cases. Each test case begins with an integer nn (2≤n≤1002 \le n \le 100), the number of stations. Each of the next nn lines contains two integers xx and yy (0≤x,y≤360 \le x, y \le 36), separated by a single space, giving the location (x,y)(x, y) of a station. Both xx and yy are always even. Within a test case all locations (x,y)(x, y) are distinct. The last test case is followed by a line containing a single 00.

Output

For each test case, output a single integer: the smallest number of walls that prevents the given nn stations from seeing one another. That is, the straight segment joining any two stations must be intersected by at least one wall. Print no extra spaces, and do not separate answers with blank lines.

Examples3

  1. Example 1

    Input
    4
    12 12
    4 8
    8 6
    2 4
    4
    0 0
    4 4
    10 8
    14 6
    0
    
    Expected output
    2
    3
    
  2. Example 2

    Input
    2
    2 2
    2 6
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    2
    0 0
    36 36
    0
    
    Expected output
    1