In this challenge, the task is to debug the existing code to successfully execute all provided test files.

There are $n$ boxes in front of you. For each $\boldsymbol{i}$, box $\boldsymbol{i}$ contains $r[i]$ red balls, $g[i]$ green balls, and $b[i]$ blue balls. 

You want to separate the balls by their color. In each operation, you can pick a single ball from some box and put it into another box. The balls are separated if no box contains balls of more than one color.

Debug the given function min_operations and compute the minimal number of operations required to separate the balls.

Note: In this problem you can modify at most six lines of code and you cannot add any new lines.

To restore the original code, click on the icon to the right of the language selector.

Input Format

The first line contains a single integer $n$.
The next $n$ lines $\boldsymbol{i}$ contain three space-separated integers, $r[i]$, $g[i]$, and $b[i]$, respectively.

Constraints

$1\leq n\leq100$ 

$0\leq r[i],\ g[i],\ b[i]\leq105$ 

Output Format

Print the minimal number of operations required to separate the balls. If this is impossible, return $-1$.

Sample Input
3
1 1 1
1 1 1
1 1 1

Sample Output
6

Explanation

Each box contains 1 ball of each color.  In this explanation, the goal will be to let the first box contain only red balls, the second box only blue balls, and the third box only green balls.   

Move 1 blue ball and 1 green ball from the first box to the second and third boxes.   
Move 1 red ball and 1 green ball from the second box to the first and third boxes.   
Move 1 red ball and 1 blue ball from the third box to the first and second boxes.   

The number of operations is 6.

Difficulty: MEDIUM

Solutions:
def Max(a, b, c):
	if a > b:
		if a > c:
			return a
		else:
			return c
	elif b > c:
		return b
	else:
		return c

def main():
	n = int(input())
	a = [[0 for i in range(3)] for j in range(n)]
	for i in range(n):
		a[i] = list(map(int, input().split()))
	(tr, tb, tg) = (0, 0, 0)
	for i in range(n):
		if a[i][0] > 0:
			tr = 1
		if a[i][1] > 0:
			tg = 1
		if a[i][2] > 0:
			tb = 1
	if tr + tg + tb > n:
		print(-1)
	else:
		Moves = 0
		for i in range(n):
			Moves = Moves + a[i][0] + a[i][1] + a[i][2] - Max(a[i][0], a[i][1], a[i][2])
		print(Moves)
main()


def min_operations(r, g, b):
	if len(r) < sum(map(any, (r, g, b))):
		return -1
	else:
		c = sum(r + g + b) - sum(map(max, (r, g, b)))
	return c
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
d = int(min_operations(r, g, b))
print(d)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
red = []
green = []
blue = []
m = 0
for i in range(n):
	(r, g, b) = map(int, input().split())
	red.append(r)
	green.append(g)
	blue.append(b)
ri = red.index(max(red))
gi = green.index(max(green))
bi = blue.index(max(blue))
m = sum(red) - red[ri] + sum(green) - green[gi] + sum(blue) - blue[bi]
print(m)


def min_operations(boxes, n):
	count = 0
	if n == 1:
		return -1
	for i in range(n):
		s = []
		for j in range(3):
			s.append(boxes[j][i])
		s.sort()
		count += sum(s[:n - 1])
	return count
boxes = list()
n = int(input())
for _ in range(n):
	box = list(map(int, input().split()))
	boxes.append(box)
result = min_operations(boxes, n)
print(result)


n = int(input())
i = 0
l = []
if n < 3:
	print('-1')
	exit()
while i < n:
	k = 0
	l1 = []
	t = input()
	x = t.split()
	y = 0
	while y < len(x):
		o = int(x[y])
		l1.append(o)
		y = y + 1
	l.append(l1)
	i = i + 1
q = 0
c = 0
while q < 3:
	l2 = l.copy()
	i = 0
	while i < n - 1:
		j = 0
		while j < n - 1 - i:
			if l2[j][q] > l2[j + 1][q]:
				t = l2[j]
				l2[j] = l2[j + 1]
				l2[j + 1] = t
			j = j + 1
		i = i + 1
	t = 1
	while t < n:
		c = c + l2[t][q]
		t = t + 1
	q = q + 1
print(c)


l = []
r = 0
b = 0
g = 0
r1 = 0
b1 = 0
g1 = 0
n = int(input())
if n == 1:
	print(-1)
else:
	for i in range(n):
		k = [int(x) for x in input().split()]
		r += k[0]
		g += k[1]
		b += k[2]
		if k[0] > r1:
			r1 = k[0]
		if k[1] > g1:
			g1 = k[1]
		if k[2] > b1:
			b1 = k[2]
	print(abs(r1 - r + g1 - g + b1 - b))


boxes_number = int(input())
boxes = []
reds = 0
blues = 0
greens = 0
for i in range(boxes_number):
	boxes.append(input())
if len(boxes) > 1:
	for i in range(len(boxes) - 1):
		reds = reds + int(boxes[i + 1][0])
		blues = blues + int(boxes[i + 1][2])
		greens = greens + int(boxes[i + 1][4])
	total = reds + blues + greens
	print(total)
else:
	print('-1')


n = int(input())
ar = []
for i in range(n):
	inp = []
	inp = list(map(int, input().split(' ')))
	ar.append(inp)
sum = 0
for k in range(n):
	for l in range(3):
		sum = sum + int(ar[k][l])
if n == 1:
	print('-1')
else:
	print(sum - n)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


t = int(input())
for x in range(t):
	n = input()
if t == 1:
	print(-1)
else:
	print(t * 2)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
arr = []
for box in range(n):
	dic = {}
	(r, g, b) = list(map(int, input().split()))
	dic['r'] = r
	dic['g'] = g
	dic['b'] = b
	dic['box'] = box
	arr.append(dic)
(max_r, box_r, max_g, box_g, max_b, box_b) = (0, 0, 0, 0, 0, 0)
for box in arr:
	if max_r < box['r']:
		max_r = box['r']
		box_r = box['box']
	if max_g < box['g']:
		max_g = box['g']
		box_g = box['box']
	if max_b < box['b']:
		max_b = box['b']
		box_b = box['box']
if box_r == box_g == box_b:
	box_g += 1
	box_b += 2
if n <= 2:
	print('-1')
else:
	transf = 0
	for box in arr:
		if box['box'] == box_r:
			transf += box['g']
			transf += box['b']
		elif box['box'] == box_g:
			transf += box['r']
			transf += box['b']
		elif box['box'] == box_b:
			transf += box['r']
			transf += box['g']
		else:
			transf += box['r']
			transf += box['g']
			transf += box['b']
	print(transf)


res = 0
for i in range(int(input())):
	box = list(map(int, input().strip().split()))
	res += sum(box[1:])
print(res)


def min_operations(boxes):
	if len(boxes) < 3:
		return -1
	highest = [-1] * 3
	total = [0] * 3
	res = 0
	for i in range(len(boxes)):
		box = boxes[i]
		for j in range(3):
			total[j] += box[j]
			highest[j] = max(highest[j], box[j])
	for k in range(3):
		res += total[k] - highest[k]
	return res
n = int(input())
boxes = []
for _ in range(n):
	b = input()
	b = b.split()
	b = [int(i) for i in b]
	boxes.append(b)
print(min_operations(boxes))


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


try:
	li = [list(map(int, input().split())) for i in range(int(input()))][::-1]
	(b, r, g) = (li[0].index(max(li[0])), li[1].index(max(li[1])), li[2].index(max(li[2])))
	print(sum(li[0]) + sum(li[1]) + sum(li[2]) - li[0][b] - li[1][r] - li[2][g])
except IndexError:
	print(-1)


from itertools import chain

def process(nb, boxes):
	counter = [sum(x) for x in zip(*boxes)]
	if len(counter) > len(boxes) and all((counter[i] > 1 for i in range(len(counter)))):
		return str(-1)
	fr = []
	largest = sorted(list(chain.from_iterable(boxes)))[::-1]
	for i in range(3):
		val = largest[0]
		while [index for (index, row) in enumerate(boxes) if val in row][0] in fr:
			pos = [[index, row.index(val)] for (index, row) in enumerate(boxes) if val in row][0]
			boxes[pos[0]][pos[1]] = 0
			del largest[0]
			val = largest[0]
		fr.append([index for (index, row) in enumerate(boxes) if val in row][0])
		del largest[0]
	count = counter[0] - fr[0] + counter[1] - fr[1] + counter[2] - fr[2]
	return str(count)
nb = int(input())
boxes = []
for n in range(nb):
	boxes.append([int(i) for i in input().split()])
print(process(nb, boxes))


import sys
num1 = int(input())
num2 = [[int(i) for i in input().split()] for j in range(num1)]
x = 0
for j in num2:
	sum = 0
	for i in j:
		sum += i
		if sum > len(j):
			print('-1')
			x = sum
			sys.exit(0)
		else:
			continue
count = 0
if x == 0:
	for j in num2:
		for i in j:
			if i >= 1:
				count += 1
			elif i == 0:
				continue
	print(count - len(num2))


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
a = []
r = []
g = []
b = []
for i in range(n):
	l = list(input().split(' '))
	r.append(int(l[0]))
	g.append(int(l[1]))
	b.append(int(l[2]))
r.sort()
g.sort()
b.sort()
if n > 2:
	sum1 = sum(r) - r[-1]
	sum2 = sum(g) - g[-1]
	sum3 = sum(b) - b[-1]
	print(sum1 + sum2 + sum3)
else:
	print('-1')


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


count = 0
boxes = int(input())
if boxes < 3:
	count = -1
else:
	for _ in range(boxes):
		arr = list(map(int, input().rstrip().split()))
		arr.remove(max(arr))
		count += sum(arr)
print(count)


n = int(input())
lst = []
for i in range(n):
	lst.append([int(x) for x in input().split(' ')])
count = 0
for (index, box) in enumerate(lst):
	for ball_index in range(len(box)):
		if ball_index != index:
			count += 1
print(count)


n = int(input())
if n < 3:
	print(-1)
else:
	a = []
	for i in range(n):
		a.append(list(map(lambda x: int(x), input().split(' '))))
	print(n * 2)


num_boxes = int(input())
if num_boxes < 3:
	print(-1)
else:
	(r, g, b) = (0, 0, 0)
	for _ in range(num_boxes):
		(ri, gi, bi) = map(int, input().split())
		r += ri
		g += gi
		b += bi
	print(sum([r, g, b]) - max([r, g, b]))


import operator
from functools import reduce
n = int(input())
bins = []
(colour, num_correct_balls) = ([0] * n, [0] * n)
exist_ball = [False] * 3
for i in range(n):
	bins.append(list(map(int, input().rstrip().split())))
	exist_ball = list(map(lambda x, y: x or bool(y), exist_ball, bins[i]))
	(colour[i], num_correct_balls[i]) = max(enumerate(bins[i]), key=operator.itemgetter(1))
total_balls = sum((sum(row) for row in bins))

def minOps():
	if sum(exist_ball) > n:
		return -1
	need_bin = []
	for c in range(3):
		if exist_ball[c] and c not in colour:
			need_bin.append(c)
	if not need_bin:
		excess = 0
	elif len(need_bin) == 1:
		safe_to_take = [c for c in range(3) if colour.count(c) >= 2]
		if len(safe_to_take) == 2:
			excess = list(map(lambda row: max(row) - row[need_bin[0]], bins))
			excess = min(excess)
		else:
			candidate = [i for i in range(n) if colour[i] == safe_to_take[0]]
			excess = list(map(lambda row: max(row) - row[need_bin[0]], [bins[i] for i in candidate]))
			excess = min(excess)
	else:
		(c1, c2) = need_bin
		candidate = {}
		for c in need_bin:
			all_excesses = [max(row) - row[c] for row in bins]
			candidate[c] = sorted(enumerate(all_excesses), key=operator.itemgetter(1))
			candidate[c] = candidate[c][:2]
		if candidate[c1][0][0] == candidate[c2][0][0]:
			excess_a = candidate[c1][0][1] + candidate[c2][1][1]
			excess_b = candidate[c1][1][1] + candidate[c2][0][1]
			excess = min([excess_a, excess_b])
		else:
			excess = candidate[c1][0][1] + candidate[c2][0][1]
	return total_balls - sum(num_correct_balls) - excess
print(minOps())


n = int(input())
arr = []
for i in range(0, n):
	arr.append(input().split(' '))
if n == 1:
	print(-1)
else:
	print(n * n - n)


n = int(input())
balls = []
red_balls = 0
green_balls = 0
blue_balls = 0
most_red_balls = 0
most_green_balls = 0
most_blue_balls = 0
if n < 3:
	print(-1)
else:
	for i in range(n):
		box = input().split()
		balls.append([int(j) for j in box])
		red_balls += balls[-1][0]
		green_balls += balls[-1][1]
		blue_balls += balls[-1][2]
		if balls[-1][0] > most_red_balls:
			most_red_balls = balls[-1][0]
		if balls[-1][1] > most_green_balls:
			most_green_balls = balls[-1][1]
		if balls[-1][2] > most_blue_balls:
			most_blue_balls = balls[-1][2]
	total_operations = red_balls - most_red_balls + (green_balls - most_green_balls) + (blue_balls - most_blue_balls)
	print(total_operations)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
l = []
if n < 3:
	print(-1, end='')
else:
	for i in range(0, n):
		s = []
		x = input()
		s = x.split()
		s = list(map(int, s))
		l.append(s)
	c = 0
	for i in range(0, n):
		for j in range(0, 3):
			if i != j:
				c = c + l[i][j]
	print(c, end='')


n = int(input())
op = (n - 1) * n
if n == 1:
	op = -1
print(op)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
count = 0
matrix = []
minn1 = 0
minn2 = 0
minn3 = 0
p1 = 0
p2 = 0
p3 = 0
for i in range(n):
	a = list(map(int, input().rstrip().split()))
	matrix.append(a)
if n < 3:
	print('-1')
else:
	for j in range(n):
		p = matrix[j][0]
		q = matrix[j][1]
		r = matrix[j][2]
		if p > minn1:
			minn1 = p
			p1 = j
		if q > minn2:
			minn2 = q
			p2 = j
		if r > minn3:
			minn3 = r
			p3 = j
	for k in range(n):
		if k != p1:
			count += matrix[k][0]
		if k != p2:
			count += matrix[k][1]
		if k != p3:
			count += matrix[k][2]
	print(count)


def min_operations(red, green, blue, n):
	count_ = 0
	if n > 1:
		count_ += sum(red[1:])
	if n >= 2:
		count_ += sum(green[0:1] + green[2:])
	if n >= 3:
		count_ += sum(blue[0:2] + blue[3:])
	return count_ if count_ != 0 else -1
n = int(input())
red = []
green = []
blue = []
for i in range(n):
	(r, g, b) = map(int, input().split())
	red.append(r)
	green.append(g)
	blue.append(b)
print(min_operations(red, green, blue, n))


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
(r, b, g) = ([], [], [])
t = 0
c = 0
for i in range(n):
	(x, y, z) = map(int, input().split())
	t = t + (x + y + z)
	r.append(x)
	b.append(y)
	g.append(z)
if n != 3:
	print(-1)
else:
	print(t - (r[0] + b[1] + g[2]))


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(int(min_operations(r, g, b)))


print(6)


def solve(boxes):
	answer = ''
	num_colors = 0
	color_counts = [0, 0, 0]
	other_colors = []
	best_boxes = [{}, {}, {}]
	stolen_boxes = []
	assignments = {}
	if True:
		for (i, box) in enumerate(boxes):
			color_counts[0] += box[0]
			color_counts[1] += box[1]
			color_counts[2] += box[2]
			a = box[1] + box[2]
			b = box[0] + box[2]
			c = box[0] + box[1]
			other_color_counts = [a, b, c]
			other_colors.append(other_color_counts)
			best_boxes[0][i] = a
			best_boxes[1][i] = b
			best_boxes[2][i] = c
			assignments[i] = 0
			if b < a:
				assignments[i] = 1
			elif c < a and c < b:
				assignments[i] = 2
	colors = []
	for (i, color_count) in enumerate(color_counts):
		if color_count > 0:
			num_colors += 1
			colors.append(i)
	draft_choices = {}
	for color_num in range(3):
		draft_choices[color_num] = [v[0] for v in sorted(best_boxes[color_num].items(), key=lambda kv: (-kv[1], kv[0]))]
	for color in colors:
		for draft_choice in draft_choices[color]:
			if draft_choice not in stolen_boxes:
				assignments[draft_choice] = color
				stolen_boxes.append(draft_choice)
				break
	answer = 0
	for (box, color) in assignments.items():
		answer += other_colors[box][color]
	if len(boxes) < num_colors:
		answer = -1
	print(f'{answer}')
ar_count = int(input())
boxes = []
for i in range(ar_count):
	color_counts = input()
	boxes.append([int(x) for x in color_counts.split()])
solve(boxes)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
if n == 1:
	print(-1)
	exit()
(operations, d) = (0, 0)
for i in range(n):
	a = list(map(int, input().split()))
	for j in range(n):
		if j != d:
			operations += a[j]
		if j == n - 1:
			d += 1
print(operations)


def min_operations(r, g, b):
	return -1 if len(r) < sum(map(any, (r, g, b))) else sum(r + g + b) - sum(map(max, (r, g, b)))
r = []
g = []
b = []
for _ in range(int(input())):
	(ri, gi, bi) = map(int, input().split(' '))
	r += [ri]
	g += [gi]
	b += [bi]
print(min_operations(r, g, b))


n = int(input())
lst = []
for i in range(n):
	lst.append([int(x) for x in input().split(' ')])
count = 0
for (index, box) in enumerate(lst):
	for ball_index in range(len(box)):
		if ball_index != index:
			count += 1
print(count)


n = int(input())
mat = []
for i in range(0, n):
	l1 = list(map(int, input().split(' ')))
	mat.append(l1)
	l1 = []
if len(mat) < 3:
	print(-1)
else:
	ri = 0
	bi = 0
	gi = 0
	r = mat[0][0]
	g = mat[0][1]
	b = mat[0][2]
	for i in range(0, n):
		if mat[i][0] > r:
			r = mat[i][0]
			ri = i
		if mat[i][1] > g:
			g = mat[i][1]
			gi = i
		if mat[i][2] > b:
			b = mat[i][2]
			bi = i
	s = 0
	for i in range(n):
		if i == ri:
			continue
		else:
			s = s + mat[i][0]
	for i in range(n):
		if i == gi:
			continue
		else:
			s = s + mat[i][1]
	for i in range(n):
		if i == bi:
			continue
		else:
			s = s + mat[i][2]
	print(s)


n = int(input())
if n != 3:
	print(-1)
else:
	(l, r, t) = (list(map(int, input().split())), list(map(int, input().split())), list(map(int, input().split())))
	print(sum(l) + sum(r) + sum(t) - l[0] - r[1] - t[2])


a = int(input())
count = 0
x = []
for i in range(a):
	x.append([str(j) for j in input().split()])
print(a * 2)


n = int(input())
arr = []
for i in range(n):
	b = list(map(int, input().split()))
	arr.append(b)
(maxr, maxg, maxb) = (0, 0, 0)
(cr, cg, cb) = (0, 0, 0)
for i in range(n):
	if maxr < arr[i][0]:
		maxr = arr[i][0]
	if maxg < arr[i][1]:
		maxg = arr[i][1]
	if maxb < arr[i][2]:
		maxb = arr[i][2]
	cr = cr + arr[i][0]
	cg = cg + arr[i][1]
	cb = cb + arr[i][2]
print(cr - maxr + (cg - maxg) + (cb - maxb))