Under construction

Still being built. Things may change

Turning on the lights

Interview

Time limit2sMemory limit512 MB

Summary
Bessie walks through lit adjacent rooms to press switches that light new rooms, and every room ever lit counts.
Level

Medium5 of 10

Topics
BFS, Graph
Solved
No attempts yet

Problem

Farmer John built a new barn with N×NN \times N rooms. The rooms carry the coordinates (1,1)(1, 1) through (N,N)(N, N). Bessie the cow is afraid of the dark and wants as many rooms lit as possible.

Bessie starts in (1,1)(1, 1), the only room whose light is on. Some rooms hold a switch that turns the light of another room on and off. For example, a switch in (1,1)(1, 1) can control the light of (1,2)(1, 2). Bessie can press a switch only while she is inside the room that holds it. She can enter a room only when its light is on, and from a room she moves to a room adjacent in one of the four directions.

A room that Bessie lights counts even if she never gets inside it. Room (1,1)(1, 1), lit from the start, counts as well. Find the largest number of rooms Bessie can have lit.

Input

The first line contains two integers NN and MM. (2≤N≤1002 \le N \le 100, 1≤M≤200001 \le M \le 20000)

Each of the next MM lines contains four integers xx, yy, aa, bb. It means a switch in room (x,y)(x, y) turns the light of room (a,b)(a, b) on and off. (1≤x,y,a,b≤N1 \le x, y, a, b \le N) One room can hold several switches, and several switches can control the light of the same room.

Output

Print the largest number of rooms Bessie can have lit.

Hint

In the first example Bessie uses the switch in (1,1)(1, 1) to light (1,2)(1, 2) and (1,3)(1, 3). Walking to (1,3)(1, 3) lets her light (2,1)(2, 1), and from (2,1)(2, 1) she lights (2,2)(2, 2). Room (2,3)(2, 3) stays dark, so she can never press the switch inside it. Five rooms is the most she can light.

Examples4

  1. Example 1

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

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

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

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