Point Card

Time limit2sMemory limit512 MB

Summary
Given M cards with A wins out of 2N cells, pay 1 yen per flipped stamp to make at least M-1 cards hold N or more wins; minimize total cost.
Level

Medium4 of 10

Topics
Greedy, Sorting, Array
Solved
No attempts yet

Problem

The JOI shopping district runs a point card service. Each point card has 2N2N cells for stamps. Every time you buy something, you draw a lottery, and one empty cell receives either a "win" stamp or a "miss" stamp depending on the result. A cell can never be stamped more than once.

A point card on which at least NN of the 2N2N cells carry a win stamp can be exchanged for one prize. You can also pay 1 yen to change any single stamp on a card into the other kind of stamp.

JOI has MM point cards, and all 2N2N cells of each card are filled. Card ii has AiA_i win stamps and BiB_i miss stamps.

JOI wants to get at least M−1M-1 prizes. Find the minimum cost needed to do this.

Input

The input consists of M+1M+1 lines.

The first line contains two integers NN and MM (1≤N≤10001 \le N \le 1000, 1≤M≤10001 \le M \le 1000) separated by a space. Each point card has 2N2N cells, and JOI has MM point cards.

The ii-th of the next MM lines (1≤i≤M1 \le i \le M) contains two integers AiA_i and BiB_i (0≤Ai≤2N0 \le A_i \le 2N, 0≤Bi≤2N0 \le B_i \le 2N, Ai+Bi=2NA_i + B_i = 2N). Point card ii has AiA_i win stamps and BiB_i miss stamps.

Output

Print on one line the minimum cost, in yen, that JOI needs to get at least M−1M-1 prizes.

Hint

In Example 1, changing 3 miss stamps on card 1 and 1 miss stamp on card 3 into win stamps costs 4 yen and makes 5−1=45-1=4 cards exchangeable for prizes. This is the minimum cost.

In Example 2, 4−1=34-1=3 cards can already be exchanged for prizes, so no stamp needs to change.

Examples2

  1. Example 1

    Input
    4 5
    1 7
    6 2
    3 5
    4 4
    0 8
    
    Expected output
    4
    
  2. Example 2

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