Spiral

Time limit5sMemory limit128 MB

Summary
On an N by N grid with blocked fountain cells, find the longest path made of four straight segments turning only right, never revisiting a field.
Level

Medium7 of 10

Topics
Brute force, Implementation, Prefix sum, Geometry
Solved
No attempts yet

Problem

Kalemegdan Park is the largest park in Belgrade. Model the park as an N×NN \times N grid of N2N^2 unit fields. Some fields contain a fountain; every other field is empty. A rider may move between two empty fields only when they share an edge (up, down, left, or right).

The rider only likes spiral routes. A spiral route is built as follows: choose a starting empty field and a starting direction (North, East, South, or West). Move at least one field in that direction, then turn 90 degrees to the right; move at least one field in the new direction, and turn 90 degrees to the right again; move at least one field, and after a final 90 degree right turn move at least one more field. A route therefore consists of exactly four straight segments, and every turn is clockwise.

The route may never pass through a fountain and may never visit the same field twice. The length of a spiral route is the number of fields it covers (equivalently, one plus the total number of steps taken).

The figure above shows a park with N=6N = 6 (black squares are fountains) together with a few possible spiral routes.

Print the length of the longest spiral route the rider can take.

Input

The first line contains two integers NN and KK: the side length of the square park and the number of fountains.

Each of the next KK lines contains two integers xx and yy, the coordinates of one fountain. Field (x,y)(x, y) lies in row xx counted from the top and column yy counted from the left, so (1,1)(1, 1) is the top-left field and (N,1)(N, 1) is the bottom-left field. North means up, East means right, South means down, and West means left.

Output

Print a single integer: the length of the longest spiral route. At least one spiral route is guaranteed to exist.

Constraints

  • 2≤N≤10002 \le N \le 1000
  • 0≤K≤min⁡(2000,N2)0 \le K \le \min(2000, N^2)
  • 1≤x,y≤N1 \le x, y \le N

Examples3

  1. Example 1

    Input
    6 9
    2 1
    2 4
    4 4
    6 2
    6 3
    5 3
    4 6
    5 1
    2 6
    
    Expected output
    14
    
  2. Example 2

    Input
    3 0
    
    Expected output
    8
    
  3. Example 3

    Input
    6 7
    1 1
    1 6
    3 3
    3 4
    4 3
    6 1
    6 6
    
    Expected output
    16