Ibis is fighting with a monster.
The health of the monster is H.
Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.
The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.
Ibis wins when the health of the monster becomes 0 or below.
Find the minimum total Magic Points that have to be consumed before winning.

-----Constraints-----
 - 1 \leq H \leq 10^4
 - 1 \leq N \leq 10^3
 - 1 \leq A_i \leq 10^4
 - 1 \leq B_i \leq 10^4
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
H N
A_1 B_1
:
A_N B_N

-----Output-----
Print the minimum total Magic Points that have to be consumed before winning.

-----Sample Input-----
9 3
8 3
4 2
2 1

-----Sample Output-----
4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.
Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.
In this way, we can win at the total cost of 4 Magic Points.

Difficulty: MEDIUM

Solutions:
(h, n) = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
mx = max((a for (a, b) in ab))
dp = [10 ** 10] * (h + 1 + mx)
dp[0] = 0
for i in range(1, h + 1 + mx):
	dp[i] = min((dp[i - a] + b for (a, b) in ab))
print(min(dp[h:]))


def main():
	(h, n) = list(map(int, input().split()))
	ab = [tuple(map(int, input().split())) for _ in range(n)]
	large_num = 10 ** 9
	dp = [large_num] * (h + 10 ** 4 + 1)
	dp[0] = 0
	for abe in ab:
		a = abe[0]
		b = abe[1]
		for i1 in range(h + 1):
			if dp[i1 + a] > dp[i1] + b:
				dp[i1 + a] = dp[i1] + b
	r = min(dp[h:])
	print(r)

def __starting_point():
	main()
__starting_point()


def dp(H, N, ab):
	max_a = max([ab[i][0] for i in range(N)])
	d = [INF for _ in range(H + max_a)]
	d[0] = 0
	for h in range(1, H + max_a):
		d[h] = min((d[h - a] + b for (a, b) in ab))
	return min(d[H:])
INF = 10 ** 10
(H, N) = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(N)]
print(dp(H, N, ab))


import numpy as np
(H, N) = list(map(int, input().split()))
ab = []
for i in range(N):
	(a, b) = list(map(int, input().split()))
	ab.append([a, b])
ab = np.array(ab)
a_list = ab[:, 0]
b_list = ab[:, 1]
max_a = ab.max()
inf = float('inf')
dp = np.array([inf for _ in range(H + max_a)])
dp[0] = 0
for i in range(1, len(dp)):
	dp[i] = np.amin(dp[np.maximum(i - a_list, 0)] + b_list)
print(int(min(dp[H:])))


(H, N) = list(map(int, input().split()))
magics = [list(map(int, input().split())) for _ in range(N)]
max_A = max((a for (a, b) in magics))
INF = 10 ** 10
DP = [INF] * (H + max_A)
DP[0] = 0
for i in range(1, H + max_A):
	DP[i] = min((DP[i - a] + b for (a, b) in magics))
ans = min(DP[H:])
print(ans)


def num():
	from sys import stdin
	(h, n) = list(map(int, input().split()))
	magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
	INF = float('inf')
	ans = [INF] * (h + 1)
	ans[-1] = 0
	for i in range(h, 0, -1):
		if ans[i] != INF:
			for (j, k) in magic:
				if i - j < 0:
					num = ans[i] + k
					if ans[0] > num:
						ans[0] = num
				else:
					num = ans[i] + k
					if ans[i - j] > num:
						ans[i - j] = num
	return ans[0]
print(num())


(h, n) = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
dp = [10 ** 5 for _ in range(h + 1)]
dp[0] = 0
for i in range(1, h + 1):
	dp[i] = min([dp[max(0, i - pw)] + ma for (pw, ma) in ab])
print(dp[h])


import sys
input = sys.stdin.readline
INF = float('inf')

def main():
	(H, N) = list(map(int, input().split()))
	A = [0] * N
	B = [0] * N
	for i in range(N):
		(A[i], B[i]) = list(map(int, input().split()))
	dp = [INF] * (H + max(A))
	dp[0] = 0
	for h in range(H):
		for (a, b) in zip(A, B):
			if dp[h] + b < dp[h + a]:
				dp[h + a] = dp[h] + b
	ans = min(dp[H:])
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
(h, n) = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(n)]
min_hp = h
max_hp = h + max((a for (a, b) in AB))
dp = [sys.maxsize] * (max_hp + 1)
dp[0] = 0
for i in range(1, max_hp + 1):
	dp[i] = min((dp[i - a] + b for (a, b) in AB))
print(min(dp[h:]))


(h, n) = map(int, input().split())
li = []
x = []
for i in range(n):
	(A, B) = map(int, input().split())
	x.append(A)
	li.append([A, B])
dp = [10 ** 9] * (h + max(x))
dp[0] = 0
for i in range(1, len(dp)):
	dp[i] = min((dp[i - a] + b for (a, b) in li))
print(min(dp[h:]))


import numpy as np
(H, N) = list(map(int, input().split()))
A = []
B = []
for i in range(N):
	(a, b) = list(map(int, input().split()))
	A.append(a)
	B.append(b)
A = np.array(A)
B = np.array(B)
dp = np.ones(H + 1 + 10 ** 4) * np.inf
dp[H + 1:] = 0
dp[0] = 0
for i in range(1, H + 1):
	dp[i] = min(dp[i - A] + B)
print(int(dp[H]))