Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.

The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.


-----Input-----

You are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0.


-----Output-----

Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky.


-----Examples-----
Input
000000

Output
0

Input
123456

Output
2

Input
111000

Output
1



-----Note-----

In the first example the ticket is already lucky, so the answer is 0.

In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.

In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.

Difficulty: MEDIUM_HARD
Tags: Complete search

Implementation

Greedy algorithms
Skill types: Complete search

Greedy algorithms

Starter code:

Solutions:
s = list(map(int, input()))
ans = 9 * 6 + 1
for a in range(10):
	for b in range(10):
		for c in range(10):
			for d in range(10):
				for e in range(10):
					for f in range(10):
						if a + b + c == d + e + f:
							ans = min(ans, [a == s[0], b == s[1], c == s[2], d == s[3], e == s[4], f == s[5]].count(False))
print(ans)


ticket = [int(x) for x in input()]
fst = ticket[:3]
sec = ticket[3:]
diff = abs(sum(fst) - sum(sec))
if sum(fst) > sum(sec):
	(fst, sec) = (sec, fst)
if diff == 0:
	print(0)
elif 9 - min(fst) >= diff or max(sec) >= diff:
	print(1)
elif 18 - (sum(fst) - max(fst)) >= diff or sum(sec) - min(sec) >= diff or 9 - min(fst) + max(sec) >= diff:
	print(2)
else:
	print(3)


ticket = input()
(q1, q2) = ([(int(i), j) for (j, i) in enumerate(ticket[:3])], [(int(i), j) for (j, i) in enumerate(ticket[3:])])
(p1, p2) = ([i for (i, j) in q1], [i for (i, j) in q2])
if sum(p1) > sum(p2):
	(p1, p2) = (p2, p1)
	(q1, q2) = (q2, q1)
if sum(p1) == sum(p2):
	print(0)
	exit()
for i in range(20):
	if 9 - min(p1) > max(p2):
		pos = min(q1)[1]
		p1[pos] = 9
		q1[pos] = (9, pos)
	else:
		pos = max(q2)[1]
		p2[pos] = 0
		q2[pos] = (0, pos)
	if sum(p1) >= sum(p2):
		print(i + 1)
		exit()


l = list(input())
for i in range(6):
	l[i] = int(l[i])
l1 = l[:3]
l2 = l[3:]
s1 = sum(l[:3])
s2 = sum(l[3:])
if s1 == s2:
	print(0)
elif s1 > s2:
	x = s1 - s2
	ls = []
	for i in range(3):
		ls.append(l1[i])
		ls.append(9 - l2[i])
	ls.sort(reverse=True)
	i = 0
	jud = 0
	ans = 0
	while jud == 0:
		ans += ls[i]
		if ans < x:
			i += 1
		else:
			print(i + 1)
			jud = 1
else:
	x = s2 - s1
	ls = []
	for i in range(3):
		ls.append(l2[i])
		ls.append(9 - l1[i])
	ls.sort(reverse=True)
	i = 0
	jud = 0
	ans = 0
	while jud == 0:
		ans += ls[i]
		if ans < x:
			i += 1
		else:
			print(i + 1)
			jud = 1


def build(b):
	s = 0
	b = b[::-1]
	for i in range(6):
		s += b[i] * 10 ** i
	return s
t = [-1] * 1000000
n = int(input())
t[n] = 0
v = [n]
while v != []:
	a = v.pop(0)
	x = []
	while a != 0:
		x = [a % 10] + x
		a //= 10
	x = [0] * (6 - len(x)) + x
	if sum(x[:3]) == sum(x[3:]):
		print(t[build(x)])
		break
	for i in range(6):
		for j in range(10):
			s = build(x[:i] + [j] + x[i + 1:])
			if t[s] == -1:
				t[s] = t[build(x)] + 1
				v += [s]


n = input()
(a, b, c, d, e, f) = map(int, n)

def g(a, b, c, s):
	(m1, m2, m3) = sorted([a, b, c])
	if s == a + b + c:
		return 0
	elif s > a + b + c:
		s1 = a + b + c
		if m2 + m3 + 9 >= s:
			return 1
		if m3 + 18 >= s:
			return 2
		else:
			return 3
	else:
		if m1 + m2 <= s:
			return 1
		if m1 <= s:
			return 2
		return 3
ll = []
for s in range(28):
	ll.append(g(a, b, c, s) + g(d, e, f, s))
print(min(ll))


s = list(map(int, input()))
ans = 6
for d0 in range(10):
	for d1 in range(10):
		for d2 in range(10):
			for d3 in range(10):
				for d4 in range(10):
					d5 = d0 + d1 + d2 - d3 - d4
					if 0 <= d5 <= 9:
						d = [d0, d1, d2, d3, d4, d5]
						delta = 0
						for i in range(6):
							if s[i] != d[i]:
								delta += 1
						ans = min(delta, ans)
print(ans)


s = input()
l = list()
l1 = list(map(int, s[:3]))
l2 = list(map(int, s[3:]))
if sum(l1) > sum(l2):
	(l1, l2) = (l2, l1)
(suml, sumr) = (sum(l1), sum(l2))
for (i, j) in zip(l1, l2):
	l.append(9 - i)
	l.append(j)
l.sort(reverse=True)
ans = 0
for i in l:
	if suml >= sumr:
		break
	suml += i
	ans += 1
print(ans)


s = list(map(int, input()))
left_sum = sum(s[:3])
right_sum = sum(s[3:])
if left_sum >= right_sum:
	abc_min = s[3:]
	abc_max = s[:3]
elif left_sum < right_sum:
	abc_min = s[:3]
	abc_max = s[3:]
dif = max(left_sum, right_sum) - min(left_sum, right_sum)
ans = 0
for k in range(3):
	if max(abc_max) > 9 - min(abc_min):
		dif -= max(abc_max)
		del abc_max[abc_max.index(max(abc_max))]
	else:
		dif -= 9 - min(abc_min)
		del abc_min[abc_min.index(min(abc_min))]
	ans += 1
	if dif <= 0:
		break
if left_sum == right_sum:
	print(0)
else:
	print(ans)


def f(x):
	return sum(x[:3]) == sum(x[3:])

def main():
	a = [int(i) for i in input()]
	if f(a):
		return 0
	for i in range(6):
		for j in range(10):
			b = a[:]
			b[i] = j
			if f(b):
				return 1
	for i in range(6):
		for j in range(i):
			for k in range(10):
				for l in range(10):
					b = a[:]
					b[i] = k
					b[j] = l
					if f(b):
						return 2
	return 3
print(main())


num = input()
l = [int(_) for _ in num]
if sum(l[3:]) > sum(l[:3]):
	(l[:3], l[3:]) = (l[3:], l[:3])
l[:3] = sorted(l[:3], reverse=True)
l[3:] = sorted(l[3:], reverse=True)
answer = 0
i = 0
j = 5
while sum(l[:3]) > sum(l[3:]):
	answer += 1
	if l[i] > 9 - l[j]:
		l[i] = 0
		i += 1
	else:
		l[j] = 9
		j -= 1
print(answer)


def comp(n, m):
	rtn = 0
	for i in range(6):
		if n[i] != m[i]:
			rtn += 1
	return rtn
n = list(map(int, list(input())))
ans = 6
for i in range(1000000):
	m = []
	for _ in range(6):
		m.append(i % 10)
		i //= 10
	m = list(reversed(m))
	if sum(m[:3]) == sum(m[3:]):
		ans = min(ans, comp(n, m))
print(ans)


s = [int(k) for k in input()]
from itertools import product
l = set([i for i in range(10)])
cnt = 6
ck = 0
for a in l:
	for b in l:
		for c in l:
			for d in l:
				for e in l:
					for f in l:
						if a + b + c != d + e + f:
							continue
						p = [a, b, c, d, e, f]
						nx = len([i for i in range(6) if p[i] != s[i]])
						cnt = min(cnt, nx)
print(cnt)


a = [int(q) for q in list(input())]
b = a[0:3]
c = a[3:6]
min = sum(b)
max = sum(c)
t = 5
if min > max:
	(min, max) = (max, min)
	(b, c) = (c, b)
if min == max:
	print(0)
else:
	b.sort()
	c.sort()
	if min in c and min != 0 and (max - min > 9 - b[0]):
		if 0 in c or max - min <= 9 - b[0] or max - min <= c[2]:
			print(1)
		else:
			print(2)
	elif max - min <= 9 - b[0] or max - min <= c[2]:
		print(1)
	elif max - min - (9 - b[0]) <= 9 - b[1] or max - min - c[2] <= c[1]:
		print(2)
	elif 9 - b[0] + c[2] >= max - min:
		print(2)
	else:
		print(3)


s = input()
a = [0, 0, 0, 0, 0, 0]
t = []
ans = 3
for i in s:
	t.append(ord(i) - ord('0'))
for a[0] in range(10):
	for a[1] in range(10):
		for a[2] in range(10):
			for a[3] in range(10):
				for a[4] in range(10):
					for a[5] in range(10):
						anss = 6
						if a[0] + a[1] + a[2] == a[3] + a[4] + a[5]:
							for i in range(6):
								if a[i] == t[i]:
									anss = anss - 1
						if anss < ans:
							ans = anss
print(ans)


s = input()
l = []
for d in s[:3]:
	l.append(int(d))
u = []
for d in s[3:]:
	u.append(int(d))
if sum(l) == sum(u):
	print(0)
else:
	if sum(l) > sum(u):
		(l, u) = (u, l)
	diffs = sorted(list(map(lambda x: 9 - x, l)) + u)[::-1]
	for i in range(1, 7):
		if sum(diffs[:i]) >= sum(u) - sum(l):
			print(i)
			break


a = list(map(int, input()))
b = a[:3]
a = a[3:]
if sum(a) > sum(b):
	(a, b) = (b, a)
d = sum(b) - sum(a)
if d == 0:
	print(0)
	exit()
t = list(b)
for i in a:
	t.append(9 - i)
for (i, j) in enumerate(sorted(t, reverse=True)):
	d -= j
	if d <= 0:
		print(i + 1)
		break


n = input()
arr = []
for i in n:
	arr += [i]
arr = list(map(int, arr))
total = arr[0] + arr[1] + arr[2] - arr[3] - arr[4] - arr[5]
if total == 0:
	print(0)
elif total > 0:
	arr[3] = 9 - arr[3]
	arr[4] = 9 - arr[4]
	arr[5] = 9 - arr[5]
	arr.sort()
	i = 0
	while total > 0:
		total -= arr[5 - i]
		i += 1
	print(i)
else:
	arr[0] = 9 - arr[0]
	arr[1] = 9 - arr[1]
	arr[2] = 9 - arr[2]
	arr.sort()
	total = -total
	arr.sort()
	i = 0
	while total > 0:
		total -= arr[5 - i]
		i += 1
	print(i)


n = list(input())
n = [int(n[i]) for i in range(6)]
s1 = sum(n)
s = sum(n[:3])
s1 -= s
if s > s1:
	n = n[::-1]
sw = []
sw.append(9 - n[0])
sw.append(9 - n[1])
sw.append(9 - n[2])
sw.append(n[-3])
sw.append(n[-2])
sw.append(n[-1])
ab = abs(s1 - s)
sw.sort(reverse=True)
i = 0
ans = 0
while ab > 0:
	ab -= sw[i]
	ans += 1
	i += 1
print(ans)


a = [int(i) for i in input()]
b = sorted([9 - a[i] for i in range(3)])
d = sorted([9 - a[i] for i in range(3, 6)])
c = sum(a[:3]) - sum(a[3:])
s = 0
if c < 0:
	c = -c
	t = 2
	r = 2
	q = []
	q += a[3:]
	q.sort()
	while c > 0:
		if q[r] > b[t]:
			c -= q[r]
			s += 1
			r -= 1
		else:
			c -= b[t]
			s += 1
			t -= 1
else:
	t = 2
	r = 2
	q = []
	q += a[:3]
	q.sort()
	while c > 0:
		if q[r] > d[t]:
			c -= q[r]
			s += 1
			r -= 1
		else:
			c -= d[t]
			s += 1
			t -= 1
print(s)


i = list(map(lambda c: int(c), input()))
d = sum([i[3] - i[0], i[4] - i[1], i[5] - i[2]])
if d < 0:
	i.reverse()
	d *= -1
for z in range(3):
	i[z] = 9 - i[z]
i = sorted(i)
i.reverse()
ir = 0
for (ind, z) in enumerate(i):
	if ir >= d:
		print(ind)
		break
	ir += z


a = list(input())
for i in range(6):
	a[i] = int(a[i])
a1 = a[0:3]
a2 = a[3:6]
a1.sort()
a2.sort()
ans = []
s1 = sum(a1)
s2 = sum(a2)
if s1 == s2:
	ans.append(0)
	ans.append(0)
elif s1 > s2:
	if a1[1] + a1[2] <= s2 or a1[0] + a1[2] <= s2 or a1[0] + a1[1] <= s2:
		ans.append(1)
	elif a1[0] <= s2 or a1[1] <= s2 or a1[2] <= s2:
		ans.append(2)
	else:
		ans.append(3)
	if s1 <= 9 + a2[0] + a2[1] or s1 <= 9 + a2[1] + a2[2] or s1 <= 9 + a2[0] + a2[2]:
		ans.append(1)
	elif s1 <= 18 + a2[0] or s1 <= 18 + a2[1] or s1 <= 18 + a2[2]:
		ans.append(2)
	else:
		ans.append(3)
	if a1[0] + a1[1] <= s2:
		ans.append(1)
	elif a1[0] + a1[1] <= 9 + a2[1] + a2[2]:
		ans.append(2)
	else:
		ans.append(3)
else:
	if a2[1] + a2[2] <= s1 or a2[0] + a2[2] <= s1 or a2[0] + a2[1] <= s1:
		ans.append(1)
	elif a2[0] <= s1 or a2[1] <= s1 or a2[2] <= s1:
		ans.append(2)
	else:
		ans.append(3)
	if s2 <= 9 + a1[0] + a1[1] or s2 <= 9 + a1[1] + a1[2] or s2 <= 9 + a1[0] + a1[2]:
		ans.append(1)
	elif s2 <= 18 + a1[0] or s2 <= 18 + a1[1] or s2 <= 18 + a1[2]:
		ans.append(2)
	else:
		ans.append(3)
	if a2[0] + a2[1] <= s1:
		ans.append(1)
	elif a2[0] + a2[1] <= 9 + a1[1] + a1[2]:
		ans.append(2)
	else:
		ans.append(3)
print(min(ans))


def digitsum(x):
	s = 0
	while x > 0:
		s += x % 10
		x //= 10
	return s

def main():
	ds = [digitsum(x) for x in range(1000)]
	n = input()
	ans = 6
	for i in range(1000000):
		if ds[i // 1000] == ds[i % 1000]:
			s = '{:06d}'.format(i)
			diff = sum((1 for (i, c) in enumerate(n) if c != s[i]))
			ans = min(ans, diff)
	print(ans)
main()


m = list(map(int, input()))
ans = 0
if sum(m[:3]) > sum(m[3:]):
	m = m[3:] + m[:3]
while sum(m[:3]) != sum(m[3:]):
	ans += 1
	minsum1 = 10
	maxsum2 = 0
	mini = -1
	maxi = -1
	for i in range(3):
		if m[i] < minsum1:
			minsum1 = m[i]
			mini = i
	for i in range(3, 6):
		if m[i] > maxsum2:
			maxsum2 = m[i]
			maxi = i
	var1 = m[0:6]
	var1[mini] = sum(var1[3:]) - sum(var1[:3]) + minsum1
	if var1[mini] > 9:
		var1[mini] = 9
	var2 = m[0:6]
	var2[maxi] = maxsum2 - (sum(var2[3:]) - sum(var2[:3]))
	if var2[maxi] < 0:
		var2[maxi] = 0
	if sum(var1[3:]) - sum(var1[:3]) < sum(var2[3:]) - sum(var2[:3]):
		m = var1
	else:
		m = var2
print(ans)


s = list(input())
l1 = []
l2 = []
l = []
(a, sa) = (0, 0)
for i in range(3):
	l1.append(int(s[i]))
	l2.append(int(s[i + 3]))
if sum(l1) > sum(l2):
	(l1, l2) = (l2, l1)
d = sum(l2) - sum(l1)
for i in range(3):
	l.append(9 - l1[i])
	l.append(l2[i])
l.sort()
for i in range(5, -1, -1):
	if sa >= d:
		break
	sa += l[i]
	a += 1
print(a)


t = list(input())
s = int(t[0]) + int(t[1]) + int(t[2])
s1 = int(t[3]) + int(t[4]) + int(t[5])

def first(s, s1, t):
	t = t[:3]
	todo = s1 - s
	if 9 - int(min(t, key=lambda x: int(x))) < todo:
		small = 9 - int(min(t[:2], key=lambda x: int(x)))
		t.remove(min(t, key=lambda x: int(x)))
		if 9 - int(min(t, key=lambda x: int(x))) + small < todo:
			return 3
		else:
			return 2
	else:
		return 1

def secend(s, s1, t):
	t = t[3:]
	todo = s - s1
	if 9 - int(min(t, key=lambda x: int(x))) < todo:
		smaller = 9 - int(min(t, key=lambda x: int(x)))
		t.remove(min(t, key=lambda x: int(x)))
		if 9 - int(min(t, key=lambda x: int(x))) + smaller < todo:
			return 3
		else:
			return 2
	else:
		return 1

def third(s, s1, t):
	t = t[:3]
	todo = s - s1
	if int(max(t, key=lambda x: int(x))) < todo:
		small = int(max(t, key=lambda x: int(x)))
		t.remove(max(t, key=lambda x: int(x)))
		if int(max(t, key=lambda x: int(x))) + small < todo:
			return 3
		else:
			return 2
	else:
		return 1

def fourth(s, s1, t):
	t = t[3:]
	todo = s1 - s
	if int(max(t, key=lambda x: int(x))) < todo:
		small = int(max(t, key=lambda x: int(x)))
		t.remove(max(t, key=lambda x: int(x)))
		if int(max(t, key=lambda x: int(x))) + small < todo:
			return 3
		else:
			return 2
	else:
		return 1

def fifth(s, s1, t):
	todo = s1 - s
	if int(max(t[3:], key=lambda x: int(x))) + (9 - int(min(t[:3], key=lambda x: int(x)))) >= todo:
		return 2
	else:
		return 3

def sixth(s, s1, t):
	todo = s - s1
	if int(max(t[:3], key=lambda x: int(x))) + (9 - int(min(t[3:], key=lambda x: int(x)))) >= todo:
		return 2
	else:
		return 3
if s != s1:
	if s > s1:
		print(min(third(s, s1, t), secend(s, s1, t), sixth(s, s1, t)))
	else:
		print(min(fourth(s, s1, t), first(s, s1, t), fifth(s, s1, t)))
else:
	print(0)


a = input()
(b1, b2) = (0, 0)
for i in range(3):
	b1 += int(a[i])
for i in range(3):
	b2 += int(a[3 + i])
if b1 > b2:
	p = 0
	d = []
	c = list(a[3:])
	c = list(map(int, c))
	for i in c:
		d.append(9 - i)
	c = list(a[:3])
	c = list(map(int, c))
	for i in c:
		d.append(i)
	d.sort()
	d.reverse()
	for i in range(len(d)):
		p += d[i]
		if p >= b1 - b2:
			print(i + 1)
			break
elif b1 < b2:
	p = 0
	d = []
	c = list(a[:3])
	c = list(map(int, c))
	for i in c:
		d.append(9 - i)
	c = list(a[3:])
	c = list(map(int, c))
	for i in c:
		d.append(i)
	d.sort()
	d.reverse()
	for i in range(len(d)):
		p += d[i]
		if p >= b2 - b1:
			print(i + 1)
			break
else:
	print(0)


num = input()
p1 = [int(x) for x in num[:3]]
p2 = [int(x) for x in num[3:]]
temp = p1[:] if sum(p1) < sum(p2) else p2[:]
p2 = p2 if sum(p2) >= sum(p1) else p1[:]
p1 = temp
_min = 0
while sum(p1) < sum(p2):
	if 9 - min(p1) > max(p2):
		p1[p1.index(min(p1))] = 9
	else:
		p2[p2.index(max(p2))] = 0
	_min += 1
print(_min)


n = input()
m = len(n)
arr = []
arr1 = []
a = int(n[0])
b = int(n[1])
c = int(n[2])
x = int(n[m - 3])
y = int(n[m - 2])
z = int(n[m - 1])
p = a + b + c
q = x + y + z
if p == q:
	print(0)
	exit()
arr = [a, b, c]
arr1 = [x, y, z]
arr.sort()
arr1.sort()
i = 0
j = 0
if p > q:
	i = 2
	j = 0
	s = p - q
	s1 = s
	s2 = s
	count = 0
	count1 = 0
	while 1:
		if 9 - arr1[j] < s:
			count += 1
			s1 = s1 - (9 - arr1[j])
		else:
			count += 1
			print(count + count1)
			exit()
		if arr[i] < s:
			count1 += 1
			s2 = s2 - arr[i]
		else:
			count1 += 1
			print(count1 + count - 1)
			exit()
		if s1 <= s2:
			s = s1
			j += 1
			count1 -= 1
		else:
			s = s2
			i -= 1
			count -= 1
	print(count + count1)
else:
	j = 2
	i = 0
	s = q - p
	s1 = s
	s2 = s
	count = 0
	count1 = 0
	while 1:
		if 9 - arr[i] < s:
			count += 1
			s1 = s1 - (9 - arr[i])
		else:
			count += 1
			print(count + count1)
			exit()
		if arr1[j] < s:
			count1 += 1
			s2 = s2 - arr1[j]
		else:
			count1 += 1
			print(count1 + count - 1)
			exit()
		if s1 <= s2:
			s = s1
			i += 1
			count1 -= 1
		else:
			s = s2
			j -= 1
			count -= 1
	print(count + count1)


S = input()
a = S[:3]
b = S[3:]

def Sum(st):
	n = 0
	for x in st:
		n += int(x)
	return n

def g(s, t):
	diff = int(t[0]) + int(t[1]) + int(t[2]) - int(s[0]) - int(s[1]) - int(s[2])
	ma = 9 - int(s[0])
	c = 0
	if 9 - int(s[1]) > ma:
		c = 1
		ma = 9 - int(s[c])
	if 9 - int(s[2]) > ma:
		c = 2
		ma = 9 - int(s[c])
	mm = int(t[0])
	c2 = 0
	if int(t[1]) > mm:
		mm = int(t[1])
		c2 = 1
	if int(t[2]) > mm:
		mm = int(t[2])
		c2 = 2
	if ma > mm:
		C = str(int(s[c]) + min(diff, ma))
		s = s[:c] + C + s[c + 1:]
	else:
		C = str(int(t[c2]) - min(diff, mm))
		t = t[:c2] + C + t[c2 + 1:]
	return (s, t)

def f():
	nonlocal a, b
	if Sum(a) == Sum(b):
		print(0)
		return
	if Sum(a) < Sum(b):
		n = 0
		while Sum(a) != Sum(b):
			(a, b) = g(a, b)
			n += 1
		print(n)
		return
	n = 0
	while Sum(a) != Sum(b):
		(b, a) = g(b, a)
		n += 1
	print(n)
f()


a = input()
b = list(a)
c = [int(x) for x in b]
d = c[:3]
e = c[3:]
f = []
if sum(d) == sum(e):
	print(0)
elif sum(d) > sum(e):
	for i in d:
		f.append(i)
	for j in e:
		f.append(9 - j)
	f.sort()
	k = sum(d) - sum(e)
	if k > f[-1]:
		if k > f[-1] + f[-2]:
			print(3)
		else:
			print(2)
	else:
		print(1)
else:
	for i in d:
		f.append(9 - i)
	for j in e:
		f.append(j)
	f.sort()
	k = sum(e) - sum(d)
	if k > f[-1]:
		if k > f[-1] + f[-2]:
			print(3)
		else:
			print(2)
	else:
		print(1)


l = list(input())
a = list(map(int, l))
l1 = a[0:3]
l2 = a[3:6]
if sum(l1) > sum(l2):
	b = l2
	l2 = l1
	l1 = b
l1.sort()
l2.sort()
d = sum(l2) - sum(l1)
if d == 0:
	print('0')
else:
	i = 0
	j = 2
	c = 0
	while d > 0:
		if l1 == []:
			d -= l2[j]
			l2.remove(l2[j])
			j -= 1
			c += 1
		elif l2 == []:
			d -= 9 - l1[0]
			l1.remove(l[0])
			c += 1
		elif 9 - l1[0] >= l2[j]:
			d -= 9 - l1[i]
			l1.remove(l1[i])
			c += 1
		else:
			d -= l2[j]
			l2.remove(l2[j])
			j -= 1
			c += 1
	print(c)


s = input()
a = list(s[:3])
b = list(s[3:6])
sa = int(a[0]) + int(a[1]) + int(a[2])
sb = int(b[0]) + int(b[1]) + int(b[2])
if sa == sb:
	print(0)
	exit()
if sa > sb:
	c = a
	a = b
	b = c
r = abs(sa - sb)
ans = 1
for i in range(2):
	mina = min(int(a[0]), int(a[1]), int(a[2]))
	maxb = max(int(b[0]), int(b[1]), int(b[2]))
	mina = 9 - mina
	if mina >= r or maxb >= r:
		print(ans)
		exit()
	else:
		ans += 1
	r -= max(mina, maxb)
	if mina >= maxb:
		ch = 1
	else:
		ch = 0
	mina = 9 - mina
	if ch:
		if int(a[0]) == mina:
			a[0] = 9
		elif int(a[1]) == mina:
			a[1] = 9
		else:
			a[2] = 9
	elif int(b[0]) == maxb:
		b[0] = 0
	elif int(b[1]) == maxb:
		b[1] = 0
	else:
		b[2] = 0
print(3)


s = [int(x) for x in input()]
ans = 6
k = 0
for d0 in range(10):
	for d1 in range(10):
		for d2 in range(10):
			for d3 in range(10):
				for d4 in range(10):
					d5 = d0 + d1 + d2 - d3 - d4
					if 0 <= d5 <= 9:
						d = [d0, d1, d2, d3, d4, d5]
						de = 0
						for i in range(6):
							if s[i] != d[i]:
								de += 1
						k += 1
						ans = min(de, ans)
print(ans)


inp = input()
li = []
for i in inp:
	li.append(int(i))
sum1 = sum(li[:3])
sum2 = sum(li[3:])
if sum2 > sum1:
	for i in range(3):
		li[i] = 9 - li[i]
	for i in range(3, 6):
		li[i] = li[i]
else:
	for i in range(3):
		li[i] = li[i]
	for i in range(3, 6):
		li[i] = 9 - li[i]
li.sort(reverse=True)
dif = max(sum1, sum2) - min(sum1, sum2)
cur = 0
for i in range(6):
	if cur >= dif:
		print(i)
		exit(0)
	cur += li[i]


a = input()
ls = int(a[0]) + int(a[1]) + int(a[2])
rs = int(a[3]) + int(a[4]) + int(a[5])
dif = ls - rs
if dif == 0:
	print(0)
	quit()
p = [0] * 6
for i in range(6):
	if (i < 3) ^ (dif > 0):
		p[i] = 9 - int(a[i])
	else:
		p[i] = int(a[i])
p.sort(reverse=True)
ps = 0
for i in range(6):
	ps += p[i]
	if ps >= abs(dif):
		print(i + 1)
		break


def main():
	digits = input()
	li = list(digits)
	digits = list(map(int, li))
	first = digits[:3]
	second = digits[3:]
	first.sort()
	second.sort()
	sumf = sum(first)
	sums = sum(second)
	if sumf == sums:
		print('0')
		return
	elif sumf < sums:
		small = 1
	else:
		small = 2
	if small == 1:
		diff = sums - sumf
		cnt1 = 0
		for i in range(0, 3):
			diff = diff + first[i]
			if diff <= 9:
				cnt1 = cnt1 + 1
				break
			else:
				diff = diff - 9
				cnt1 = cnt1 + 1
		cnt2 = 0
		diff = sums - sumf
		for i in range(2, -1, -1):
			diff = diff - second[i]
			if diff <= 0:
				cnt2 = cnt2 + 1
				break
			else:
				cnt2 = cnt2 + 1
		result = min(cnt1, cnt2)
	else:
		cnt1 = 0
		diff = sumf - sums
		for i in range(0, 3):
			diff = diff + second[i]
			if diff <= 9:
				cnt1 = cnt1 + 1
				break
			else:
				diff = diff - 9
				cnt1 = cnt1 + 1
		cnt2 = 0
		diff = sumf - sums
		for i in range(2, -1, -1):
			diff = diff - first[i]
			if diff <= 0:
				cnt2 = cnt2 + 1
				break
			else:
				cnt2 = cnt2 + 1
		result = min(cnt1, cnt2)
	temp = 9
	if small == 1:
		for i in range(0, 3):
			for j in range(0, 3):
				diff = sums - sumf
				diff = diff + first[i] - 9
				diff = diff - second[j]
				if diff <= 0:
					temp = 2
					break
	else:
		for i in range(0, 3):
			for j in range(0, 3):
				diff = sumf - sums
				diff = diff + second[i] - 9
				diff = diff - first[j]
				if diff <= 0:
					temp = 2
					break
	result = min(result, temp)
	print(result)
main()


s = input()
arr = [int(i) for i in s]
count = 0
while count < 4:
	a = arr[0] + arr[1] + arr[2]
	b = arr[3] + arr[4] + arr[5]
	r = abs(a - b)
	if a == b:
		print(count)
		exit()
	if a < b:
		num_min = arr.index(min(arr[0], arr[1], arr[2]))
		num_max = arr.index(max(arr[3], arr[4], arr[5]))
	else:
		num_min = arr.index(min(arr[3], arr[4], arr[5]))
		num_max = arr.index(max(arr[0], arr[1], arr[2]))
	if r <= arr[num_max]:
		print(count + 1)
		exit()
	if r <= 9 - arr[num_min]:
		print(count + 1)
		exit()
	if 9 - arr[num_min] > arr[num_max]:
		arr[num_min] = 9
	else:
		arr[num_max] = 0
	count += 1
print(count)


s = input()
a = [int(x) for x in s[:3]]
b = [int(x) for x in s[3:]]
a.sort()
b.sort()
if sum(a) > sum(b):
	(a, b) = (b, a)
if sum(a) == sum(b):
	print(0)
	exit()
dt = sum(b) - sum(a)
diffs = []
for i in a:
	diffs.append(9 - i)
diffs.extend(b)
diffs.sort()
i = 0
while dt > 0:
	dt -= diffs[5 - i]
	i += 1
print(i)


s = input()
v1 = v2 = 0
for i in s[:3]:
	v1 += int(i)
for i in s[3:]:
	v2 += int(i)
ans = 0
s1 = list(s[:3])
for i in range(3):
	s1[i] = int(s1[i])
s2 = list(s[3:])
for i in range(3):
	s2[i] = int(s2[i])
if v1 < v2:
	while True:
		ans += 1
		if 9 - min(s1) > max(s2):
			v1 = v1 + 9 - min(s1)
			s1.remove(min(s1))
			s1.append(9)
			if v1 >= v2:
				break
		else:
			v2 = v2 - max(s2)
			s2.remove(max(s2))
			s2.append(0)
			if v2 <= v1:
				break
	print(ans)
elif v1 > v2:
	while True:
		ans += 1
		if 9 - min(s2) < max(s1):
			v1 = v1 - max(s1)
			s1.remove(max(s1))
			s1.append(0)
			if v1 <= v2:
				break
		else:
			v2 = v2 + 9 - min(s2)
			s2.remove(min(s2))
			s2.append(9)
			if v2 >= v1:
				break
	print(ans)
else:
	print(ans)


n = input()
a = []
b = []
for i in range(3):
	a.append(int(n[i]))
for i in range(3, 6):
	b.append(int(n[i]))
c = sum(a)
d = sum(b)
e = []
f = abs(c - d)
count = 0
if c == d:
	print(0)
elif c > d:
	e += a
	for i in range(len(b)):
		e.append(9 - b[i])
	e.sort()
	e.reverse()
	while f > 0:
		f -= e[count]
		count += 1
	print(count)
else:
	e += b
	for i in range(len(a)):
		e.append(9 - a[i])
	e.sort()
	e.reverse()
	while f > 0:
		f -= e[count]
		count += 1
	print(count)


A = [int(x) for x in list(input())]
B = sorted(A[:3])
C = sorted(A[3:6])
b = sum(B)
c = sum(C)
a = b - c
if a != 0:
	if a < 0:
		D = sorted([9 - x for x in B] + C)
	elif a > 0:
		D = sorted(B + [9 - x for x in C])
	D.reverse()
	d = 0
	result = 0
	for x in D:
		d += x
		result += 1
		if d >= abs(a):
			break
	print(result)
else:
	print(0)


num = input()
p1 = sorted([int(x) for x in num[:3]])
p2 = sorted([int(x) for x in num[3:]])
c1 = p1[:] if sum(p1) < sum(p2) else p2[:]
c2 = list(reversed(p1[:] if sum(p1) > sum(p2) else p2[:]))
_min = 0
while sum(c1) < sum(c2):
	if 9 - min(c1) > max(c2):
		c1[c1.index(min(c1))] = 9
	else:
		c2[c2.index(max(c2))] = 0
	_min += 1
print(_min)


a = list(str(input()))
a = [int(i) for i in a]
one = a[:3]
two = a[3:]
o = sum(one)
t = sum(two)
one.sort()
two.sort()
s = 0
c = []
if o == t:
	s = 0
elif o > t:
	b = o - t
	c.append(one[0])
	c.append(one[1])
	c.append(one[2])
	c.append(9 - two[0])
	c.append(9 - two[1])
	c.append(9 - two[2])
	c.sort()
	c.reverse()
	e = 0
	for i in range(0, 6):
		e += c[i]
		if e >= b:
			s = i + 1
			break
else:
	b = t - o
	c.append(two[0])
	c.append(two[1])
	c.append(two[2])
	c.append(9 - one[0])
	c.append(9 - one[1])
	c.append(9 - one[2])
	c.sort()
	c.reverse()
	e = 0
	for i in range(0, 6):
		e += c[i]
		if e >= b:
			s = i + 1
			break
print(s)


line = [int(x) for x in list(input())]
l1 = line[0:3]
l2 = line[3:]
s1 = sum(l1)
s2 = sum(l2)
s = abs(s1 - s2)
ans = 0
if s1 > s2:
	l2 = list(map(lambda x: 9 - x, l2))
	l = l1 + l2
	l.sort(reverse=True)
	for i in l:
		if s > 0:
			s -= i
			ans += 1
		else:
			break
elif s2 > s1:
	l1 = list(map(lambda x: 9 - x, l1))
	l = l1 + l2
	l.sort(reverse=True)
	for i in l:
		if s > 0:
			s -= i
			ans += 1
		else:
			break
print(ans)


a = list(map(int, input()))
a_first = a[:3]
a_last = a[3:]
sum_a_first = sum(a_first)
sum_a_last = sum(a_last)
if sum_a_first != sum_a_last:
	a_first.sort()
	a_last.sort()
	if sum_a_first > sum_a_last:
		(a_first, a_last) = (a_last, a_first)
		(sum_a_first, sum_a_last) = (sum_a_last, sum_a_first)
	count_change_a_first = 1
	sum_temp = sum_a_first
	for i in range(3):
		temp = a_first[i]
		is_break = False
		while temp < 9:
			temp += 1
			sum_temp += 1
			if sum_temp == sum_a_last:
				is_break = True
				break
		if is_break:
			break
		count_change_a_first += 1
	count_change_a_last = 1
	sum_temp = sum_a_last
	for i in range(3):
		temp = a_last[3 + ~i]
		is_break = False
		while temp > 0:
			temp -= 1
			sum_temp -= 1
			if sum_temp == sum_a_first:
				is_break = True
				break
		if is_break:
			break
		count_change_a_last += 1
	sum_a_first_temp = sum_a_first
	sum_a_last_temp = sum_a_last
	first_a_first = a_first[0]
	last_a_last = a_last[2]
	index = 0
	is_both = False
	while first_a_first < 9 or last_a_last > 0:
		if index % 2 == 0:
			if first_a_first < 9:
				first_a_first += 1
				sum_a_first_temp += 1
			else:
				last_a_last -= 1
				sum_a_last_temp -= 1
		elif last_a_last > 0:
			last_a_last -= 1
			sum_a_last_temp -= 1
		else:
			first_a_first += 1
			sum_a_first_temp += 1
		if sum_a_first_temp == sum_a_last_temp:
			is_both = True
			break
		index += 1
	if is_both:
		print(min(count_change_a_first, count_change_a_last, 2))
	else:
		print(min(count_change_a_first, count_change_a_last))
else:
	print(0)


def readline():
	return map(int, input().split())

def main():
	number = list(map(int, input()))
	(steps, line) = (0, [number])

	def diff(n):
		return sum(n[0:3]) - sum(n[3:])

	def next(n):
		d = diff(n)
		for i in range(6):
			if d > 0 and i < 3 or (d < 0 and i >= 3):
				r = range(n[i])
			else:
				r = range(n[i] + 1, 10)
			for digit in r:
				m = n.copy()
				m[i] = digit
				yield m
	while True:
		next_line = list()
		for number in line:
			if diff(number) == 0:
				print(steps)
				return
			next_line.extend(next(number))
		(steps, line) = (steps + 1, next_line)
main()


import pdb
digits = [int(i) for i in input()]

def three_digits_sum(digits):
	first_three = 0
	last_three = 0
	for i in range(3):
		first_three += digits[i]
		last_three += digits[i + 3]
	return [first_three, last_three]

def calculate(digits):
	og_sum = three_digits_sum(digits)
	diff = abs(og_sum[0] - og_sum[1])
	if diff == 0:
		return 0
	else:
		if og_sum[0] < og_sum[1]:
			lower = list(digits[0:3])
			upper = list(digits[3:6])
		else:
			lower = list(digits[3:6])
			upper = list(digits[0:3])
		biggest_diff_amt = 0
		biggest_diff_idx = 0
		biggest_diff_set = None
		biggest_diff_list = None
		for i in range(3):
			el_diff = 9 - lower[i]
			if el_diff >= diff:
				return 1
			elif el_diff > biggest_diff_amt:
				biggest_diff_list = lower
				biggest_diff_idx = i
				biggest_diff_set = 9
				biggest_diff_amt = el_diff
		for i in range(3):
			el_diff = upper[i]
			if el_diff >= diff:
				return 1
			elif el_diff > biggest_diff_amt:
				biggest_diff_list = upper
				biggest_diff_idx = i
				biggest_diff_set = 0
				biggest_diff_amt = el_diff
		biggest_diff_list[biggest_diff_idx] = biggest_diff_set
		return 1 + calculate(lower + upper)
print(calculate(digits))


numbers = input()
numbers = [n for n in map(lambda n: int(n), numbers)]

def check_lucky(list):
	return sum(list[:3]) == sum(list[3:])
if check_lucky(numbers):
	print(0)
	exit()

def incDigit(x):
	x += 1
	if x > 9:
		x = 0
	return x
minReplaced = 7
replaced = 0
index = 0

def checkForDigit(index, list, replaced):
	global minReplaced
	replaced += 1
	for x in range(10):
		if x != numbers[index]:
			list[index] = x
			if check_lucky(list):
				minReplaced = min(replaced, minReplaced)
for i in range(6):
	newNumbers = [x for x in numbers]
	checkForDigit(i, newNumbers, 0)
if minReplaced == 1:
	print(1)
	exit()
for i in range(6):
	newNumbers = [x for x in numbers]
	for t in range(10):
		if t != numbers[i]:
			newNumbers[i] = t
			for z in range(i, 6, 1):
				newNumbers2 = [y for y in newNumbers]
				checkForDigit(z, newNumbers2, 1)
if minReplaced == 2:
	print(2)
	exit()
print(3)


def work():
	s = input()
	a = [int(s[0]), int(s[1]), int(s[2])]
	b = [int(s[3]), int(s[4]), int(s[5])]
	if sum(a) == sum(b):
		print(0)
		return
	if sum(a) > sum(b):
		(a, b) = (b, a)
	a = sorted(a)
	b = sorted(b)
	ben = [9 - a[0], 9 - a[1], 9 - a[2], b[0], b[1], b[2]]
	ben = sorted(ben)[::-1]
	k = sum(b) - sum(a)
	t = 0
	i = 0
	while t < k:
		t += ben[i]
		i += 1
	print(i)
	return
work()


l = list(input())
a = []
b = []
for i in range(3):
	a.append(int(l[i]))
for i in range(3, 6):
	b.append(int(l[i]))
a.sort()
b.sort()
s1 = sum(a)
s2 = sum(b)
if s1 == s2:
	print(0)
elif s1 < s2:
	diff = s2 - s1
	a.sort()
	b.sort(reverse=True)
	c = []
	t = 0
	for i in range(3):
		c.append(9 - a[i])
		c.append(b[i])
	c.sort(reverse=True)
	for i in range(6):
		t = t + c[i]
		if t >= diff:
			break
	print(i + 1)
else:
	diff = s1 - s2
	t = 0
	a.sort(reverse=True)
	b.sort()
	c = []
	for i in range(3):
		c.append(a[i])
		c.append(9 - b[i])
	c.sort(reverse=True)
	for i in range(6):
		t = t + c[i]
		if t >= diff:
			break
	print(i + 1)


def lucky(s):
	ds = []
	while s:
		ds.append(s % 10)
		s //= 10
	while len(ds) < 6:
		ds.append(0)
	return ds[0] + ds[1] + ds[2] == ds[3] + ds[4] + ds[5]

def difs(a, b):
	d = 0
	while a or b:
		if a % 10 != b % 10:
			d += 1
		a //= 10
		b //= 10
	return d

def main():
	s = int(input())
	min_difs = 10
	end = 10 ** 6
	for s_t in range(0, end):
		if lucky(s_t):
			min_difs = min(min_difs, difs(s, s_t))
			if min_difs == 0:
				break
	print(min_difs)

def __starting_point():
	main()
__starting_point()


digs = list(map(int, input()))
(l, r) = (min(digs[:3], digs[3:], key=sum), max(digs[:3], digs[3:], key=sum))
ans = 0
while sum(r) - sum(l) > 0:
	if 9 - min(l) >= max(r):
		diff = 9 - min(l)
		l[l.index(min(l))] = 9
	else:
		diff = max(r)
		r[r.index(max(r))] = 0
	ans += 1
print(ans)


a = []
for i in range(10):
	for j in range(10):
		for k in range(10):
			for l in range(10):
				for m in range(10):
					for n in range(10):
						if i + j + k == l + m + n:
							a.append(str(i) + str(j) + str(k) + str(l) + str(m) + str(n))
b = input()
d = []
for i in range(len(a)):
	c = 0
	for j in range(6):
		if a[i][j] != b[j]:
			c += 1
	d.append(c)
print(min(d))


s = input()
(m, t) = (6, [[] for i in range(28)])
for i in range(10):
	for j in range(10):
		for k in range(10):
			t[i + j + k].append(str(i) + str(j) + str(k))
for q in t:
	for u in q:
		for v in q:
			m = min(m, sum((x != y for (x, y) in zip(u + v, s))))
print(m)


ticket = input()
num1 = int(ticket[0])
num2 = int(ticket[1])
num3 = int(ticket[2])
num4 = int(ticket[3])
num5 = int(ticket[4])
num6 = int(ticket[5])
minDif = 7
for l in range(1000):
	leftDif = 0
	if l % 10 != num3:
		leftDif += 1
	if l // 10 % 10 != num2:
		leftDif += 1
	if l // 100 != num1:
		leftDif += 1
	if leftDif < minDif:
		numSum = l % 10 + l // 10 % 10 + l // 100
		i = 0
		while i <= numSum and i <= 9:
			j = 0
			while j <= numSum - i and j <= 9:
				k = numSum - i - j
				if k <= 9:
					rightDif = 0
					if i != num4:
						rightDif += 1
					if j != num5:
						rightDif += 1
					if k != num6:
						rightDif += 1
					if leftDif + rightDif < minDif:
						minDif = leftDif + rightDif
				j += 1
			i += 1
print(minDif)


s = input()
l1 = [int(s[i]) for i in range(3)]
l2 = [int(s[3 + i]) for i in range(3)]
s1 = sum(l1)
s2 = sum(l2)
if s1 < s2:
	(l1, l2) = (l2, l1)
	(s1, s2) = (s2, s1)
l1.sort(reverse=True)
l2.sort()
ans = 0
p1 = 0
p2 = 0
while s1 > s2:
	if l1[p1] > 9 - l2[p2]:
		s1 -= l1[p1]
		p1 += 1
	else:
		s2 += 9 - l2[p2]
		p2 += 1
	ans += 1
print(ans)


s = input()
s = [int(i) for i in s]
(a, b) = (s[:3], s[3:])
if sum(a) > sum(b):
	(a, b) = (b, a)
t = sum(b) - sum(a)
a = [9 - i for i in a]
a = a + b
a.sort(reverse=True)
if t == 0:
	print(0)
elif a[0] >= t:
	print(1)
elif a[0] + a[1] >= t:
	print(2)
else:
	print(3)


inp = input()
inp = str(inp)
a = [0] * 6
ticket = []
for c in inp:
	ticket.append(int(c))
sum1 = 0
sum2 = 0
for i in range(3):
	sum1 += ticket[i]
	a[i] = ticket[i]
for i in range(3, 6):
	sum2 += ticket[i]
	a[i] = ticket[i]
sum3 = sum1 - sum2
if sum3 > 0:
	for i in range(3, 6):
		a[i] = 9 - a[i]
else:
	for i in range(3):
		a[i] = 9 - a[i]
	sum3 = -sum3
a = sorted(a)
printed = False
for i in range(5, 1, -1):
	if sum3 <= 0:
		print(5 - i)
		printed = True
		break
	sum3 -= a[i]
if not printed:
	print(0)


a = [int(i) for i in input()]
if sum(a[3:]) > sum(a[:3]):
	(a[:3], a[3:]) = (a[3:], a[:3])
a[:3] = sorted(a[:3], reverse=True)
a[3:] = sorted(a[3:], reverse=True)
ans = 0
i = 0
j = 5
while sum(a[:3]) > sum(a[3:]):
	ans += 1
	if a[i] > 9 - a[j]:
		a[i] = 0
		i += 1
	else:
		a[j] = 9
		j -= 1
print(ans)


s = list(input())
a = []
b = []
for i in range(0, 3):
	a.append(int(s[i]))
for i in range(3, 6):
	b.append(int(s[i]))
s1 = sum(a)
s2 = sum(b)
a.sort()
b.sort()
for i in range(0, 3):
	a.append(s1 - a[i])
for i in range(0, 3):
	b.append(s2 - b[i])
if s1 == s2:
	print(0)
else:
	for i in range(0, 3):
		if s1 - b[i + 3] >= 0 and s1 - b[i + 3] <= 9:
			print(1)
			exit()
	for i in range(0, 3):
		if s2 - a[i + 3] >= 0 and s2 - a[i + 3] <= 9:
			print(1)
			exit()
	for i in range(0, 3):
		if s1 - b[i] >= 0 and s1 - b[i] <= 18:
			print(2)
			exit()
	for i in range(0, 3):
		if s2 - a[i] >= 0 and s2 - a[i] <= 18:
			print(2)
			exit()
	for i in range(3, 6):
		x = a[i]
		for j in range(3, 6):
			if abs(x - b[j]) <= 9:
				print(2)
				exit()
	print(3)


s = list(input())
a = list(map(int, s[:3]))
b = list(map(int, s[3:]))
p = sum(a)
q = sum(b)
if p == q:
	print(0)
elif p > q:
	diff = p - q
	num1 = 0
	b = sorted(b)
	a = sorted(a, reverse=True)
	diff -= 9 - b[0]
	if diff <= 0:
		num1 = 1
	else:
		diff -= 9 - b[1]
		if diff <= 0:
			num1 = 2
		else:
			num1 = 3
		diff = p - q - (9 - b[0])
		diff -= a[0]
		if diff <= 0:
			num1 = min(num1, 2)
		else:
			num1 = min(num1, 3)
	diff = p - q
	diff -= a[0]
	if diff <= 0:
		num2 = min(1, num1)
	else:
		diff -= a[1]
		if diff <= 0:
			num2 = min(2, num1)
		else:
			num2 = min(3, num1)
		diff = p - q - a[0]
		diff -= 9 - b[0]
		if diff <= 0:
			num2 = min(num2, 2)
		else:
			num2 = min(num2, 3)
	print(num2)
else:
	diff = q - p
	num1 = 0
	a = sorted(a)
	b = sorted(b, reverse=True)
	diff -= 9 - a[0]
	if diff <= 0:
		num1 = 1
	else:
		diff -= 9 - a[1]
		if diff <= 0:
			num1 = 2
		else:
			num1 = 3
		diff = q - p - (9 - a[0])
		diff -= b[0]
		if diff <= 0:
			num1 = min(num1, 2)
		else:
			num1 = min(num1, 3)
	diff = q - p
	diff -= b[0]
	if diff <= 0:
		num2 = min(1, num1)
	else:
		diff -= b[1]
		if diff <= 0:
			num2 = min(2, num1)
		else:
			num2 = min(3, num1)
		diff = q - p - b[0]
		diff -= 9 - a[0]
		if diff <= 0:
			num2 = min(num2, 2)
		else:
			num2 = min(num2, 3)
	print(num2)


import sys

def debug(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)
a = [int(ch) for ch in input()]
assert len(a) == 6
b1 = a[0:3]
b2 = a[3:6]
if sum(b1) > sum(b2):
	(b1, b2) = (b2, b1)
diff = sum(b2) - sum(b1)
deltas = sorted([9 - x for x in b1 if x < 9] + [x for x in b2 if x > 0], reverse=True)
cum_deltas = [0] + deltas[:]
for i in range(1, len(cum_deltas)):
	cum_deltas[i] += cum_deltas[i - 1]
for (i, x) in enumerate(cum_deltas):
	if cum_deltas[i] >= diff:
		break
print(i)


x = int(input())

def s(a):
	r = 0
	while a > 0:
		r += a % 10
		a //= 10
	return r

def d(a, b):
	r = 0
	for i in range(6):
		if a % 10 != b % 10:
			r += 1
		a //= 10
		b //= 10
	return r
c = 6
for i in range(1000000):
	if s(i % 1000) == s(i // 1000):
		c = min(c, d(x, i))
print(c)


s = input()
ans = 6
for i in range(0, 10):
	for j in range(0, 10):
		for k in range(0, 10):
			for f in range(0, 10):
				for f1 in range(0, 10):
					for f2 in range(0, 10):
						if i + j + k == f + f1 + f2:
							cnt = 0
							if i != ord(s[0]) - ord('0'):
								cnt = cnt + 1
							if j != ord(s[1]) - ord('0'):
								cnt = cnt + 1
							if k != ord(s[2]) - ord('0'):
								cnt = cnt + 1
							if f != ord(s[3]) - ord('0'):
								cnt = cnt + 1
							if f1 != ord(s[4]) - ord('0'):
								cnt = cnt + 1
							if f2 != ord(s[5]) - ord('0'):
								cnt = cnt + 1
						ans = min(ans, cnt)
print(ans)


import bisect
from itertools import accumulate, count
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
from sys import maxsize
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return sorted(fac)

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
s = input()
a = [int(x) for x in s[:3]]
b = [int(x) for x in s[3:]]
a.sort()
b.sort()
if sum(a) > sum(b):
	(a, b) = (b, a)
if sum(a) == sum(b):
	print(0)
	exit()
dt = sum(b) - sum(a)
diffs = []
for i in a:
	diffs.append(9 - i)
diffs.extend(b)
diffs.sort()
i = 0
while dt > 0:
	dt -= diffs[5 - i]
	i += 1
print(i)


t = input()
a = []
for i in range(6):
	a.append(int(t[i]))
m = 27
for i in range(10):
	for j in range(10):
		for k in range(10):
			for l in range(10):
				for r in range(10):
					for o in range(10):
						if i + j + k == l + r + o:
							x = 0
							if a[0] != i:
								x += 1
							if a[1] != j:
								x += 1
							if a[2] != k:
								x += 1
							if a[3] != l:
								x += 1
							if a[4] != r:
								x += 1
							if a[5] != o:
								x += 1
							m = min(x, m)
print(m)


from sys import exit

def strsum(s):
	return sum([int(c) for c in s])

def pos_diffs(s):
	return [9 - int(i) for i in s]

def neg_diffs(s):
	return [int(i) for i in s]

def reverse_nums(s):
	return ''.join([str(9 - int(c)) for c in s])
n = input()
first = n[:3]
last = n[3:]
if strsum(first) == strsum(last):
	print('0')
	exit(0)
if strsum(first) < strsum(last):
	tbi = first
	goal = last
else:
	tbi = last
	goal = first
difference = strsum(goal) - strsum(tbi)
current = 0
change_count = 0
diffs = sorted(pos_diffs(tbi) + neg_diffs(goal))
while current < difference:
	current += diffs.pop(-1)
	change_count += 1
print(change_count)


digits = [int(i) for i in input()]
sm = digits[:3]
gr = digits[3:]
if sum(sm) > sum(gr):
	(sm, gr) = (gr, sm)
iters = 0
diff = sum(gr) - sum(sm)
while diff:
	iters += 1
	bestSm = sm.index(min(sm))
	bestGr = gr.index(max(gr))
	if 9 - sm[bestSm] > gr[bestGr]:
		sm[bestSm] = 9 if sm[bestSm] + diff > 9 else sm[bestSm] + diff
	else:
		gr[bestGr] = 0 if gr[bestGr] - diff < 0 else gr[bestGr] - diff
	diff = sum(gr) - sum(sm)
print(iters)


digits = [int(x) for x in input()]
difference = sum(digits[0:3]) - sum(digits[3:])
helper = []
if difference < 0:
	helper = [9 - x for x in digits[:3]] + digits[3:]
else:
	helper = digits[:3] + [9 - x for x in digits[3:]]
helper = sorted(helper)[::-1]
n = 0
sum_ = 0
for x in helper:
	if sum_ >= abs(difference):
		break
	sum_ += x
	n += 1
print(n)


ticket1 = input()
ticket2 = ticket1[3:]
ticket1 = ticket1[:3]
ticket1 = list(map(int, list(ticket1)))
ticket2 = list(map(int, list(ticket2)))
t1 = list.copy(ticket1)
t2 = list.copy(ticket2)
t = list.copy(t1 + t2)
ticket = list.copy(t)
r = sum(ticket1) - sum(ticket2)
(r1, r2, k) = (r, r, 0)
while r1 and r2 and (k < 3):
	r1 -= max(max(t1), 9 - min(t2))
	r2 += max(9 - min(ticket1), max(ticket2))
	k += 1
	if 9 - min(t2) > max(t1):
		t[t.index(min(t2))] = 9
	else:
		t[t.index(max(t1))] = 0
	if 9 - min(ticket1) > max(ticket2):
		ticket[ticket.index(min(ticket1))] = 9
	else:
		ticket[ticket.index(max(ticket2))] = 0
	ticket1 = ticket[:3]
	ticket2 = ticket[3:]
	t1 = t[:3]
	t2 = t[3:]
	if r1 * r2 < 0:
		(r1, r2) = (0, 0)
print(k)


n = input()
f = list(map(int, n[:3]))
l = list(map(int, n[3:]))
sf = sum(f)
sl = sum(l)
f.sort()
l.sort()
s = 0
k = 0
if sf > sl:
	d = sf - sl
	if l[0] + d >= 10:
		d -= 9 - l[0]
		if f[2] - d < 0:
			if l[1] + d >= 10:
				s = 3
			else:
				s = 2
		else:
			s = 2
	else:
		s = 1
	d = sf - sl
	if f[2] - d < 0:
		d -= f[2]
		if f[1] - d < 0:
			k = 3
		else:
			k = 2
	else:
		k = 1
elif sl > sf:
	d = sl - sf
	if f[0] + d >= 10:
		d -= 9 - f[0]
		if l[2] - d < 0:
			if f[1] + d >= 10:
				s = 3
			else:
				s = 2
		else:
			s = 2
	else:
		s = 1
	d = sl - sf
	if l[2] - d < 0:
		d -= l[2]
		if l[1] - d < 0:
			k = 3
		else:
			k = 2
	else:
		k = 1
print(min(k, s))


from __future__ import division, print_function
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)
	print(l)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans
s = list(map(int, list(si())))
B = sorted(s[:3])
C = sorted(s[3:6])
b = sum(B)
c = sum(C)
a = b - c
if a != 0:
	if a < 0:
		D = sorted([9 - x for x in B] + C)
	elif a > 0:
		D = sorted(B + [9 - x for x in C])
	D.reverse()
	d = 0
	result = 0
	for x in D:
		d += x
		result += 1
		if d >= abs(a):
			break
	print(result)
else:
	print(0)


a = [int(i) for i in input()]

def f1(a):
	for i in range(6):
		for j in range(1, 10):
			b = [i for i in a]
			b[i] += j
			b[i] %= 10
			if f0(b):
				return True
	return False

def f0(a):
	return sum(a[:3]) == sum(a[3:])

def f2(a):
	for i1 in range(5):
		for i2 in range(i1 + 1, 6):
			for j1 in range(1, 10):
				for j2 in range(1, 10):
					b = [i for i in a]
					b[i1] += j1
					b[i2] += j2
					b[i1] %= 10
					b[i2] %= 10
					if f0(b):
						return True
	return False
if f0(a):
	print(0)
elif f1(a):
	print(1)
elif f2(a):
	print(2)
else:
	print(3)


entrada = list(map(int, input()))
l = entrada[:3]
r = entrada[3:]
sl = l[0] + l[1] + l[2]
sr = r[0] + r[1] + r[2]
l.sort()
r.sort()
if sl == sr:
	print(0)
elif sl < sr:
	dif = abs(sl - sr)
	if dif <= r[2] or dif + l[0] <= 9:
		print(1)
	elif 9 - l[0] == r[2]:
		dif -= r[2]
		if dif <= r[1] or dif + l[0] <= 9:
			print(2)
		else:
			print(3)
	elif 9 - l[0] < r[2]:
		dif -= r[2]
		if dif <= r[1] or dif + l[0] <= 9:
			print(2)
		else:
			print(3)
	elif 9 - l[0] > r[2]:
		dif -= 9 - l[0]
		if dif <= r[2] or dif + l[1] <= 9:
			print(2)
		else:
			print(3)
else:
	(l, r) = (r, l)
	dif = abs(sl - sr)
	if dif <= r[2] or dif + l[0] <= 9:
		print(1)
	elif 9 - l[0] == r[2]:
		dif -= r[2]
		if dif <= r[1] or dif + l[0] <= 9:
			print(2)
		else:
			print(3)
	elif 9 - l[0] < r[2]:
		dif -= r[2]
		if dif <= r[1] or dif + l[0] <= 9:
			print(2)
		else:
			print(3)
	elif 9 - l[0] > r[2]:
		dif -= 9 - l[0]
		if dif <= r[2] or dif + l[1] <= 9:
			print(2)
		else:
			print(3)


s = input()
min_d = 6
for a in range(10):
	for b in range(10):
		for c in range(10):
			for d in range(10):
				for e in range(10):
					for f in range(10):
						if a + b + c == d + e + f:
							k = 0
							t = str(a) + str(b) + str(c) + str(d) + str(e) + str(f)
							for i in range(6):
								if s[i] != t[i]:
									k += 1
							min_d = min(min_d, k)
print(min_d)


a = input()
m = []
ans = 6
for x in a:
	m.append(int(x))
for d0 in range(10):
	for d1 in range(10):
		for d2 in range(10):
			for d3 in range(10):
				for d4 in range(10):
					d5 = d0 + d1 + d2 - d3 - d4
					if 0 <= d5 <= 9:
						r = 0
						d = [d0, d1, d2, d3, d4, d5]
						for i in range(6):
							if m[i] != d[i]:
								r += 1
						ans = min(ans, r)
print(ans)


def main():
	s = input()
	print(solver(s))

def solver(s):
	L1 = [int(x) for x in s[0:3]]
	L2 = [int(x) for x in s[3:6]]
	diff = sum(L2) - sum(L1)
	if diff == 0:
		return 0
	elif diff < 0:
		(L1, L2) = (L2, L1)
		diff = -diff
	changes = [9 - L1[i] for i in range(3)] + [L2[i] - 0 for i in range(3)]
	changes.sort(reverse=True)
	for i in range(6):
		diff -= changes[i]
		if diff <= 0:
			return i + 1
main()


digits = list(map(int, input().strip()))
left = digits[:3]
ls = sum(left)
right = digits[3:]
rs = sum(right)
if ls > rs:
	(rs, ls) = (ls, rs)
	(left, right) = (right, left)
left.sort()
right.sort(key=lambda x: -x)
i = 0
j = 0
while ls < rs:
	if 9 - left[i] > right[j]:
		ls = ls - left[i] + 9
		i += 1
	else:
		rs = rs - right[j]
		j += 1
print(i + j)


import sys
from itertools import product
n = list(map(int, input()))
ans = 6
for a in product(range(10), repeat=6):
	if a[0] + a[1] + a[2] == a[3] + a[4] + a[5]:
		x = sum((0 if n[i] == a[i] else 1 for i in range(6)))
		ans = min(ans, x)
print(ans)


def count_min(a, b):
	count = 0
	while sum(a) < sum(b):
		if 9 - min(a) > max(b):
			a.remove(min(a))
			a.append(9)
		else:
			b.remove(max(b))
			b.append(0)
		count += 1
	return count
a = input()
(b, c) = ([], [])
for x in a[:3]:
	b.append(int(x))
for x in a[3:]:
	c.append(int(x))
if sum(b) > sum(c):
	a_min = c
	a_max = b
else:
	a_min = b
	a_max = c
print(count_min(a_min, a_max))


s = input()
ans = 10
for x in range(10):
	for y in range(10):
		for z in range(10):
			for a in range(10):
				for b in range(10):
					for c in range(10):
						if x + y + z == a + b + c:
							cnt = 0
							cnt += x != ord(s[0]) - ord('0')
							cnt += y != ord(s[1]) - ord('0')
							cnt += z != ord(s[2]) - ord('0')
							cnt += a != ord(s[3]) - ord('0')
							cnt += b != ord(s[4]) - ord('0')
							cnt += c != ord(s[5]) - ord('0')
							ans = min(ans, cnt)
print(str(ans))


inp = input()
minr = 999999
for i in range(10):
	for j in range(10):
		for k in range(10):
			for l in range(10):
				for m in range(10):
					for n in range(10):
						if i + j + k == l + m + n:
							r = 0
							if i != int(inp[0]):
								r += 1
							if j != int(inp[1]):
								r += 1
							if k != int(inp[2]):
								r += 1
							if l != int(inp[3]):
								r += 1
							if m != int(inp[4]):
								r += 1
							if n != int(inp[5]):
								r += 1
							minr = min(minr, r)
print(minr)


def main():
	n = input()
	pre = n[0:3]
	post = n[3:6]
	pre_sum = Sum(int(pre))
	post_sum = Sum(int(post))
	mi = []
	if pre_sum == post_sum:
		return 0
	if pre_sum > post_sum:
		diff = pre_sum - post_sum
		mi = list(map(int, post))
		ma = list(map(int, pre))
	else:
		diff = post_sum - pre_sum
		mi = list(map(int, pre))
		ma = list(map(int, post))
	count = 0
	while diff:
		inc = 9 - min(mi)
		dec = max(ma) - 0
		if diff > inc and inc >= dec:
			count += 1
			mi[mi.index(min(mi))] = 9
			diff -= inc
		if diff > dec and inc < dec:
			count += 1
			ma[ma.index(max(ma))] = 0
			diff -= dec
		if diff <= max(ma) - 0 or diff <= 9 - min(mi):
			diff = 0
	return count + 1

def Sum(n):
	s = 0
	while n:
		s += n % 10
		n = n // 10
	return s
print(main())


s = input()
mi = 6
for a in range(10):
	for b in range(10):
		for c in range(10):
			for d in range(10):
				for e in range(10):
					for f in range(10):
						if a + b + c == d + e + f:
							k = 0
							if a != int(s[0]):
								k += 1
							if b != int(s[1]):
								k += 1
							if c != int(s[2]):
								k += 1
							if d != int(s[3]):
								k += 1
							if e != int(s[4]):
								k += 1
							if f != int(s[5]):
								k += 1
							if k < mi:
								mi = k
print(mi)


s = input()
(a, b) = (sorted(list(map(int, s[:3]))), sorted(list(map(int, s[3:]))))
(sma, smb) = (sum(a), sum(b))
if sum(a) < sum(b):
	(mn, mx) = (a, b[::-1])
else:
	(mn, mx) = (b, a[::-1])
(k1, k2) = (0, 0)
while sum(mn) < sum(mx):
	if 9 - mn[k1] >= mx[k2]:
		mn[k1] = 9
		k1 += 1
	else:
		mx[k2] = 0
		k2 += 1
print(k1 + k2)


from operator import itemgetter
n = list(map(int, list(input())))
l = n[:3]
r = n[3:]
ls = sum(l)
rs = sum(r)
ans = []
ans1 = []
for i in range(3):
	ans.append([9 - l[i], 1])
	ans.append([r[i], 2])
	ans1.append([9 - r[i], 2])
	ans1.append([l[i], 1])
cnt = 0
ans.sort(key=itemgetter(0), reverse=True)
ans1.sort(key=itemgetter(0), reverse=True)
if ls < rs:
	i = 0
	while ls < rs:
		if ans[i][1] == 1:
			ls += ans[i][0]
			cnt += 1
		else:
			rs -= ans[i][0]
			cnt += 1
		i += 1
	print(cnt)
elif ls > rs:
	i = 0
	while ls > rs:
		if ans1[i][1] == 1:
			ls -= ans1[i][0]
			cnt += 1
		else:
			rs += ans1[i][0]
			cnt += 1
		i += 1
	print(cnt)
else:
	print(0)


number = input()
num1 = number[0:3]
num2 = number[3:6]
sum1 = 0
sum2 = 0
maxnum1 = int(num1[0])
minnum1 = int(num1[0])
maxnum2 = int(num2[0])
minnum2 = int(num2[0])
for i in num1:
	sum1 += int(i)
	maxnum1 = max(int(i), maxnum1)
	minnum1 = min(int(i), minnum1)
for i in num2:
	sum2 += int(i)
	maxnum2 = max(int(i), maxnum2)
	minnum2 = min(int(i), minnum2)
if sum1 >= sum2:
	diff = sum1 - sum2
if sum1 < sum2:
	diff = sum2 - sum1
if diff == 0:
	print(0)
elif sum1 > sum2 and (maxnum1 - diff >= 0 or minnum2 + diff < 10) or (sum1 < sum2 and (minnum1 + diff < 10 or maxnum2 - diff >= 0)):
	print(1)
elif sum1 > sum2 and (minnum1 > sum2 and sum1 - maxnum1 > sum2 - minnum2 + 9 and (maxnum2 + 18 < sum1)) or (sum1 < sum2 and (minnum2 > sum1 and sum2 - maxnum2 > sum1 - minnum1 + 9 and (maxnum1 + 18 < sum2))):
	print(3)
else:
	print(2)


def Main():
	dig = 0
	n = input()
	n1 = list(map(int, list(n[:3])))
	n2 = list(map(int, list(n[3:])))
	n1 = sorted(n1)
	n2 = sorted(n2)
	delta = sum(n1) - sum(n2)
	if delta != 0:
		if delta < 0:
			(n1, n2, delta) = (n2, n1, abs(delta))
		ind1 = 2
		ind2 = 0
		while ind1 > -1 and ind2 < 3 and (delta > 0):
			if n1[ind1] >= 9 - n2[ind2]:
				delta = delta - n1[ind1]
				ind1 = ind1 - 1
				dig = dig + 1
			else:
				delta = delta - (9 - n2[ind2])
				ind2 = ind2 + 1
				dig = dig + 1
	print(dig)
Main()


s = input()
a = list(map(int, s[:3]))
b = list(map(int, s[3:]))
al = sum(a)
bl = sum(b)
dif = al - bl
cnt = 0
while dif < 0:
	cnt += 1
	if 9 - min(a) > max(b):
		dif += min(-dif, 9 - min(a))
		a[a.index(min(a))] = 9
	else:
		dif += min(-dif, max(b))
		b[b.index(max(b))] = 0
c = b[:]
b = a[:]
a = c[:]
while dif > 0:
	cnt += 1
	if 9 - min(a) > max(b):
		dif -= min(dif, 9 - min(a))
		a[a.index(min(a))] = 9
	else:
		dif -= min(dif, max(b))
		b[b.index(max(b))] = 0
print(cnt)


import itertools

def sump(a: list, p: list):
	s = 0
	for i in range(0, 3):
		if p.count(i) == 0:
			s = s + a[i]
	for i in range(3, 6):
		if p.count(i) == 0:
			s = s - a[i]
	return s

def diffsum(p: list):
	mins = 0
	maxs = 0
	for i in p:
		if i <= 2:
			maxs = maxs + 9
		else:
			mins = mins - 9
	return [mins, maxs]

def solve():
	s = input().replace('\r\n', '')
	a = []
	for ai in s:
		a.append(int(ai))
	p = [0, 1, 2, 3, 4, 5]
	if sump(a, []) == 0:
		print('0')
		return
	for i in range(1, 7):
		for pi in itertools.combinations(p, i):
			s = -sump(a, pi)
			rs = diffsum(pi)
			if s >= rs[0] and s <= rs[1]:
				print(i)
				return
	print('error')
solve()


s = input()
mas = []
for i in range(6):
	mas += [int(s[i])]
(a, b) = (mas[0:3], mas[3:6])
if sum(a) < sum(b):
	(a, b) = (b, a)
delta = sum(a) - sum(b)
a = sorted(a, reverse=True)
b.sort()
(ans, sum, l1, l2) = (0, 0, 0, 0)
while sum < delta:
	if a[l1] > 9 - b[l2]:
		sum += a[l1]
		l1 += 1
	else:
		sum += 9 - b[l2]
		l2 += 1
	ans += 1
print(ans)


p = [int(x) for x in input()]
ans = []
for a in range(10):
	for b in range(10):
		for c in range(10):
			for d in range(10):
				for e in range(10):
					f = a + b + c - d - e
					o = [a, b, c, d, e, f]
					delta = 0
					if 0 <= f <= 9:
						for i in range(6):
							if p[i] != o[i]:
								delta += 1
						ans.append(delta)
print(min(ans))


l = list(str(input()))
l = [int(i) for i in l]
l1 = l[0:3]
l1 = sorted(l1)
l2 = l[3:]
l2 = sorted(l2)
if sum(l1) > sum(l2):
	(l1, l2) = (l2, l1)
if sum(l1) == sum(l2):
	ans = 0
else:
	l1[0] = 9
	if sum(l1) >= sum(l2):
		ans = 1
	else:
		l1[1] = 9
		if sum(l1) >= sum(l2):
			ans = 2
		else:
			ans = 3
l1 = l[0:3]
l1 = sorted(l1)
l2 = l[3:]
l2 = sorted(l2)
if sum(l1) > sum(l2):
	(l1, l2) = (l2, l1)
if sum(l1) == sum(l2):
	ans = min(ans, 0)
else:
	l2[2] = 0
	if sum(l1) >= sum(l2):
		ans = min(ans, 1)
	else:
		l2[1] = 0
		if sum(l1) >= sum(l2):
			ans = min(ans, 2)
		else:
			ans = min(ans, 3)
l1 = l[0:3]
l1 = sorted(l1)
l2 = l[3:]
l2 = sorted(l2)
if sum(l1) > sum(l2):
	(l1, l2) = (l2, l1)
if sum(l1) == sum(l2):
	ans = min(ans, 0)
else:
	l2[2] = 0
	if sum(l1) >= sum(l2):
		ans = min(ans, 1)
	else:
		l1[0] = 9
		if sum(l1) >= sum(l2):
			ans = min(ans, 2)
		else:
			ans = min(ans, 3)
print(ans)


def horror_func(s):
	if s[0] + s[1] + s[2] == s[3] + s[4] + s[5]:
		return 0
	else:
		for i in range(6):
			for n in range(10):
				c = s.copy()
				c[i] = n
				if c[0] + c[1] + c[2] == c[3] + c[4] + c[5]:
					return 1
		for i in range(6):
			for j in range(6):
				for n1 in range(10):
					for n2 in range(10):
						c = s.copy()
						c[i] = n1
						c[j] = n2
						if c[0] + c[1] + c[2] == c[3] + c[4] + c[5]:
							return 2
		for i in range(6):
			for j in range(6):
				for z in range(6):
					for n1 in range(10):
						for n2 in range(10):
							for n3 in range(10):
								c = s.copy()
								c[i] = n1
								c[j] = n2
								c[z] = n3
								if c[0] + c[1] + c[2] == c[3] + c[4] + c[5]:
									return 3
s = list(map(int, input()))
print(horror_func(s))


arr = list(map(int, input()))
if sum(arr[:3]) > sum(arr[3:6]):
	arr = arr[3:6] + arr[:3]
diff = sum(arr[3:6]) - sum(arr[:3])
cnt = 0
tmparr = []
for i in range(3):
	tmparr.append(9 - arr[i])
for i in range(3, 6):
	tmparr.append(arr[i])
tmparr.sort()
for item in tmparr[::-1]:
	if diff <= 0:
		print(cnt)
		diff = 1
		break
	diff -= item
	cnt += 1
if diff <= 0:
	print(cnt)


import sys

def returndiff(s1, s2, s3, s4, s5, s6):
	ct = 0
	if s1 + s2 + s3 == s4 + s5 + s6:
		if s1 != n[0]:
			ct += 1
		if s2 != n[1]:
			ct += 1
		if s3 != n[2]:
			ct += 1
		if s4 != n[3]:
			ct += 1
		if s5 != n[4]:
			ct += 1
		if s6 != n[5]:
			ct += 1
	return ct
n = list(map(int, input()))
t = []
(n1, n2, n3, n4, n5, n6) = (n[0], n[1], n[2], n[3], n[4], n[5])
if sum(n[:3]) == sum(n[3:]):
	print(0)
	sys.exit(0)
for a in range(10):
	n1 = a
	if n1 + n2 + n3 == n4 + n5 + n6:
		t.append(returndiff(n1, n2, n3, n4, n5, n6))
	for b in range(10):
		n2 = b
		if n1 + n2 + n3 == n4 + n5 + n6:
			t.append(returndiff(n1, n2, n3, n4, n5, n6))
		for c in range(10):
			n3 = c
			if n1 + n2 + n3 == n4 + n5 + n6:
				t.append(returndiff(n1, n2, n3, n4, n5, n6))
			for d in range(10):
				n4 = d
				if n1 + n2 + n3 == n4 + n5 + n6:
					t.append(returndiff(n1, n2, n3, n4, n5, n6))
				for e in range(10):
					n5 = e
					if n1 + n2 + n3 == n4 + n5 + n6:
						t.append(returndiff(n1, n2, n3, n4, n5, n6))
					for f in range(10):
						n6 = f
						if n1 + n2 + n3 == n4 + n5 + n6:
							t.append(returndiff(n1, n2, n3, n4, n5, n6))
print(min(t))


n = input()
a = []
for i in n:
	a += [i]
a = list(map(int, a))
x = a[0] + a[1] + a[2] - a[3] - a[4] - a[5]
if x == 0:
	print(0)
elif x > 0:
	a[3] = 9 - a[3]
	a[4] = 9 - a[4]
	a[5] = 9 - a[5]
	a.sort()
	i = 0
	while x > 0:
		x -= a[5 - i]
		i += 1
	print(i)
else:
	a[0] = 9 - a[0]
	a[1] = 9 - a[1]
	a[2] = 9 - a[2]
	a.sort()
	x = -x
	i = 0
	while x > 0:
		x -= a[5 - i]
		i += 1
	print(i)


def change_num(left_arr, right_arr, count):
	(min_n, max_n) = (10, -1)
	if sum(left_arr) > sum(right_arr):
		max_arr = left_arr
		min_arr = right_arr
	else:
		max_arr = right_arr
		min_arr = left_arr
	diff = sum(max_arr) - sum(min_arr)
	for i in range(3):
		if min_n > min_arr[i]:
			min_n = min_arr[i]
			min_i = i
		if max_n < max_arr[i]:
			max_n = max_arr[i]
			max_i = i
	if diff <= 9 - min_n:
		count += 1
		return count
	elif diff <= max_n:
		count += 1
		return count
	elif max_n >= 9 - min_n:
		max_arr[max_i] = 0
	else:
		min_arr[min_i] = 9
	count += 1
	return change_num(min_arr, max_arr, count)
msg = input()
left = msg[:3]
right = msg[3:]
left_arr = []
right_arr = []
for char in left:
	left_arr.append(int(char))
for char in right:
	right_arr.append(int(char))
if sum(left_arr) == sum(right_arr):
	print(0)
else:
	print(change_num(left_arr, right_arr, 0))


from functools import reduce
e = input()
mi = 6
for i in range(1000000):
	a = str(i).zfill(6)
	s = ord(a[0]) + ord(a[1]) + ord(a[2])
	t = ord(a[3]) + ord(a[4]) + ord(a[5])
	if s == t:
		l = sum(list([0 if x[0] == x[1] else 1 for x in zip(a, e)]))
		mi = min(mi, l)
print(mi)


wow = [int(e) for e in input()]
a1 = [wow[0], wow[1], wow[2]]
a2 = [wow[3], wow[4], wow[5]]
sum1 = sum(a1)
sum2 = sum(a2)
if sum1 == sum2:
	print(0)
else:
	if sum1 < sum2:
		noi = sum1
		mak = sum2
		fnoi = list(a1)
		fmak = list(a2)
		pontang = sum2 - sum1
	if sum2 < sum1:
		noi = sum2
		mak = sum1
		fnoi = list(a2)
		fmak = list(a1)
		pontang = sum1 - sum2
	fnoi.sort()
	fmak.sort()
	ptfnoi = [0] * 3
	ptfmak = [0] * 3
	ptfnoi[0] = 9 - fnoi[0]
	ptfnoi[1] = 9 - fnoi[1]
	ptfnoi[2] = 9 - fnoi[2]
	ptfmak[0] = fmak[0]
	ptfmak[1] = fmak[1]
	ptfmak[2] = fmak[2]
	lis = [ptfnoi[0], ptfnoi[1], ptfnoi[2], ptfmak[0], ptfmak[1], ptfmak[2]]
	lis.sort()
	mx1 = lis[5]
	mx2 = lis[4]
	if mx1 >= pontang:
		print(1)
	elif mx1 + mx2 >= pontang:
		print(2)
	else:
		print(3)


digit_set = set(range(10))
double_digit_set = set(range(19))
A = [int(i) for i in input()]
first_sum = sum(A[:3])
second_sum = sum(A[3:])
one_flag = True
exit_flag = False
if first_sum == second_sum:
	print(0)
else:
	for i in range(6):
		if i < 3:
			if second_sum - (first_sum - A[i]) in digit_set:
				print(1)
				one_flag = False
				break
		elif first_sum - (second_sum - A[i]) in digit_set:
			print(1)
			one_flag = False
			break
	if one_flag:
		for i in range(6):
			for j in range(i + 1, 6):
				if i < 3 and j < 3:
					if second_sum - (first_sum - A[i] - A[j]) in double_digit_set:
						print(2)
						exit_flag = True
						break
				if i >= 3 and j >= 3:
					if first_sum - (second_sum - A[i] - A[j]) in double_digit_set:
						print(2)
						exit_flag = True
						break
				elif abs(first_sum - A[i] - second_sum + A[j]) <= 9:
					print(2)
					exit_flag = True
					break
			if exit_flag:
				break
		else:
			print(3)


a = [int(i) for i in input()]
b = a[:3]
c = a[3:]
if sum(b) == sum(c):
	print(0)
else:
	if sum(b) > sum(c):
		(b, c) = (c[:], b[:])
	diff = sum(c) - sum(b)
	mx = []
	for i in b:
		mx.append(9 - i)
	for i in c:
		mx.append(i)
	ans = 0
	cur = 0
	mx.sort(reverse=True)
	i = 0
	while cur < diff:
		ans += 1
		cur += mx[i]
		i += 1
	print(ans)


s = input()
left = [int(s[0]), int(s[1]), int(s[2])]
right = [int(s[3]), int(s[4]), int(s[5])]
s1 = sum(left)
s2 = sum(right)
if s1 > s2:
	t = left
	left = right
	right = t
	s1 = sum(left)
	s2 = sum(right)
if s1 == s2:
	print(0)
else:
	dif = s2 - s1
	if left[0] + dif <= 9 or left[1] + dif <= 9 or left[2] + dif <= 9 or (right[0] - dif >= 0) or (right[1] - dif >= 0) or (right[2] - dif >= 0):
		print(1)
	elif left[0] + left[1] + dif <= 18 or left[1] + left[2] + dif <= 18 or left[2] + left[0] + dif <= 18 or (right[0] + right[1] - dif >= 0) or (right[1] + right[2] - dif >= 0) or (right[2] + right[0] - dif >= 0):
		print(2)
	else:
		ans = 3
		for i in range(3):
			for j in range(3):
				if dif - right[j] <= 9 - left[i]:
					ans = 2
					break
		print(ans)


import math
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
n = input()
a = [int(i) for i in n]
x = a[0] + a[1] + a[2]
y = a[3] + a[4] + a[5]
if x == y:
	print(0)
elif x > y:
	a[3] = 9 - a[3]
	a[4] = 9 - a[4]
	a[5] = 9 - a[5]
	a.sort(reverse=True)
	i = 0
	t = x - y
	while t > 0:
		t -= a[i]
		i += 1
	print(i)
else:
	a[0] = 9 - a[0]
	a[1] = 9 - a[1]
	a[2] = 9 - a[2]
	a.sort(reverse=True)
	t = y - x
	i = 0
	while t > 0:
		t -= a[i]
		i += 1
	print(i)


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
s = input()
little = []
big = []
for i in range(3):
	little.append(int(s[i]))
for i in range(3):
	big.append(int(s[i + 3]))
suml = sum(little)
sumb = sum(big)
if suml > sumb:
	(little, big) = (big, little)
	(suml, sumb) = (sumb, suml)
li = []
bi = []
for i in range(3):
	li.append((9 - little[i], 'l'))
	bi.append((big[i], 'b'))
a = li + bi
a.sort(reverse=True)
for i in range(6):
	if suml >= sumb:
		print(i)
		exit()
	if a[i][1] == 'l':
		suml += a[i][0]
	else:
		sumb -= a[i][0]
print(6)


s = [int(x) for x in input()]
(a, b) = (s[:3], s[3:])
(sa, sb) = (sum(a), sum(b))
if sa > sb:
	(a, b) = (b, a)
r = abs(sb - sa)
if r == 0:
	ans = 0
else:
	breva = b[:]
	for x in a:
		breva.append(9 - x)
	td = 0
	breva.sort(reverse=True)
	for (i, j) in enumerate(breva):
		td += j
		if td >= r:
			ans = i + 1
			break
print(ans)


def solution():
	s = input()
	a = [int(i) for i in s[:3]]
	b = [int(i) for i in s[3:]]
	if sum(b) == sum(a):
		print(0)
		return
	if sum(a) > sum(b):
		(a, b) = (b, a)
	dt = sum(b) - sum(a)
	diffs = []
	for i in a:
		diffs.append(9 - i)
	diffs.extend(b)
	diffs.sort(reverse=True)
	i = 0
	while dt > 0:
		dt -= diffs[i]
		i += 1
	print(i)
solution()


s = str(input())
r = []
for el in s:
	r.append(int(el))
l1 = r[0:3]
l2 = r[3:6]
if sum(l1) < sum(l2):
	(l1, l2) = (l2, l1)
s = abs(sum(l1) - sum(l2))
if s == 0:
	print(0)
else:
	usable = []
	for e in l1:
		usable.append(e)
	for e in l2:
		usable.append(9 - e)
	usable.sort(reverse=True)
	i = 0
	while s > 0:
		s -= usable[i]
		i += 1
	print(i)


s = input()
p = s[:3]
h = s[3:]
l = [int(i) for i in p]
k = [int(i) for i in h]
q = []
if sum(k) < sum(l):
	q = [i for i in l]
	q += [9 - i for i in k]
	raz = sum(l) - sum(k)
	cnt = 0
	while raz > 0:
		q = sorted(q)
		raz -= q[len(q) - 1]
		q[len(q) - 1] = 0
		cnt += 1
	print(cnt)
elif sum(k) > sum(l):
	q = [i for i in k]
	q += [9 - i for i in l]
	raz = sum(k) - sum(l)
	cnt = 0
	while raz > 0:
		q = sorted(q)
		raz -= q[len(q) - 1]
		q[len(q) - 1] = 0
		cnt += 1
	print(cnt)
else:
	print(0)


a = input()

def ass(a):
	ans = 0
	p = list(map(int, a[:3]))
	q = list(map(int, a[3:]))
	s = sum(p) - sum(q)
	if s == 0:
		ans = 0
		return ans
	elif s > 0:
		p = list(map(lambda x: 9 - x, p))
	else:
		s = -s
		q = list(map(lambda x: 9 - x, q))
	a = p + q
	a.sort()
	ans = 0
	for i in a:
		if s <= 0:
			break
		s -= 9 - i
		ans += 1
	return ans

def bss(a):
	l = list(a)
	a = list(map(int, l))
	l1 = a[0:3]
	l2 = a[3:6]
	if sum(l1) > sum(l2):
		b = l2
		l2 = l1
		l1 = b
	l1.sort()
	l2.sort()
	d = sum(l2) - sum(l1)
	if d == 0:
		return 0
	else:
		i = 0
		j = 2
		c = 0
		while d > 0:
			if l1 == []:
				d -= l2[j]
				l2.remove(l2[j])
				j -= 1
				c += 1
			elif l2 == []:
				d -= 9 - l1[0]
				l1.remove(l[0])
				c += 1
			elif 9 - l1[0] >= l2[j]:
				d -= 9 - l1[i]
				l1.remove(l1[i])
				c += 1
			else:
				d -= l2[j]
				l2.remove(l2[j])
				j -= 1
				c += 1
	return c
print(ass(a))


def solve():
	s = input()
	x = [int(z) for z in s]
	(y, z) = (x[:3], x[3:])
	if sum(y) < sum(z):
		(y, z) = (z, y)
	a = sum(y) - sum(z)
	b = [y[-1], y[-2], y[-3], 9 - z[0], 9 - z[1], 9 - z[2]]
	b.sort()
	c = 0
	cnt = 0
	while c < a:
		c += b.pop()
		cnt += 1
	return cnt
print(solve())