This page is still under construction.

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

Make an X (Hard)

Time limit1sMemory limit1024 MB

Level

Not classified yet

Solved
No attempts yet

Problem

This problem is the same as Make an X except for the constraint on NN.

NN pushpins are stuck on a board. Each pushpin is a very small point, and the board is modeled as an infinite coordinate plane. Phoenix, who likes the letter X, wants to remove some of the pushpins so that the remaining ones form an X shape. An X shape is defined as follows.

  • There is a center pushpin PP that satisfies the conditions below.
  • No other pushpin shares an xx coordinate or a yy coordinate with PP.
  • Treating PP as the origin, no two pushpins in the same quadrant share an xx coordinate or a yy coordinate, and each quadrant contains at least one pushpin.
  • Treating PP as the origin, sort the pushpins in quadrants 1 and 3 by xx coordinate. Their yy coordinates increase.
  • Treating PP as the origin, sort the pushpins in quadrants 2 and 4 by xx coordinate. Their yy coordinates decrease.

Given the positions of the pushpins, find the minimum number of pushpins to remove so that the remaining pushpins form an X shape.

Input

The first line contains the number of pushpins NN. (1≤N≤100 0001 \leq N \leq 100\,000)

Each of the next NN lines contains the position xix_i, yiy_i of the ii-th pushpin. (−109≤xi,yi≤109-10^9 \leq x_i, y_i \leq 10^9)

All coordinates are integers, and no two pushpins share the same coordinates.

Output

Print the minimum number of pushpins to remove so that the remaining pushpins form an X shape. If the given pushpins cannot form an X shape, print -1.

Examples2

  1. Example 1

    Input
    9
    1 1
    1 0
    1 -1
    0 1
    0 0
    0 -1
    -1 1
    -1 0
    -1 -1
    
    Expected output
    4
    
  2. Example 2

    Input
    5
    0 0
    1 0
    2 0
    -1 0
    -2 0
    
    Expected output
    -1