Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems.

For this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends on the ratio of the number of participants who solved the problem to the total number of round participants. Everyone who made at least one submission is considered to be participating in the round.

$\left. \begin{array}{|l|r|} \hline \text{Solvers fraction} & {\text{Maximum point value}} \\ \hline(1 / 2,1 ] & {500} \\ \hline(1 / 4,1 / 2 ] & {1000} \\ \hline(1 / 8,1 / 4 ] & {1500} \\ \hline(1 / 16,1 / 8 ] & {2000} \\ \hline(1 / 32,1 / 16 ] & {2500} \\ \hline [ 0,1 / 32 ] & {3000} \\ \hline \end{array} \right.$

Pay attention to the range bounds. For example, if 40 people are taking part in the round, and 10 of them solve a particular problem, then the solvers fraction is equal to 1 / 4, and the problem's maximum point value is equal to 1500.

If the problem's maximum point value is equal to x, then for each whole minute passed from the beginning of the contest to the moment of the participant's correct submission, the participant loses x / 250 points. For example, if the problem's maximum point value is 2000, and the participant submits a correct solution to it 40 minutes into the round, this participant will be awarded with 2000·(1 - 40 / 250) = 1680 points for this problem.

There are n participants in the round, including Vasya and Petya. For each participant and each problem, the number of minutes which passed between the beginning of the contest and the submission of this participant to this problem is known. It's also possible that this participant made no submissions to this problem.

With two seconds until the end of the round, all participants' submissions have passed pretests, and not a single hack attempt has been made. Vasya believes that no more submissions or hack attempts will be made in the remaining two seconds, and every submission will pass the system testing.

Unfortunately, Vasya is a cheater. He has registered 10^9 + 7 new accounts for the round. Now Vasya can submit any of his solutions from these new accounts in order to change the maximum point values of the problems. Vasya can also submit any wrong solutions to any problems. Note that Vasya can not submit correct solutions to the problems he hasn't solved.

Vasya seeks to score strictly more points than Petya in the current round. Vasya has already prepared the scripts which allow to obfuscate his solutions and submit them into the system from any of the new accounts in just fractions of seconds. However, Vasya doesn't want to make his cheating too obvious, so he wants to achieve his goal while making submissions from the smallest possible number of new accounts.

Find the smallest number of new accounts Vasya needs in order to beat Petya (provided that Vasya's assumptions are correct), or report that Vasya can't achieve his goal.


-----Input-----

The first line contains a single integer n (2 ≤ n ≤ 120) — the number of round participants, including Vasya and Petya.

Each of the next n lines contains five integers a_{i}, 1, a_{i}, 2..., a_{i}, 5 ( - 1 ≤ a_{i}, j ≤ 119) — the number of minutes passed between the beginning of the round and the submission of problem j by participant i, or -1 if participant i hasn't solved problem j.

It is guaranteed that each participant has made at least one successful submission.

Vasya is listed as participant number 1, Petya is listed as participant number 2, all the other participants are listed in no particular order.


-----Output-----

Output a single integer — the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal.


-----Examples-----
Input
2
5 15 40 70 115
50 45 40 30 15

Output
2

Input
3
55 80 10 -1 -1
15 -1 79 60 -1
42 -1 13 -1 -1

Output
3

Input
5
119 119 119 119 119
0 0 0 0 -1
20 65 12 73 77
78 112 22 23 11
1 78 60 111 62

Output
27

Input
4
-1 20 40 77 119
30 10 73 50 107
21 29 -1 64 98
117 65 -1 -1 -1

Output
-1



-----Note-----

In the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 420 + 360 + 270 = 2970 points, while Petya will score just 800 + 820 + 420 + 440 + 470 = 2950 points.

In the second example, Vasya has to make a single unsuccessful submission to any problem from two new accounts, and a single successful submission to the first problem from the third new account. In this case, the maximum point values of the problems will be equal to 500, 1500, 1000, 1500, 3000. Vasya will score 2370 points, while Petya will score just 2294 points.

In the third example, Vasya can achieve his goal by submitting the solutions to the first four problems from 27 new accounts. The maximum point values of the problems will be equal to 500, 500, 500, 500, 2000. Thanks to the high cost of the fifth problem, Vasya will manage to beat Petya who solved the first four problems very quickly, but couldn't solve the fifth one.

Source: codeforces | Difficulty: HARD
Tags: Complete search

Greedy algorithms


Solutions:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
solved = [0 for i in range(5)]
score = [0 for i in range(5)]
for i in range(n):
	for j in range(5):
		solved[j] += int(a[i][j] > -1)
for k in range(31 * n + 1):
	for i in range(5):
		tot = n + k
		cur = solved[i]
		if a[0][i] > -1 and a[1][i] > -1 and (a[0][i] > a[1][i]):
			cur += k
		score[i] = 500
		while score[i] < 3000 and 2 * cur <= tot:
			cur *= 2
			score[i] += 500
	res = [0, 0]
	for j in range(2):
		for i in range(5):
			if a[j][i] > -1:
				res[j] += score[i] / 250 * (250 - a[j][i])
	if res[0] > res[1]:
		print(k)
		return
print('-1')


def get_value(parters, solved):
	if solved * 32 <= parters:
		return 3000
	if solved * 16 <= parters:
		return 2500
	if solved * 8 <= parters:
		return 2000
	if solved * 4 <= parters:
		return 1500
	if solved * 2 <= parters:
		return 1000
	return 500

def points(value, time):
	return value - value // 500 * time
n = int(input())
t = [0] * n
for i in range(n):
	t[i] = list(map(int, input().split()))
value = [0] * 5
solve = [0] * 5
for i in range(5):
	solved = 0
	for j in range(n):
		if t[j][i] != -1:
			solved += 1
		else:
			t[j][i] = 250
	value[i] = get_value(n, solved)
	solve[i] = solved
vasya = 0
for i in range(5):
	vasya += points(value[i], t[0][i])
petya = 0
for i in range(5):
	petya += points(value[i], t[1][i])
pot = [[0] * 20000 for i in range(5)]
for problem in range(5):
	for cur_ac in range(20000):
		new_value = 0
		if t[0][problem] < t[1][problem]:
			new_value = get_value(n + cur_ac, solve[problem])
		elif t[0][problem] != 250:
			new_value = get_value(n + cur_ac, solve[problem] + cur_ac)
		else:
			new_value = get_value(n + cur_ac, solve[problem])
		win = points(new_value, t[0][problem]) - points(new_value, t[1][problem]) - points(value[problem], t[0][problem]) + points(value[problem], t[1][problem])
		pot[problem][cur_ac] = win
res = -1
for i in range(20000):
	tmp = 0
	for pr in range(5):
		if len(pot[pr]) <= i:
			tmp += pot[pr][-1]
		else:
			tmp += pot[pr][i]
	if tmp > petya - vasya:
		res = i
		break
print(res)


import sys
inf = 10 ** 9 + 7

def solve():
	n = int(sys.stdin.readline())
	v = [int(vi) for vi in sys.stdin.readline().split()]
	p = [int(pi) for pi in sys.stdin.readline().split()]
	cnt = [0] * 5
	for i in range(5):
		if v[i] != -1:
			cnt[i] += 1
		if p[i] != -1:
			cnt[i] += 1
	for i in range(n - 2):
		a = [int(ai) for ai in sys.stdin.readline().split()]
		for j in range(5):
			if a[j] != -1:
				cnt[j] += 1
	for i in range(10 ** 5):
		if check(n, v, p, cnt, i):
			print(i)
			return
	print(-1)

def check(n, v, p, cnt, m):
	tot = n + m
	solved = cnt[:]
	dif = 0
	for i in range(5):
		if p[i] != -1 and v[i] > p[i]:
			solved[i] += m
	for i in range(5):
		if solved[i] * 2 > tot:
			max_score = 500
		elif solved[i] * 4 > tot:
			max_score = 1000
		elif solved[i] * 8 > tot:
			max_score = 1500
		elif solved[i] * 16 > tot:
			max_score = 2000
		elif solved[i] * 32 > tot:
			max_score = 2500
		else:
			max_score = 3000
		if v[i] == p[i] == -1:
			pass
		elif v[i] == -1:
			dif -= max_score * (250 - p[i]) // 250
		elif p[i] == -1:
			dif += max_score * (250 - v[i]) // 250
		else:
			dif += max_score * (p[i] - v[i]) // 250
	return dif > 0
solve()


n = int(input())
t = [0] * n
for i in range(n):
	t[i] = list(map(int, input().split()))
value = [0] * 5
solve = [0] * 5
for i in range(5):
	solved = 0
	for j in range(n):
		if t[j][i] != -1:
			solved += 1
		else:
			t[j][i] = 250
	if solved * 2 > n:
		value[i] = 500
	elif solved * 4 > n:
		value[i] = 1000
	elif solved * 8 > n:
		value[i] = 1500
	elif solved * 16 > n:
		value[i] = 2000
	elif solved * 32 > n:
		value[i] = 2500
	else:
		value[i] = 3000
	solve[i] = solved
vasya = 0
for i in range(5):
	vasya += value[i] - value[i] // 250 * t[0][i]
petya = 0
for i in range(5):
	petya += value[i] - value[i] // 250 * t[1][i]
pot = [[0] * 20000 for i in range(5)]
for problem in range(5):
	if t[0][problem] < t[1][problem]:
		cur_ac = 0
		while 1:
			if solve[problem] * 32 <= n + cur_ac:
				win = 3000 - 3000 // 250 * t[0][problem] - (3000 - 3000 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 16 <= n + cur_ac:
				win = 2500 - 2500 // 250 * t[0][problem] - (2500 - 2500 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 8 <= n + cur_ac:
				win = 2000 - 2000 // 250 * t[0][problem] - (2000 - 2000 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 4 <= n + cur_ac:
				win = 1500 - 1500 // 250 * t[0][problem] - (1500 - 1500 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 2 <= n + cur_ac:
				win = 1000 - 1000 // 250 * t[0][problem] - (1000 - 1000 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			else:
				win = 500 - 500 // 250 * t[0][problem] - (500 - 500 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			cur_ac += 1
			if cur_ac == 20000:
				break
	elif t[0][problem] != 250:
		cur_ac = 0
		while 1:
			if solve[problem] * 32 + cur_ac <= n + cur_ac:
				win = 3000 - 3000 // 250 * t[0][problem] - (3000 - 3000 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 16 + cur_ac <= n + cur_ac:
				win = 2500 - 2500 // 250 * t[0][problem] - (2500 - 2500 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 8 + cur_ac <= n + cur_ac:
				win = 2000 - 2000 // 250 * t[0][problem] - (2000 - 2000 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 4 + cur_ac <= n + cur_ac:
				win = 1500 - 1500 // 250 * t[0][problem] - (1500 - 1500 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			elif solve[problem] * 2 + cur_ac <= n + cur_ac:
				win = 1000 - 1000 // 250 * t[0][problem] - (1000 - 1000 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			else:
				win = 500 - 500 // 250 * t[0][problem] - (500 - 500 // 250 * t[1][problem]) - (value[problem] - value[problem] // 250 * t[0][problem] - (value[problem] - value[problem] // 250 * t[1][problem]))
				pot[problem][cur_ac] = win
			cur_ac += 1
			if cur_ac == 20000:
				break
	else:
		cur_ac = 0
		while 1:
			if solve[problem] * 32 <= n + cur_ac:
				win = -(3000 - 3000 // 250 * t[1][problem]) - -(value[problem] - value[problem] // 250 * t[1][problem])
				pot[problem][cur_ac] = win
			elif solve[problem] * 16 <= n + cur_ac:
				win = -(2500 - 2500 // 250 * t[1][problem]) - -(value[problem] - value[problem] // 250 * t[1][problem])
				pot[problem][cur_ac] = win
			elif solve[problem] * 8 <= n + cur_ac:
				win = -(2000 - 2000 // 250 * t[1][problem]) - -(value[problem] - value[problem] // 250 * t[1][problem])
				pot[problem][cur_ac] = win
			elif solve[problem] * 4 <= n + cur_ac:
				win = -(1500 - 1500 // 250 * t[1][problem]) - -(value[problem] - value[problem] // 250 * t[1][problem])
				pot[problem][cur_ac] = win
			elif solve[problem] * 2 <= n + cur_ac:
				win = -(1000 - 1000 // 250 * t[1][problem]) - -(value[problem] - value[problem] // 250 * t[1][problem])
				pot[problem][cur_ac] = win
			else:
				win = -(500 - 500 // 250 * t[1][problem]) - -(value[problem] - value[problem] // 250 * t[1][problem])
				pot[problem][cur_ac] = win
			cur_ac += 1
			if cur_ac == 20000:
				break
res = -1
maxi = 0
for i in range(5):
	maxi = max(maxi, len(pot[i]))
for i in range(maxi):
	tmp = 0
	for pr in range(5):
		if len(pot[pr]) <= i:
			tmp += pot[pr][-1]
		else:
			tmp += pot[pr][i]
	if tmp > petya - vasya:
		res = i
		break
print(res)


from sys import stdin, stdout
fractionBoundaries = [1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 0]
scoreList = [500, 1000, 1500, 2000, 2500, 3000]

def assessScores(n, Vsol, Psol, solvers):
	vScore = 0
	pScore = 0
	for i in range(5):
		for j in range(6):
			if solvers[i] / n > fractionBoundaries[j]:
				qnScore = scoreList[j]
				break
		if Vsol[i] >= 0:
			vScore += qnScore - qnScore * Vsol[i] // 250
		if Psol[i] >= 0:
			pScore += qnScore - qnScore * Psol[i] // 250
	return vScore > pScore

def main():
	n = int(stdin.readline().rstrip())
	Vsol = [int(x) for x in stdin.readline().rstrip().split()]
	Psol = [int(x) for x in stdin.readline().rstrip().split()]
	solvers = [0] * 5
	for _ in range(n - 2):
		a = [int(x) for x in stdin.readline().rstrip().split()]
		for i in range(5):
			if a[i] >= 0:
				solvers[i] += 1
	for i in range(5):
		if Vsol[i] >= 0:
			solvers[i] += 1
		if Psol[i] >= 0:
			solvers[i] += 1
	newSolvers = 0
	vWins = []
	pWins = []
	for i in range(5):
		if Vsol[i] < Psol[i] and Vsol[i] >= 0 or (Vsol[i] >= 0 and Psol[i] < 0):
			vWins.append(i)
		elif Psol[i] < Vsol[i] and Psol[i] >= 0 and (Vsol[i] >= 0):
			pWins.append(i)
	if len(vWins) == 0:
		print(-1)
	else:
		while not assessScores(n + newSolvers, Vsol, Psol, solvers) and newSolvers <= 100000007:
			solversNeeded = 9999999999999
			for i in range(5):
				if i in vWins:
					currentRatio = solvers[i] / (newSolvers + n)
					for j in range(6):
						if solvers[i] / (newSolvers + n) > fractionBoundaries[j]:
							nextBoundary = fractionBoundaries[j]
							break
					if nextBoundary != 0:
						if solvers[i] % nextBoundary == 0:
							solversNeeded = min([solvers[i] // nextBoundary - (newSolvers + n), solversNeeded])
						else:
							solversNeeded = min([solvers[i] // nextBoundary - (newSolvers + n) + 1, solversNeeded])
				elif i in pWins and Vsol[i] > 0:
					currentRatio = solvers[i] / (newSolvers + n)
					for j in range(6):
						if solvers[i] / (newSolvers + n) > fractionBoundaries[j]:
							if j > 0:
								nextBoundary = fractionBoundaries[j - 1]
								solversNeeded = min([(nextBoundary * (newSolvers + n) - solvers[i]) // (1 - nextBoundary) + 1, solversNeeded])
							break
			newSolvers += solversNeeded
			for x in pWins:
				solvers[x] += solversNeeded
		if newSolvers > 1000000007:
			print(-1)
		else:
			print(int(newSolvers))
main()


def f(v, x, n):
	if v < 0:
		return 0
	elif x << 1 > n:
		return int(500 * (1 - v / 250))
	elif x << 2 > n:
		return int(1000 * (1 - v / 250))
	elif x << 3 > n:
		return int(1500 * (1 - v / 250))
	elif x << 4 > n:
		return int(2000 * (1 - v / 250))
	elif x << 5 > n:
		return int(2500 * (1 - v / 250))
	else:
		return int(3000 * (1 - v / 250))
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
c = [sum((_[i] >= 0 for _ in a)) for i in range(5)]
ans = -1
for i in range(10000):
	(p, q) = (0, 0)
	for j in range(5):
		(x, y) = (c[j], n)
		if a[0][j] > a[1][j] and a[1][j] >= 0:
			x += i
		p += f(a[0][j], x, n + i)
		q += f(a[1][j], x, n + i)
	if p > q:
		ans = i
		break
print(ans)