Under construction

Still being built. Things may change

Square Running

Time limit1sMemory limit1024 MB

Summary
Given n runners on concentric lanes around a rectangle, find the earliest second when every runner is in the photographer's row or column, or report -1.
Level

Hard8 of 10

Topics
Number theory, Math, Implementation
Solved
No attempts yet

Problem

Runsberg citizens are keen on sports, especially running. Standard marathons bored them, so they decided to organize a marathon in Minecraft style. The event will be held on Runsberg's Central Square. Citizens believe that the most important thing in sports is not winning but participating, so the goal of the square running is not to win but to take a beautiful photo of all participants.

The Central Square is a rectangle divided into unit squares. Rows are numbered from top to bottom starting from one, and columns are numbered from left to right starting from one. Each unit square has coordinates rr and cc, its row and column number.

A rectangular grass field lies on the square, with sides parallel to the square's sides. Its top left unit square is (RL,CL)(R_L, C_L), and its bottom right unit square is (RR,CR)(R_R, C_R). There are nn lanes for nn runners around the grass field. Lane ii is at distance ii from the field's border, and runner ii is on lane ii. Runner ii starts from the unit square (ri,ci)(r_i, c_i). All runners start at the same time at the same speed. Every second, each runner moves from the current square to the next square in their lane, counterclockwise.

A photographer stands on the unit square (Rp,Cp)(R_p, C_p) of the grass field. The photographer uses a dual-lens camera that takes a photo in two opposite directions at once. A photo is beautiful if, at the moment of the shot, all runners are in row RpR_p or in column CpC_p. Because of the camera, runners may be in the same row on both the left and right of the photographer, or in the same column above and below.

Find the minimum number of seconds tt after the start after which the photographer can take a beautiful photo. If no beautiful photo is possible under these conditions, find that out instead.

Input

The first line contains an integer nn (1≤n≤181 \le n \le 18), the number of runners. The second line contains six integers RLR_L, CLC_L, RRR_R, CRC_R (n+1≤RL≤RR≤100−nn + 1 \le R_L \le R_R \le 100 - n, n+1≤CL≤CR≤100−nn + 1 \le C_L \le C_R \le 100 - n), RpR_p (RL≤Rp≤RRR_L \le R_p \le R_R), and CpC_p (CL≤Cp≤CRC_L \le C_p \le C_R). These are the top left corner, the bottom right corner, and the photographer's position. It is guaranteed that RR−RL+CR−CLR_R - R_L + C_R - C_L is divisible by 4.

The next nn lines each contain two integers rir_i and cic_i, the starting coordinates of runner ii. Each start lies on lane ii, each lane holds exactly one runner, and lane ii is at distance ii from the field's border.

Output

Print the minimum number of seconds tt after the start at which the photographer can take a beautiful photo, or −1-1 if that is impossible.

Hint

Starting positions of the runners.

Positions of the runners after 3 seconds. All runners are in row RpR_p, so the photographer can take a beautiful photo.

Examples2

  1. Example 1

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

    Input
    2
    4 4 5 7 5 4
    3 4
    7 8
    
    Expected output
    3