Mini Multiverse

View as PDF

Submit solution


Points: 1
Time limit: 2.0s
Memory limit: 256M

Problem types

Zeyna is an amazing scientist known around the world. One day she was talking with her unbearable old friend Şehmettin. While talking Şehmettin bet to her that Zeyna can not prove or disprove the multiverse theory. Which Zeyna thought was impossible but still accepted the bet. Being brilliant as she is she had a plan. Since she couldn't prove or disprove it she decided to create it. Thus winning the bet by showing the newly created multiverse.

But creating an infinite dimensional multiverse was too time consuming, too pricy it was not optimal. So she created a 2-dimensional multiverse where each cell includes a universe. In \(\mathbf{N}\) of those universes she placed a Mahmut. As we all know a Mahmut can not live without another Mahmut.

Your job is to find the least costly way to bring any of the 2 Mahmuts out of all \(\mathbf{N}\) Mahmuts together. But dimensional travel is costly while mirror travel is free.

  • Dimensional travel costs 1 dimension travel coupon and it can only be used to travel to a neighboring universe
    • A neighboring universe is defined as a universe whose coordinate is adjacent to the universe you are currently in in any of the four cardinal directions (up, down, left, or right) in a two-dimensional plane.
  • Mirror travel costs nothing and it can be used to travel to a mirror universe.
    • A Mirror universe is defined as a universe whose coordinate can be obtained from reflecting its coordinates across one coordinate axes. For example mirror universes of the universe at coordinate (a, b) would be (-a, b) and (a, -b)

Input

The first line contains only one integer, \(\mathbf{N}\).

Next \(\mathbf{N}\) lines all contain 2 integers each, X and Y coordinates of a universe that contains a Mahmut. It is guaranteed that no Mahmuts are placed in the same universe or a universe that can reach another Mahmut without using dimension travel coupons.

  • \(2 \leq \mathbf{N} \leq 10^5\)
  • \(-10^9 \leq \mathbf{X,Y} \leq 10^9\)

Output

Print the least number of dimension travel coupons needed for bringing 2 Mahmuts together.

Example

Input 1:

5
-6 -9
0 5
-3 1
10 -8
1 1

Output 1:

2

Input 2:

4
0 0
3 -4
-12 5
-100 -100

Output 2:

7

Explanation

Input 1: The best option is to bring Mahmuts 3 and 5 together, who are at (-3, 1) and (1, 1). We will do it in 3 steps:

  • Mirror travel on X axis: (-1, 1) (free)
  • Dimensional travel - going to left universe: (-2, 1) (costs 1 coupon)
  • Dimensional travel - going to left universe: (-3, 1) (costs 1 coupon)