Under construction

Still being built. Things may change

Guitar Strings

Time limit2sMemory limit128 MB

Summary
Buy at least N guitar strings using packages of 6 or single strings from M brands at given prices, minimizing total cost.
Level

Easy3 of 10

Topics
Brute force, Greedy, Math
Solved
No attempts yet

Problem

N strings on Kangto's guitar have snapped. New strings can be bought either as packages of 6 strings or individually in any quantity.

For each brand, the package price and single-string price are given. Find the minimum cost needed to buy at least N guitar strings.

Input

The first line contains the number of snapped strings N and the number of brands M. N is a positive integer at most 100, and M is a positive integer at most 50.

Each of the next M lines contains a brand's package price and single-string price, separated by a space. Every price is an integer between 0 and 1,000, inclusive.

Output

Print the minimum cost needed to buy at least N guitar strings.

Hint

There is no additional hint.

Examples6

  1. Example 1

    Input
    4 2
    12 3
    15 4
    
    Expected output
    12
    
  2. Example 2

    Input
    10 3
    20 8
    40 7
    60 4
    
    Expected output
    36
    
  3. Example 3

    Input
    15 1
    100 40
    
    Expected output
    300
    
  4. Example 4

    Input
    17 1
    12 3
    
    Expected output
    36
    
  5. Example 5

    Input
    7 2
    10 3
    12 2
    
    Expected output
    12
    
  6. Example 6

    Input
    9 16
    21 25
    77 23
    23 88
    95 43
    96 19
    59 36
    80 13
    51 24
    15 8
    25 61
    21 22
    3 9
    68 68
    67 100
    83 98
    96 57
    
    Expected output
    6