This page is still under construction.

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

Laser

Time limit3sMemory limit512 MB

Summary
Choose up to K rays from the origin to hit the most first-quadrant segments, with no segment hit by two rays.
Level

Medium7 of 10

Topics
Dynamic programming, Geometry, Intervals, Sorting
Solved
No attempts yet

Problem

Jaehyun built a game a while back. It is called "Yoo Jaemin", and the hero of the game is Yoo Jaemin.

Yoo Jaemin fires laser beams from the origin (0,0)(0, 0). A beam is a ray that starts at the origin and travels outward in whatever direction the player picks. The goal is to hit as many of the segments on the coordinate plane as possible. Yoo Jaemin can fire the beam at most KK times, and a beam that only passes through an endpoint of a segment still counts as a hit.

Jaehyun left one case unhandled while writing the game: hitting a segment that has already been hit crashes the program. He panicked at first, then decided a game with that rule sounds fun anyway, so he wants to play it optimally. Find the largest number of segments he can hit without ever hitting the same segment twice.

Input

The first line contains KK and NN. (1≤K≤1001 \le K \le 100, 1≤N≤500 0001 \le N \le 500\,000)

Each of the next NN lines contains one segment as x1x_1, y1y_1, x2x_2, y2y_2. (1≤x1,y1,x2,y2≤1 000 0001 \le x_1, y_1, x_2, y_2 \le 1\,000\,000) The segment joins the point (x1,y1)(x_1, y_1) and the point (x2,y2)(x_2, y_2).

Output

Print the largest number of segments that can be hit with at most KK laser beams, under the condition that a segment already hit is never hit again.

Hint

Examples2

  1. Example 1

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

    Input
    2 3
    1 1 1 3
    1 1 1 1
    1 3 1 3
    
    Expected output
    2