This page is still under construction.

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

Meeting in the Sierpinski Labyrinth

Time limit3sMemory limit512 MB

Summary
Tourists stand on cells of a grid that are free exactly when row and column share no binary 1 bit, and must meet in one cell with minimum total steps.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Bit manipulation
Solved
No attempts yet

Problem

A group of Czech tourists is walking through a labyrinth of a strange self-similar shape. The ground plan of the labyrinth is a Sierpinski triangle, a fractal named after the Polish mathematician Wacław Sierpiński.

The labyrinth has a billion rows, numbered 00 to 109−110^9 - 1 from top to bottom, and a billion columns, numbered 00 to 109−110^9 - 1 from left to right. Every field is either free or blocked.

The field in row XX and column YY is free if the bitwise AND of XX and YY equals 00, and blocked otherwise. In other words, the field is blocked if, when XX and YY are written in binary, there is an integer kk such that the kk-th digit from the right is 11 in both numbers.

The tourists are tired after a long day of wandering and want to gather in one free field to exchange experiences. In one step, a tourist jumps to one of the adjacent free fields, up, down, left or right.

Given the current positions of the tourists, write a program that determines the minimum total number of steps needed for all of them to meet in the same field.

Input

The first line contains the integer NN, the number of tourists. (1≤N≤1000001 \le N \le 100000)

Each of the next NN lines contains two integers RiR_i and SiS_i, the row and the column of the field where the ii-th tourist stands. (0≤Ri,Si≤109−10 \le R_i, S_i \le 10^9 - 1)

Every tourist stands on a free field, so the bitwise AND of RiR_i and SiS_i is 00. Several tourists can stand on the same field.

Output

Print the minimum number of steps on the first and only line.

The answer can exceed the range of a 32-bit integer, so use a 64-bit integer type (long long in C or C++, int64 in Pascal).

Hint

In the first example, one of the fields where the tourists can meet is (2, 0).

In the second example, one of the fields where the tourists can meet is (8, 4).

Examples2

  1. Example 1

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

    Input
    6
    2 5
    3 4
    8 7
    9 6
    10 5
    11 4
    
    Expected output
    50