You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.

In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.

What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$.


-----Input-----

The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero.


-----Output-----

If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number.

Note that you can swap only adjacent digits in the given number.


-----Examples-----
Input
5071

Output
4

Input
705

Output
1

Input
1241367

Output
-1



-----Note-----

In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.

Source: codeforces | Difficulty: HARD
Tags: Complete search

Greedy algorithms


Solutions:
s = input()[::-1]
t = len(s)

def g(c, i=0):
	return s.find(str(c), i) + 1 or 50
(i, f) = (g(0), g(5))
j = min(g(0, i), f)
m = i + j + (j < i)
j = min(g(2), g(7))
l = f + j + (j < f)
if f == t:
	i = t - 1
	while s[i - 1] == '0':
		l += 1
		i -= 1
m = min(m, l)
print((-1, m - 3)[m < 40])


n = input()
a = n[:]
p = len(n) - 1
(count1, count2) = (0, 0)
i = p
(flag1, flag2) = (0, 0)
while i > -1:
	if n[i] == '5' and flag1 == 0:
		count1 = p - i
		n = n[:i] + n[i + 1:] + '5'
		flag1 = 1
	if a[i] == '0' and flag2 == 0:
		count2 = p - i
		a = a[:i] + a[i + 1:] + '0'
		flag2 = 1
	i = i - 1
if flag1 == 1 or flag2 == 1:
	i = p - 1
	if flag1 == 1:
		flag1 = -1
	if flag2 == 1:
		flag2 = -1
	while i > -1:
		if (int(n[i]) - 2) % 5 == 0 and flag1 == -1:
			count1 = count1 + p - i - 1
			flag1 = 2
		if int(a[i]) % 5 == 0 and flag2 == -1:
			count2 = count2 + p - i - 1
			flag2 = 2
		i = i - 1
if flag1 == 2 and flag2 == 2:
	if n[0] == '0':
		i = 0
		while n[i] == '0' and i < p - 1:
			count1 = count1 + 1
			i = i + 1
		if i == p - 1:
			count1 = count2
	print(min(count1, count2))
elif flag1 == 2 and flag2 != 2:
	print(count1)
elif flag1 != 2 and flag2 == 2:
	print(count2)
else:
	print(-1)


num = str(input())
n = len(num)
num_r = num[::-1]
(s0, s5) = (num_r.find('0'), num_r.find('5'))
if s0 != -1:
	newnum_r = num_r[:s0] + num_r[s0 + 1:]
	c = []
	for a in ['0', '5']:
		if newnum_r.find(a) != -1:
			c.append(newnum_r.find(a))
	if c != []:
		s = min(c)
		s0 = s0 + s
	else:
		s0 = 999999999
else:
	s0 = 999999999
if s5 != n - 1 or num.find('0') != 1:
	if s5 != -1:
		newnum_r = num_r[:s5] + num_r[s5 + 1:]
		c = []
		for a in ['2', '7']:
			if newnum_r.find(a) != -1:
				c.append(newnum_r.find(a))
		if c != []:
			s = min(c)
			s5 = s5 + s
		else:
			s5 = 999999999
	else:
		s5 = 999999999
	ssr = min(s0, s5)
	if ssr != 999999999:
		print(ssr)
	else:
		print(-1)
else:
	if s5 != -1:
		newnum_r = num_r[:s5] + num_r[s5 + 1:]
		c = []
		for a in ['2', '7']:
			if newnum_r.find(a) != -1:
				c.append(newnum_r.find(a))
		if c != []:
			s = min(c)
			sk = 0
			for i in range(1, n - 1):
				if num[i] == '0':
					sk = sk + 1
				else:
					break
			s5 = s5 + s + sk
		else:
			s5 = 999999999
	else:
		s5 = 999999999
	ssr = min(s0, s5)
	if ssr != 999999999:
		print(ssr)
	else:
		print(-1)


a = input()
k = 0
if a.rfind('5') == 0 and a[1] == '0' and (len(a) > 2):
	a = a[0] + a[2] + a[1] + a[3:]
	k = a.count('0')
if a.count('0') >= 2:
	a00 = -1 + len(a) - 1 - a.rfind('0') + (len(a) - 1 - a.rfind('0', 0, a.rfind('0')))
else:
	a00 = 1000
if a.rfind('2') > a.rfind('5') and a.rfind('2') != -1 and (a.rfind('5') != -1):
	a25 = len(a) - 1 - a.rfind('2') + (len(a) - 1 - a.rfind('5', 0, a.rfind('2')))
elif a.rfind('2') != -1 and a.rfind('5') != -1:
	a25 = len(a) - 2 - a.rfind('5') + (len(a) - 1 - a.rfind('2', 0, a.rfind('5')))
else:
	a25 = 1000
if a.rfind('5') > a.rfind('0') and a.rfind('0') != -1 and (a.rfind('5') != -1):
	a50 = len(a) - 1 - a.rfind('5') + (len(a) - 1 - a.rfind('0', 0, a.rfind('5')))
elif a.rfind('0') != -1 and a.rfind('5') != -1:
	a50 = len(a) - 2 - a.rfind('0') + (len(a) - 1 - a.rfind('5', 0, a.rfind('0')))
else:
	a50 = 1000
if a.rfind('7') > a.rfind('5') and a.rfind('7') != -1 and (a.rfind('5') != -1):
	a75 = len(a) - 1 - a.rfind('7') + (len(a) - 1 - a.rfind('5', 0, a.rfind('7')))
elif a.rfind('7') != -1 and a.rfind('5') != -1:
	a75 = len(a) - 2 - a.rfind('5') + (len(a) - 1 - a.rfind('7', 0, a.rfind('5')))
else:
	a75 = 1000
if min(a00, a25, a50, a75) == 1000:
	print(-1)
else:
	print(min(a00, a25, a50, a75) + k)


k = input()
zeroes = [-1, -1]
five = -1
two = -1
seven = -1
for i in range(len(k)):
	if k[i] == '0':
		zeroes[zeroes.index(min(zeroes))] = i
	elif k[i] == '2':
		two = i
	elif k[i] == '5':
		five = i
	elif k[i] == '7':
		seven = i
if len(k) == 3 and k[:2] == '50' and (k[-1] != '5') and (k[-1] != '0'):
	print(2)
elif k == '5020' or k == '5070':
	print(1)
elif k[-2:] == '25' or k[-2:] == '00' or k[-2:] == '50' or (k[-2:] == '75'):
	print(0)
elif k[-2:] == '52' or k[-2:] == '05' or k[-2:] == '57':
	print(1)
elif k == '505':
	print(1)
elif k == '500':
	print(0)
elif len(k) != 4 and (k[:3] == '502' and two == 2 or (k[:3] == '507' and seven == 2)):
	print(len(k))
elif len(k) == 4 and (k[:3] == '502' or k[:3] == '507'):
	print(4)
elif k[:2] == '50' and five == 0 and (min(zeroes) == -1) and (seven != -1 or two != -1):
	print(2 * len(k) - max(two, seven) - 1)
elif k[:2] == '50' and five == 0 and (min(zeroes) != -1) and (seven != -1 or two != -1):
	cnt0 = 0
	for u in range(1, len(k)):
		if k[u] == '0':
			cnt0 += 1
	print(min(len(k) * 2 - zeroes[0] - zeroes[1] - 3, cnt0 - 1 + len(k)))
else:
	ch = []
	fl = 0
	if min(zeroes) > -1:
		fl = 1
		ch.append(len(k) * 2 - zeroes[0] - zeroes[1] - 3)
	if five != -1 and max(zeroes) > -1:
		fl = 1
		if five < max(zeroes):
			ch.append(2 * len(k) - max(zeroes) - five - 3)
		else:
			ch.append(2 * len(k) - max(zeroes) - five - 2)
	if five != -1:
		if two != -1:
			fl = 1
			if two < five:
				ch.append(2 * len(k) - two - five - 3)
			else:
				ch.append(2 * len(k) - two - five - 2)
		if seven != -1:
			fl = 1
			if seven < five:
				ch.append(2 * len(k) - seven - five - 3)
			else:
				ch.append(2 * len(k) - seven - five - 2)
	if fl:
		print(min(ch))
	else:
		print(-1)


k = input()
import sys
Min = sys.maxsize
if '5' in k and '0' in k:
	temp = len(k) - 2 - k.rfind('5') + (len(k) - 1) - k.rfind('0')
	if k.rfind('5') > k.rfind('0'):
		temp += 1
	if temp < Min:
		Min = temp
if '2' in k and '5' in k:
	temp = len(k) - 2 - k.rfind('2') + (len(k) - 1) - k.rfind('5')
	if k.rfind('5') == 0 and k[1] == '0' and (len(k) > 3):
		add = 0
		i = 1
		while k[i] == '0':
			add += 1
			i += 1
		temp += add
	if k.rfind('2') > k.rfind('5'):
		temp += 1
	if temp < Min:
		Min = temp
if '7' in k and '5' in k:
	temp = len(k) - 2 - k.rfind('7') + (len(k) - 1) - k.rfind('5')
	if k.rfind('5') == 0 and k[1] == '0' and (len(k) > 3):
		add = 0
		i = 1
		while k[i] == '0':
			add += 1
			i += 1
		temp += add
	if k.rfind('7') > k.rfind('5'):
		temp += 1
	if temp < Min:
		Min = temp
if k.count('0') > 1:
	css = k.rfind('0')
	temp = len(k) - 1 - css
	k = k[:css] + k[css + 1:]
	temp += len(k) - 1 - k.rfind('0')
	if temp < Min:
		Min = temp
if Min == sys.maxsize:
	print(-1)
else:
	print(Min)


INF = float('inf')

def count_min_steps(num_str):
	result = INF
	for (i, c1) in enumerate(num_str):
		suffix = num_str[:i] + num_str[i + 1:]
		for (j, c2) in enumerate(suffix):
			swap_str = suffix[:j] + suffix[j + 1:] + c2 + c1
			if swap_str[0] != '0' and int(swap_str) % 25 == 0:
				result = min(result, len(num_str) + len(suffix) - i - j - 2)
	return result

def solve(num_str):
	result = INF
	for (i, c) in enumerate(num_str):
		swap_str = num_str[i] + num_str[:i] + num_str[i + 1:]
		result = min(result, count_min_steps(swap_str) + i)
	return result
number_str = input()
result = solve(number_str)
print(-1 if result == INF else result)


n = list(input())[::-1]
INF = 10 ** 9

def solve(b, a):
	(a, b) = (str(a), str(b))
	if a not in n or b not in n:
		return INF
	nn = n[:]
	ia = nn.index(a)
	nn = [a] + nn[:ia] + nn[ia + 1:]
	ib = nn.index(b)
	nn = [a, b] + nn[1:ib] + nn[ib + 1:]
	if len(n) == 2:
		return ia + ib - 1
	cnt = 0
	for i in range(len(n) - 1, 1, -1):
		if nn[i] != '0':
			break
		cnt += 1
	else:
		return INF
	return ia + ib + cnt - 1
if n.count('0') >= 2:
	ans = -1
	cnt = 0
	for i in range(len(n)):
		if n[i] == '0':
			ans += i
			cnt += 1
			if cnt == 2:
				break
else:
	ans = INF
ans = min(ans, solve(2, 5), solve(5, 0), solve(7, 5))
if ans == INF:
	print(-1)
else:
	print(ans)


s = input()[::-1]
m = I = 41
f = s.find('5') + 1
i = s.find('0') + 1
t = len(s)
if i:
	j = min(s.find('0', i) + 1 or I, f or I)
	if j < I:
		m = i + j - 3
		if j < i:
			m += 1
if f:
	j = min(s.find('2') + 1 or I, s.find('7') + 1 or I)
	if j < I:
		l = f + j - 3
		if j < f:
			l += 1
		if f == t:
			i = t - 1
			while s[i - 1] == '0':
				l += 1
				i -= 1
		m = min(m, l)
print((-1, m)[m < I])


import sys
num = input()
length = len(num)
minimum = sys.maxsize
possibles = ['25', '50', '75', '00']
for possible in possibles:
	if possible[0] == possible[1] and num.count('0') > 1:
		first = num.rfind(possible[0])
		second = num[:first].rfind(possible[1])
		temp_min = length - 2 - second + length - 1 - first
		minimum = min(minimum, temp_min)
	elif possible[0] != possible[1] and possible[0] in num and (possible[1] in num):
		first = num.rfind(possible[0])
		second = num.rfind(possible[1])
		temp_min = length - 2 - first + length - 1 - second
		if second == 0 and length > 3:
			i = 1
			while num[i] == '0':
				temp_min += 1
				i += 1
		if first > second:
			temp_min += 1
		minimum = min(minimum, temp_min)
if minimum == sys.maxsize:
	print(-1)
else:
	print(minimum)


y = input()
fives = y.count('5')
zeroes = y.count('0')
twos = y.count('2')
sevens = y.count('7')
if fives == 0:
	if zeroes < 2:
		print(-1)
		exit(0)
elif zeroes == 0 and twos == 0 and (sevens == 0):
	print(-1)
	exit(0)
best = 5 * len(y)
y = y[::-1]
for xx in [['5', '2'], ['0', '0'], ['5', '7'], ['0', '5']]:
	z = y
	count = 0
	for x in xx:
		if not x in z:
			count = 5 * len(y)
			break
		f = z.index(x)
		z = z[:f] + z[f + 1:]
		count += f
	if len(z) > 2 and z[-1] == '0':
		valid = False
		for x in z[::-1]:
			if x == '0':
				count += 1
				continue
			else:
				valid = True
				break
		if not valid:
			count += 5 * len(y)
	if count < best:
		best = count
print(best)


import sys
input = sys.stdin.readline
n = list(input().rstrip())
m = len(n)
inf = 114514
if m == 1:
	ans = -1
elif m == 2:
	x = 10 * int(n[0]) + int(n[1])
	if x % 25 == 0:
		ans = 0
	elif x == 52 or x == 57:
		ans = 1
	else:
		ans = -1
else:
	ans = inf
	for i in range(m):
		if int(n[i]) % 5:
			continue
		n0 = list(n)
		cnt = 0
		f = 1
		if i == 0:
			f = 0
			for j in range(1, m - 1):
				if not int(n0[j]) == 0:
					f = 1
					cnt += j - 1
					for k in range(j - 1, 0, -1):
						(n0[k], n0[k + 1]) = (n0[k + 1], n0[k])
					break
		if not f:
			continue
		for j in range(m - i - 1):
			(n0[i + j], n0[i + j + 1]) = (n0[i + j + 1], n0[i + j])
			cnt += 1
		if int(n0[0]) == 0:
			continue
		if not 10 * int(n0[-2]) + int(n0[-1]) % 25:
			ans = min(ans, cnt)
			continue
		c = min(2, int(n0[-1]))
		f = 0
		for j in range(m - 2, 0, -1):
			if int(n0[j]) % 5 == c:
				f = 1
				cnt += m - 2 - j
				ans = min(ans, cnt)
				break
		if not f and int(n0[0]) % 5 == c:
			cnt += m - 2
			f = 0
			for j in range(1, m - 1):
				if int(n0[j]):
					f = 1
					cnt += j - 1
					break
			if f:
				ans = min(ans, cnt)
if not ans ^ inf:
	ans = -1
print(ans)


class Problem:

	def solve(self, s):
		ans = -1
		n = len(s)
		for i in range(n):
			for j in range(n):
				if i != j:
					c1 = c2 = 0
					(t, c1) = self.move_end(s, i, j)
					if t[0] == '0':
						k = self.find_nonzero(t)
						(t, c2) = self.move_begin(t, k)
					if self.ok(t):
						ans = self.best(ans, c1 + c2)
		return ans

	def move_end(self, s, i, j):
		if j < i:
			i -= 1
		t = s
		(t, c1) = self.move_char(t, j, len(t) - 1)
		(t, c2) = self.move_char(t, i, len(t) - 2)
		return (t, c1 + c2)

	def move_begin(self, s, i):
		return self.move_char(s, i, 0)

	def move_char(self, s, i: 'index from', j: 'index to'):
		l = list(s)
		count = 0
		while i != j:
			count += 1
			if i < j:
				(l[i], l[i + 1]) = (l[i + 1], l[i])
				i += 1
			else:
				(l[i - 1], l[i]) = (l[i], l[i - 1])
				i -= 1
		return (''.join(l), count)

	def ok(self, s):
		return s[0] != '0' and int(s) % 25 == 0

	def find_nonzero(self, s):
		found = -1
		for i in range(len(s)):
			if s[i] != '0':
				found = i
				break
		return found

	def best(self, a, b):
		if a == -1:
			return b
		if b == -1:
			return a
		return a if a < b else b
s = input()
ans = Problem().solve(s)
print(ans)
exit()
assert 0 == Problem().solve('100')
assert 1 == Problem().solve('52')
assert 4 == Problem().solve('5071')
assert 1 == Problem().solve('705')
assert -1 == Problem().solve('1241367')
assert 2 == Problem().solve('507')
assert 1 == Problem().solve('17010')
assert 6 == Problem().solve('52231')
assert 5 == Problem().solve('50267')
print('ok')


num = str(input())
endings = [[0, 0], [5, 2], [0, 5], [5, 7]]
lenght = len(num)
candidate = []
if lenght == 2:
	if [int(num[1]), int(num[0])] in endings:
		print(0)
	elif [int(num[0]), int(num[1])] in endings:
		print(1)
	else:
		print(-1)
	exit()
for tup in endings:
	poss = []
	ind = []
	leftCheck = [i for i in tup]
	count = 0
	for i in tup:
		for j in range(lenght - 1, -1, -1):
			index = lenght - j - 1
			if str(i) == num[j] and j not in ind and (int(num[j]) in leftCheck):
				if len(ind):
					if str(tup[1]) == num[j] and j < ind[0]:
						index -= 1
				ind.append(j)
				poss.append(index)
				del leftCheck[leftCheck.index(i)]
				count += 1
			if count == 2:
				s = sum(poss)
				if 0 in ind:
					i = 1
					while num[i] == '0' and i not in ind:
						s += 1
						i += 1
				candidate.append(s)
				break
if len(candidate):
	print(min(candidate))
else:
	print(-1)


s = list(input())
n = len(s)
if n == 1:
	print(-1)
	exit(0)
ans = 2000000000.0
for i in range(n):
	for j in range(i + 1, n):
		s1 = s[:]
		cnt = 0
		(i1, j1) = (i + 1, j + 1)
		while j1 < n:
			(s1[j1 - 1], s1[j1]) = (s1[j1], s1[j1 - 1])
			j1 += 1
			cnt += 1
		while i1 < n - 1:
			(s1[i1 - 1], s1[i1]) = (s1[i1], s1[i1 - 1])
			i1 += 1
			cnt += 1
		k = 0
		while k < n and s1[k] == '0':
			k += 1
		if int(''.join(s1)) % 25 == 0:
			ans = min(ans, cnt + k)
		(s1[n - 1], s1[n - 2]) = (s1[n - 2], s1[n - 1])
		if int(''.join(s1)) % 25 == 0:
			ans = min(ans, cnt + k + 1)
if ans == 2000000000.0:
	print(-1)
else:
	print(ans)


k = input()
n = len(k)
import sys
Min = sys.maxsize
ck = ['50', '25', '75']
for c in ck:
	if c[0] in k and c[1] in k:
		temp = 2 * n - 3 - k.rfind(c[0]) - k.rfind(c[1])
		if c[1] == '5':
			if k.rfind('5') == 0 and k[1] == '0' and (n > 3):
				add = 0
				i = 1
				while k[i] == '0':
					add += 1
					i += 1
				temp += add
		temp += k.rfind(c[0]) > k.rfind(c[1])
		Min = min(temp, Min)
if k.count('0') > 1:
	css = k.rfind('0')
	temp = n - 1 - css
	k = k[:css] + k[css + 1:]
	temp += len(k) - 1 - k.rfind('0')
	Min = min(temp, Min)
print([Min, -1][Min == sys.maxsize])


import sys
n = input()
num = ['25', '50', '75', '00']
minn = sys.maxsize
step = 0
length = len(n)
for x in num:
	if x[0] == x[1] and n.count('0') > 1:
		first = n.rfind(x[0])
		second = n[:first].rfind(x[1])
		step = length - 1 - second + length - 2 - first
		minn = min(step, minn)
	elif x[0] != x[1] and x[0] in n and (x[1] in n):
		i = 1
		first = n.rfind(x[0])
		second = n.rfind(x[1])
		step = length - 1 - second + length - 2 - first
		if second == 0:
			while n[i] == '0':
				step += 1
				i += 1
		if second < first:
			step += 1
		minn = min(minn, step)
if minn == sys.maxsize:
	print('-1')
else:
	print(minn)


s = input()
n = len(s)

def i(c, o=0):
	try:
		return s[::-1].index(str(c), o)
	except:
		return -1
oo = 0
while oo < n - 1 and s[oo + 1] == '0':
	oo += 1
o5 = i(5)
o7 = i(7)
o2 = i(2)
o01 = i(0)
o02 = i(0, o01 + 1) if o01 >= 0 else -1
r = 100
if o01 >= 0 and o02 >= 0:
	r = min(r, o01 + o02 - 1)
if o01 >= 0 and o5 >= 0:
	r = min(r, o01 + o5 - int(o01 < o5))
if o5 >= 0 and o2 >= 0:
	r = min(r, o5 + o2 - int(o5 < o2) + int(o5 == n - 1 and oo))
if o5 >= 0 and o7 >= 0:
	r = min(r, o5 + o7 - int(o5 < o7) + int(o5 == n - 1 and oo))
print(r if r < 100 else -1)


s = input().strip()

def find(a, b):
	if a not in s or b not in s:
		return 40
	j = s.rindex(b)
	i = -1
	for (t, v) in enumerate(s):
		if t != j and v == a:
			i = t
	if i < 0:
		return 40
	o = 0
	if not (i and j) and len(s) > 2:
		for t in range(len(s)):
			if t != i and t != j:
				if s[t] == '0':
					o += 1
				else:
					break
	return (i > j) + 2 * len(s) - i - j - 3 + o
t = min(find('0', '0'), find('2', '5'), find('5', '0'), find('7', '5'))
print(t if t < 40 else -1)


z = input()
n = len(z)
maxa = 10 ** 18
y = int(z)
if y % 25 == 0:
	print(0)
	quit()
if n == 1:
	print(-1)
	quit()
for i in range(n):
	for j in range(n):
		if i == j:
			continue
		copy = list(z)
		swaps = 0
		for k in range(i, n - 1):
			(copy[k], copy[k + 1]) = (copy[k + 1], copy[k])
			swaps += 1
		if j > i:
			j -= 1
		for k in range(j, n - 2):
			(copy[k], copy[k + 1]) = (copy[k + 1], copy[k])
			swaps += 1
		if (10 * (ord(copy[n - 2]) - 48) + (ord(copy[n - 1]) - 48)) % 25:
			continue
		if copy[0] == '0':
			done = 0
			for yo in range(n - 2):
				if copy[yo] != '0':
					done = 1
					swaps += yo
					break
			if done:
				maxa = min(maxa, swaps)
			else:
				continue
		maxa = min(maxa, swaps)
if maxa == 10 ** 18:
	print(-1)
else:
	print(maxa)


def is_valid(a):
	i = len(a) - 1
	cnt = 0
	while i >= 0 and a[i] == 0:
		cnt += 1
		i -= 1
	if i == -1 and cnt > 0:
		return (False, cnt)
	return (True, cnt)

def create(a):
	b = [x for x in a]
	return b

def solve(d, a):
	min_ = float('inf')
	for (x, y) in [[2, 5], [7, 5], [5, 0]]:
		if x in d and y in d:
			posX = d[x][0]
			posY = d[y][0]
			b = create(a)
			b.remove(x)
			b.remove(y)
			num = posX + posY
			if posY < posX:
				num -= 1
			(flg, cnt) = is_valid(b)
			if flg == True:
				min_ = min(min_, num + cnt)
	if 0 in d and len(d[0]) >= 2:
		min_ = min(min_, d[0][0] + d[0][1] - 1)
	if min_ < float('inf'):
		return min_
	return -1
temp = int(input())
a = []
d = {}
while temp > 0:
	a.append(temp % 10)
	temp //= 10
for (i, x) in enumerate(a):
	if x not in d:
		d[x] = []
	d[x].append(i)
print(solve(d, a))


def swipe(s, i):
	return s[:i] + s[i + 1] + s[i] + (s[i + 2:] if i + 2 < len(s) else '')

def nazis(s):
	m = float('inf')
	for i in range(9):
		if s.find(str(i + 1)) > -1:
			m = min(s.find(str(i + 1)), m)
	return m
n = input()
c = float('inf')
if n.count('0') >= 2 or ('5' in n and '0' in n) or ('2' in n and '5' in n) or ('7' in n and '5' in n):
	s = n
	if n.count('0') >= 2:
		a = n.rfind('0')
		b = n.rfind('0', 0, a)
		c = 2 * len(n) - a - b - 3
	if '5' in n and '0' in n:
		cc = 0
		a = n.rfind('0')
		while n[-1] != '0':
			n = swipe(n, a)
			a += 1
			cc += 1
		b = n.rfind('5')
		while n[-2] != '5':
			n = swipe(n, b)
			b += 1
			cc += 1
		if n[0] == '0':
			cc += nazis(n)
		c = min(c, cc)
	if '2' in n and '5' in n:
		n = s
		cc = 0
		b = n.rfind('5')
		while n[-1] != '5':
			n = swipe(n, b)
			b += 1
			cc += 1
		a = n.rfind('2')
		while n[-2] != '2':
			n = swipe(n, a)
			a += 1
			cc += 1
		if n[0] == '0':
			cc += nazis(n)
		c = min(c, cc)
	if '7' in n and '5' in n:
		n = s
		cc = 0
		b = n.rfind('5')
		while n[-1] != '5':
			n = swipe(n, b)
			b += 1
			cc += 1
		a = n.rfind('7')
		while n[-2] != '7':
			n = swipe(n, a)
			a += 1
			cc += 1
		if n[0] == '0':
			cc += nazis(n)
		c = min(c, cc)
	print(c)
else:
	print(-1)


n = [int(x) for x in input()]
var = [[0, 0], [5, 2], [5, 7], [0, 5]]
res = 1000
ln = len(n)
for c in var:
	num = n[:]
	i1 = -1
	i2 = -1
	i0 = -1
	for i in range(ln - 1, -1, -1):
		if num[i] == c[0]:
			i1 = i
			break
	if i1 == -1:
		continue
	tres = 0
	idx = i1 + 1
	while i1 != ln - 1:
		(num[idx], num[i1]) = (num[i1], num[idx])
		idx = idx + 1
		i1 = i1 + 1
		tres = tres + 1
	idx = i1 + 1
	for i in range(ln - 1, -1, -1):
		if i != i1 and num[i] == c[1]:
			i2 = i
			break
	if i2 == -1:
		continue
	idx = i2 + 1
	if idx == ln:
		tres = tres + 1
		(num[i2 - 1], num[i2]) = (num[i2], num[i2 - 1])
	else:
		while i2 != ln - 2:
			(num[idx], num[i2]) = (num[i2], num[idx])
			idx = idx + 1
			i2 = i2 + 1
			tres = tres + 1
	for i in range(0, ln - 1):
		if num[i] != 0:
			i0 = i
			break
	if i0 == -1:
		continue
	idx = i0 - 1
	while i0 != 0:
		(num[idx], num[i0]) = (num[i0], num[idx])
		idx = idx - 1
		i0 = i0 - 1
		tres = tres + 1
	res = min(res, tres)
if res < 100:
	print(res)
else:
	print(-1)


n = input()
if '2' not in n or '5' not in n:
	a = 10 ** 18
else:
	a = 0
	i = len(n) - 1
	while n[i] != '5':
		i -= 1
		a += 1
	updated_n = n[0:i] + n[i + 1:]
	i = len(n) - 2
	while updated_n[i] != '2':
		i -= 1
		a += 1
	updated_n = updated_n[0:i] + updated_n[i + 1:]
	for i in range(len(updated_n)):
		if updated_n[i] != '0':
			break
	a += i
if '7' not in n or '5' not in n:
	b = 10 ** 18
else:
	b = 0
	i = len(n) - 1
	while n[i] != '5':
		i -= 1
		b += 1
	updated_n = n[0:i] + n[i + 1:]
	i = len(n) - 2
	while updated_n[i] != '7':
		i -= 1
		b += 1
	updated_n = updated_n[0:i] + updated_n[i + 1:]
	for i in range(len(updated_n)):
		if updated_n[i] != '0':
			break
	b += i
if '5' not in n or '0' not in n:
	c = 10 ** 18
else:
	c = 0
	i = len(n) - 1
	while n[i] != '0':
		i -= 1
		c += 1
	updated_n = n[0:i] + n[i + 1:]
	i = len(n) - 2
	while updated_n[i] != '5':
		i -= 1
		c += 1
	updated_n = updated_n[0:i] + updated_n[i + 1:]
	for i in range(len(updated_n)):
		if updated_n[i] != '0':
			break
	c += i
if n.count('0') < 2:
	d = 10 ** 18
else:
	d = 0
	i = len(n) - 1
	while n[i] != '0':
		i -= 1
		d += 1
	updated_n = n[0:i] + n[i + 1:]
	i = len(n) - 2
	while updated_n[i] != '0':
		i -= 1
		d += 1
	updated_n = updated_n[0:i] + updated_n[i + 1:]
	for i in range(len(updated_n)):
		if updated_n[i] != '0':
			break
	d += i
ans = min(a, b, c, d)
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


import bisect
import math
import sys
from collections import deque

def main():
	s = list(input())

	def check(last_but_one, last):
		t = s.copy()
		n = len(t)
		res = 0
		pos_last = n - 1
		while pos_last >= 0 and t[pos_last] != last:
			pos_last -= 1
		if pos_last < 0:
			return False
		while pos_last < n - 1:
			(t[pos_last], t[pos_last + 1]) = (t[pos_last + 1], t[pos_last])
			res += 1
			pos_last += 1
		pos_last_but_one = n - 2
		while pos_last_but_one >= 0 and t[pos_last_but_one] != last_but_one:
			pos_last_but_one -= 1
		if pos_last_but_one < 0:
			return False
		while pos_last_but_one < n - 2:
			(t[pos_last_but_one], t[pos_last_but_one + 1]) = (t[pos_last_but_one + 1], t[pos_last_but_one])
			res += 1
			pos_last_but_one += 1
		if t[-1] != last or t[-2] != last_but_one:
			return False
		pos = 0
		while pos < n and t[pos] == '0':
			pos += 1
		if pos == n:
			return False
		while pos > 0:
			(t[pos - 1], t[pos]) = (t[pos], t[pos - 1])
			pos -= 1
			res += 1
		if t[-1] != last or t[-2] != last_but_one:
			return False
		return res
	if len(s) == 1:
		print(-1)
	else:
		ans = math.inf
		for (a, b) in (('0', '0'), ('2', '5'), ('5', '0'), ('7', '5')):
			if a == s[-2] and b == s[-1]:
				ans = 0
				break
			ver = check(a, b)
			if ver:
				ans = min(ans, ver)
		print(-1 if ans == math.inf else ans)
main()


s = input()[::-1]
ans = 10 ** 9
mp = {'0': '05', '5': '27'}
for i in range(len(s)):
	if s[i] not in mp:
		continue
	rem = s[:i] + s[i + 1:]
	for j in range(len(rem)):
		if rem[j] not in mp[s[i]]:
			continue
		rem2 = rem[:j] + rem[j + 1:]
		if rem2:
			lastnz = -1
			while lastnz >= -len(rem2) and rem2[lastnz] == '0':
				lastnz -= 1
			if lastnz < -len(rem2):
				continue
			lastnz = -lastnz - 1
		else:
			lastnz = 0
		ans = min(ans, i + j + lastnz)
print(-1 if ans == 10 ** 9 else ans)


def inx(a, s):
	ll = []
	for i in range(len(a)):
		if s == a[i]:
			ll.append(i)
	try:
		return max(ll)
	except:
		return False

def check(a, Comb):
	Counter = 0
	Nxt = inx(a, Comb[1])
	if Nxt is False:
		Counter = 0
	else:
		for i in range(Nxt + 1, len(a)):
			(a[i - 1], a[i]) = (a[i], a[i - 1])
			Counter += 1
	Fst = inx(a, Comb[0])
	if Fst is False:
		Counter = 0
	else:
		for i in range(Fst + 1, len(a) - 1):
			(a[i - 1], a[i]) = (a[i], a[i - 1])
			Counter += 1
	if Fst is False:
		Counter = None
	if Nxt is False:
		Counter = None
	return Counter

def search(a):
	s = None
	flg = False
	for i in range(len(a)):
		if flg is True:
			break
		if a[i] == '2':
			for j in range(len(a)):
				if a[j] == '5':
					s = '255'
					flg = True
					break
		elif a[i] == '5':
			for j in range(len(a)):
				if a[j] == '2':
					s = '255'
					flg = True
					break
				elif a[j] == '7':
					s = '755'
					flg = True
					break
				elif a[j] == '0':
					s = '505'
					flg = True
					break
		elif a[i] == '7':
			for j in range(len(a)):
				if a[j] == '5':
					s = '755'
					flg = True
					break
		elif a[i] == '0':
			for j in range(len(a)):
				if a[j] == '5':
					s = '500'
					flg = True
					break
				elif a[j] == '0' and i != j:
					s = '000'
					flg = True
					break
	if s is None:
		print(-1)
		exit()
	return s

def czero(a):
	ctr = 0
	index = None
	for i in range(len(a)):
		if a[i] != '0':
			index = i
			break
	if index == 0:
		return None
	else:
		cou = index
		while a[0] == '0':
			(a[cou - 1], a[cou]) = (a[cou], a[cou - 1])
			ctr += 1
			cou -= 1
		return ctr

def search1(a, t, Comb):
	s = None
	flg = False
	if Comb[2] == '5':
		if '2' not in t:
			s = '255'
			t.append('2')
		elif '7' not in t:
			s = '755'
			t.append('7')
		elif '0' not in t:
			s = '505'
			t.append('0')
	elif Comb[2] == '0':
		if '5' not in t:
			s = '500'
			t.append('5')
		elif '0' not in t:
			s = '000'
			t.append('0')
		else:
			s = '---'
	return (s, t)

def ZERO(a, mass):
	Counter = 0
	Nxt = None
	Fst = None
	for i in range(len(a)):
		if a[i] == '0':
			if not Fst:
				Fst = i
			elif not Nxt:
				Nxt = i
			else:
				break
	if Fst is None:
		return (mass, None)
	elif Nxt is None:
		return (mass, None)
	else:
		for i in range(Nxt + 1, len(a)):
			(a[i - 1], a[i]) = (a[i], a[i - 1])
			Counter += 1
		for i in range(Fst + 1, len(a) - 1):
			(a[i - 1], a[i]) = (a[i], a[i - 1])
			Counter += 1
		f = czero(a)
		if f is not None:
			Counter += f
		mass.append(Counter)
		return (mass, Counter)
mass = []
temp = []
Counter = 0
a = input()[::-1]
if a[0] == '5' and a[1] == '2':
	print(0)
	exit()
if a[0] == '5' and a[1] == '7':
	print(0)
	exit()
if a[0] == '0' and a[1] == '0':
	print(0)
	exit()
if a[0] == '0' and a[1] == '5':
	print(0)
	exit()
Comb = search(a)
if Comb == '000':
	a = list(a)[::-1]
	Nxt = None
	Fst = None
	i = len(a) - 1
	while i != 0:
		if a[i] == '0':
			if not Fst:
				Fst = i
			elif not Nxt:
				Nxt = i
			else:
				break
		i -= 1
	for i in range(Fst + 1, len(a)):
		(a[i - 1], a[i]) = (a[i], a[i - 1])
		Counter += 1
	for i in range(Nxt + 1, len(a) - 1):
		(a[i - 1], a[i]) = (a[i], a[i - 1])
		Counter += 1
	f = czero(a)
	if f is not None:
		Counter += f
	mass.append(Counter)
else:
	b = a
	for i in range(3):
		a = list(b)[::-1]
		Counter = 0
		(Comb2, temp) = search1(a, temp, Comb)
		if Comb2 == '---':
			pass
		elif Comb2 == '000':
			(mass, Counter) = ZERO(a, mass)
			if Counter is not None:
				f = czero(a)
				if f is not None:
					Counter += f
				mass.append(Counter)
		else:
			Counter = check(a, Comb2)
			if Counter is not None:
				f = czero(a)
				if f is not None:
					Counter += f
				mass.append(Counter)
print(min(mass))


s = input()
if int(s) % 25 == 0:
	print(0)
else:
	sols = []
	d = {}
	n = list(s)
	nr = s[::-1]
	for c in ('0', '2', '5', '7'):
		p = nr.find(c)
		d[c] = p
	for (c1, c2) in (('2', '5'), ('5', '0'), ('7', '5')):
		if c1 not in n or c2 not in n:
			continue
		dc1 = d[c1]
		dc2 = d[c2]
		sol = 0
		if dc1 == len(n) - 1 and len(n) > 1 and (n[1] == '0'):
			i = 1
			while i < len(n) and (n[i] == '0' or dc2 == i):
				i += 1
			if i != len(n):
				i -= 1
				sol = i
				dc1 -= 1
				if dc2 > len(n) - i - 1:
					dc2 -= 1
		if dc2 == len(n) - 1 and len(n) > 1 and (n[1] == '0'):
			i = 1
			while i < len(n) and (n[i] == '0' or dc1 == i):
				i += 1
			if i != len(n):
				sol = i
				dc2 -= 1
				if dc1 > len(n) - i - 1:
					dc1 -= 1
		if dc1 == 0:
			sols.append(dc2 + sol)
			continue
		if dc1 < dc2:
			sols.append(dc2 + dc1 + sol)
		else:
			sols.append(dc2 + dc1 - 1 + sol)
	z1 = nr.find('0')
	if z1 != -1:
		z2 = nr.find('0', z1 + 1)
		if z2 != -1:
			sols.append(z1 + z2 - 1)
	if len(sols) == 0:
		print(-1)
	else:
		print(min(sols))


n = input()
n = list(n)
if not '5' in n and (not '0' in n) and (not '7' in n) and (not '2' in n):
	print(-1)
	quit()
wkn = []
wkn[:] = n
ans = -1
for i in reversed(list(range(len(n)))):
	if n[i] == '0':
		wk1 = n[:i] + n[i + 1:] + [n[i]]
		ans = len(n) - i - 1
		if wk1[0] != '0':
			n[:] = wk1
			break
		else:
			count = 0
			f1 = True
			for j in wk1:
				count += 1
				if j != '0':
					f1 = False
					break
			if f1:
				ans = -1
				break
			ans += count - 1
			wk1 = [wk1[count - 1]] + wk1[:count - 1] + wk1[count:]
			n[:] = wk1
			break
if ans != -1:
	f = True
	for i in reversed(list(range(len(n) - 1))):
		if n[i] == '0' or n[i] == '5':
			wk1 = n[:i] + n[i + 1:-1] + [n[i]]
			ans += len(n) - i - 2
			if wk1[0] != '0':
				f = False
				break
			else:
				count = 0
				f1 = True
				for j in wk1:
					count += 1
					if j != '0':
						f1 = False
						break
				if f1:
					ans = -1
					break
				ans += count - 1
				f = False
				break
	if f:
		ans = -1
wkans = ans
ans = -1
n = wkn
for i in reversed(list(range(len(n)))):
	if n[i] == '5':
		wk1 = n[:i] + n[i + 1:] + [n[i]]
		ans = len(n) - i - 1
		if wk1[0] != '0':
			n[:] = wk1
			break
		else:
			count = 0
			f1 = True
			for j in wk1:
				count += 1
				if j != '0':
					f1 = False
					break
			if f1:
				ans = -1
				break
			ans += count - 1
			wk1 = [wk1[count - 1]] + wk1[:count - 1] + wk1[count:]
			n[:] = wk1
			break
if ans != -1:
	f = True
	for i in reversed(list(range(len(n) - 1))):
		if n[i] == '7' or n[i] == '2':
			wk1 = n[:i] + n[i + 1:-1] + [n[i]]
			ans += len(n) - i - 2
			if wk1[0] != '0':
				f = False
				break
			else:
				count = 0
				f1 = True
				for j in wk1:
					count += 1
					if j != '0':
						f1 = False
						break
				if f1:
					ans = -1
					break
				ans += count - 1
				f = False
				break
	if f:
		ans = -1
if wkans == -1:
	print(ans)
	quit()
if ans == -1:
	print(wkans)
	quit()
print(min(ans, wkans))


def go():
	s = input()
	n = len(s)
	if '5' not in s and '0' not in s:
		return -1
	k = ['00', '25', '50', '75']
	m = float('inf')
	for x in k:
		if x == '00' and s.count('0') > 1:
			first = s.rfind('0')
			second = s[:first].rfind('0')
			d = n - 1 - first + n - 2 - second
			m = min(m, d)
		elif x != '00' and x[0] in s and (x[1] in s):
			first = s.rfind(x[0])
			second = s.rfind(x[1])
			d = n - 1 - second + n - 2 - first
			if second == 0:
				i = 1
				while s[i] == '0':
					i += 1
					d += 1
			if second < first:
				d += 1
			m = min(m, d)
	if float('inf') == m:
		return -1
	return m
print(go())


INF = 1e+18

def replace_zero(s, n):
	for i in range(1, n + 1):
		if s[i] != '0':
			return i - 1
	return None

def find_number(c, s, end=1):
	n = len(s) - end
	cnt = 0
	for i in range(n, -1, -1):
		if s[i] == c:
			if i == 0 and s[i + 1] == '0':
				cnt = replace_zero(s, n)
				if cnt is None:
					return (None, s)
			return (n - i + cnt, s[:i] + s[i + 1:] + s[i])
	return (None, s)

def make_integer(c1, c2, s):
	if c1 + c2 == s[len(s) - 2:]:
		return 0
	(cnt1, s) = find_number(c2, s)
	if cnt1 is None:
		return INF
	(cnt2, s) = find_number(c1, s, 2)
	if cnt2 is None:
		return INF
	return cnt1 + cnt2

def main():
	n = input()
	ans = min(make_integer('0', '0', n), make_integer('2', '5', n), make_integer('5', '0', n), make_integer('7', '5', n))
	if ans == INF:
		ans = -1
	print(ans)
main()


import sys
import queue
INFINITY = 10 ** 10

def main():
	n = input()
	print(solve(n))

def solve(n):
	if int(n) < 1000:
		return brute(n)
	forward = min([calc(str(n), last_digits) for last_digits in ['00', '25', '50', '75']])
	reverse = min([calc(str(n), last_digits) + 1 for last_digits in ['52', '05', '57']])
	res = min(forward, reverse)
	if res >= INFINITY:
		res = -1
	return res

def calc(n, last_digits):
	if not last_digits:
		return 0
	idx = n.rfind(last_digits[-1])
	if idx == -1:
		return INFINITY
	res = len(n) - idx - 1
	n = n[:idx] + n[idx + 1:]
	last_digits = last_digits[:-1]
	extra = 0
	if n and n[0] == '0':
		idx = len(n)
		for digit in '123456789':
			if n.find(digit) != -1:
				idx = min(idx, n.find(digit))
		if idx == len(n):
			return idx
		n = swap(n, 0, idx)
		extra = idx
	return res + calc(n, last_digits) + extra

def brute(n):
	q = queue.Queue()
	dis = dict()
	q.put(str(n))
	dis[str(n)] = 0
	while not q.empty():
		s = q.get()
		if int(s) % 25 == 0:
			return dis[s]
		for i in range(1, len(s)):
			j = i - 1
			t = swap(s, i, j)
			if t not in dis and t[0] != '0':
				dis[t] = dis[s] + 1
				q.put(t)
	return -1

def swap(s, i, j):
	chars = list(s)
	(chars[i], chars[j]) = (chars[j], chars[i])
	return ''.join((char for char in chars))
main()


from functools import cmp_to_key
import sys

def getIntList():
	return list(map(int, input().split()))
s0 = input()

def work(s0, st):
	x = s0.rfind(st[1])
	if x < 0:
		return 10000
	r = 0
	r += len(s0) - x - 1
	s0 = s0[:x] + s0[x + 1:]
	x = s0.rfind(st[0])
	if x < 0:
		return 10000
	r += len(s0) - x - 1
	s0 = s0[:x] + s0[x + 1:]
	if not s0:
		return r
	if s0 == '0' * len(s0):
		return 10000
	for i in range(len(s0)):
		if s0[i] != '0':
			return r + i
	return 10000
sz = ['00', '25', '50', '75']
z = [work(s0, s) for s in sz]
r = min(z)
if r > 1000:
	r = -1
print(r)


s = input()
n = len(s)
c = dict()
ans = 1e+18
(c['2'], c['5'], c['7'], c['0']) = (0, 0, 0, 0)
for i in s:
	if i not in c:
		c[i] = 0
	c[i] += 1
if c['0'] >= 2:
	res = -1
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			if res == -1:
				res = n - 1 - i
			else:
				res += n - 2 - i
				break
	ans = min(ans, res)
if c['0'] and c['5']:
	res = 0
	f = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			res += n - 1 - i
			break
		if s[i] == '5':
			f = 1
	for i in range(n - 1, -1, -1):
		if s[i] == '5':
			res += max(0, n - 2 - i + f)
			break
	ans = min(ans, res)
if c['2'] and c['5']:
	res = 0
	f = 0
	flag = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '5':
			res += n - 1 - i
			break
		if s[i] == '2':
			f = 1
	if i == 0 and s[1] == '0':
		for i in range(1, n):
			if s[i] != '0':
				break
		flag = i - 1
	for i in range(n - 1, -1, -1):
		if s[i] == '2':
			res += max(0, n - 2 - i + f)
			break
	ans = min(ans, res + flag)
if c['7'] and c['5']:
	res = 0
	f = 0
	flag = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '5':
			res += n - 1 - i
			break
		if s[i] == '7':
			f = 1
	if i == 0 and s[1] == '0':
		for i in range(1, n):
			if s[i] != '0':
				break
		flag = i - 1
	for i in range(n - 1, -1, -1):
		if s[i] == '7':
			res += max(0, n - 2 - i + f)
			break
	ans = min(ans, res + flag)
if ans == 1e+18:
	ans = -1
print(ans)


import itertools
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def move(s, prev, next):
	cost = abs(prev - next)
	s = list(s)
	old = s[prev]
	s = s[:prev] + s[prev + 1:]
	s = s[:next] + [old] + s[next:]
	s = ''.join(s)
	return (cost, s)
n = int(input())
st = str(n)
l = len(st)
if l == 1:
	print(-1)
	exit()
best = 10 ** 5
for (p1, p2, p3) in itertools.product(range(l), repeat=3):
	s = str(st)
	if s[p1] == 0:
		continue
	(p1_cost, s) = move(s, p1, 0)
	if s[0] == '0':
		continue
	(p2_cost, s) = move(s, p2, l - 1)
	if s[0] == '0':
		continue
	(p3_cost, s) = move(s, p3, l - 2)
	if s[0] == '0':
		continue
	if s[-2:] in ['00', '25', '50', '75']:
		best = min(best, p1_cost + p2_cost + p3_cost)
if best == 10 ** 5:
	print(-1)
else:
	print(best)


import sys
n = int(input())
if n % 25 == 0:
	print(0)
else:
	tmp = n
	digits = [0] * 10
	while tmp:
		digits[tmp % 10] += 1
		tmp //= 10
	s = str(n)
	length = len(s)
	leading_zero_inc = 0
	for i in range(1, length):
		if s[i] != '0':
			leading_zero_inc = i
			break
	ans = sys.maxsize
	if digits[0] >= 2:
		i = s.rfind('0')
		j = s.rfind('0', 0, i)
		i = length - 1 - i
		j = length - 2 - j
		ans = min(ans, i + j)
	if digits[2] >= 1 and digits[5] >= 1:
		i = s.rfind('2')
		j = s.rfind('5')
		inc = False
		if j < i:
			inc = True
		if i == 0:
			i = length - 3 + leading_zero_inc
		else:
			i = length - 2 - i
		if j == 0:
			j = length - 2 + leading_zero_inc
		else:
			j = length - 1 - j
		ans = min(ans, i + j + inc)
	if digits[5] >= 1 and digits[0] >= 1:
		i = s.rfind('5')
		j = s.rfind('0')
		inc = False
		if j < i:
			inc = True
		if i == 0 and j != 1:
			i = length - 3 + leading_zero_inc
		else:
			i = length - 2 - i
		j = length - 1 - j
		ans = min(ans, i + j + inc)
	if digits[7] >= 1 and digits[5] >= 1:
		i = s.rfind('7')
		j = s.rfind('5')
		inc = False
		if j < i:
			inc = True
		if i == 0:
			i = length - 3 + leading_zero_inc
		else:
			i = length - 2 - i
		if j == 0:
			j = length - 2 + leading_zero_inc
		else:
			j = length - 1 - j
		ans = min(ans, i + j + inc)
	if ans == sys.maxsize:
		ans = -1
	print(ans)


l = [int(e) for e in input().strip()]

def lei00(l):
	result = []
	for i in range(len(l) - 1, -1, -1):
		if l[i] == 0:
			result.append(i)
		if len(result) == 2:
			break
	if len(result) < 2:
		return None
	return 2 * len(l) - result[0] - result[1] - 3

def lei(l, x, y):
	assert x != y
	ix = None
	iy = None
	for i in range(len(l) - 1, -1, -1):
		if l[i] == x and ix == None:
			ix = i
		elif l[i] == y and iy == None:
			iy = i
		if ix != None and iy != None:
			break
	if ix == None or iy == None:
		return None
	result = 2 * len(l) - ix - iy - 3
	if ix > iy:
		result += 1
	if x == 7 or x == 2:
		assert y == 5
		if iy == 0 and l[1] == 0:
			if len(l) >= 4:
				iii = 1
				while True:
					iii += 1
					if l[iii] != 0:
						break
				result += iii - 1
			if len(l) == 3:
				return None
	return result
l = [lei00(l), lei(l, 5, 0), lei(l, 7, 5), lei(l, 2, 5)]
result = None
for e in l:
	if e == None:
		continue
	if result == None or result > e:
		result = e
if result == None:
	print(-1)
else:
	print(result)


def idx(s, c, start=0):
	try:
		return s[::-1].index(c, start)
	except:
		return -1

def main():
	s = input()
	zero_cnt = 0
	while zero_cnt < len(s) - 1 and s[zero_cnt + 1] == '0':
		zero_cnt += 1
	i01 = idx(s, '0')
	i02 = idx(s, '0', i01 + 1)
	i2 = idx(s, '2')
	i5 = idx(s, '5')
	i7 = idx(s, '7')
	r = 666
	if i01 != -1 and i02 != -1:
		r = min(r, i01 + (i02 - 1))
	if i2 != -1 and i5 != -1:
		r = min(r, i2 + i5 - int(i2 > i5) + int((i2 == len(s) - 1 or i5 == len(s) - 1) and zero_cnt))
	if i5 != -1 and i01 != -1:
		r = min(r, i5 + i01 - int(i5 > i01))
	if i7 != -1 and i5 != -1:
		r = min(r, i7 + i5 - int(i7 > i5) + int((i7 == len(s) - 1 or i5 == len(s) - 1) and zero_cnt))
	print(r if r != 666 else -1)
main()


def finder(n, ind):
	i = ind - 1
	while i >= 0:
		if n[i] == '0':
			break
		else:
			i = i - 1
	return i

def zeros(n):
	count = 0
	pol = 1
	while pol < l:
		if n[pol] == '0':
			count = count + 1
		else:
			break
		pol = pol + 1
	return count
n = input()
ans = []
l = len(n)
if '2' in n and '5' in n:
	c2 = l - n.rfind('2') - 2
	c5 = l - n.rfind('5') - 1
	if c2 < c5:
		k = c2 + c5 + 1
	else:
		k = c2 + c5
	if n[1] == '0' and (n[0] == '2' or n[0] == '5'):
		ans.append(k + zeros(n))
	else:
		ans.append(k)
if '5' in n and '0' in n:
	c5 = l - n.rfind('5') - 2
	c0 = l - n.rfind('0') - 1
	if c5 < c0:
		k = c5 + c0 + 1
	else:
		k = c5 + c0
	ans.append(k)
if '7' in n and '5' in n:
	c7 = l - n.rfind('7') - 2
	c5 = l - n.rfind('5') - 1
	if c7 < c5:
		k = c7 + c5 + 1
	else:
		k = c7 + c5
	if n[1] == '0' and (n[0] == '7' or n[0] == '5'):
		ans.append(k + zeros(n))
	else:
		ans.append(k)
if n.count('0') >= 2:
	c01 = l - n.rfind('0') - 2
	c02 = l - finder(n, n.rfind('0')) - 1
	ans.append(c01 + c02)
if int(n) % 25 == 0:
	print(0)
elif len(ans) == 0:
	print(-1)
else:
	print(min(ans))


def digits(n):
	return len(str(n))

def digit(n, i):
	if i < digits(n):
		return int(str(n)[digits(n) - i - 1])
	return 0

def swap_digits(n, i, j):
	return n + digit(n, i) * (10 ** j - 10 ** i) + digit(n, j) * (10 ** i - 10 ** j)
n = int(input())
if n % 25 == 0:
	print(0)
	exit()
ans = []
for i in range(digits(n)):
	for j in range(digits(n)):
		if i == j:
			continue
		(x, y, cur, cur_ans) = (i, j, n, 0)
		if y > x:
			x += 1
		while y > 0:
			cur = swap_digits(cur, y, y - 1)
			y -= 1
			cur_ans += 1
		while x > 1:
			cur = swap_digits(cur, x, x - 1)
			x -= 1
			cur_ans += 1
		for k in range(digits(n), -1, -1):
			if digit(cur, k) != 0:
				cur_ans += digits(n) - 1 - k
				break
		if cur % 25 == 0:
			ans.append(cur_ans)
print(min(ans) if len(ans) != 0 else -1)


num = input()[::-1]
case1 = False
case2 = False
case3 = False
case4 = False
zero1 = num.find('0')
zero2 = num.find('0', num.find('0') + 1)
five = num.find('5')
seven = num.find('7')
two = num.find('2')
if zero1 != -1:
	if five != -1:
		case1 = True
if five != -1:
	if two != -1:
		case2 = True
if zero1 != -1:
	if zero2 != -1:
		case3 = True
if five != -1:
	if seven != -1:
		case4 = True
cases = []
if case1:
	cases.append([zero1, five])
if case2:
	cases.append([five, two])
if case3:
	cases.append([zero1, zero2])
if case4:
	cases.append([five, seven])

def score(pair):
	i = 0
	line = num[::-1]
	while i < len(num) - 2 and line.replace(str(num[pair[0]]), '').replace(str(num[pair[1]]), '')[i] == '0':
		i += 1
	if pair[0] < pair[1]:
		return pair[0] + pair[1] - 1 + i
	else:
		return pair[0] + pair[1] + i
if cases:
	print(min([score(p) for p in cases]))
else:
	print(-1)


import re
Inf = 1061109567

def remove(s_, pos):
	return ''.join((ch for (i, ch) in enumerate(s_) if i != pos))

def cal(s_, ch2, ch1):
	(pos1, n) = (s_.rfind(ch1), len(s_))
	if pos1 == -1:
		return Inf
	s_ = remove(s_, pos1)
	pos2 = s_.rfind(ch2)
	if pos2 == -1:
		return Inf
	s_ = remove(s_, pos2)
	sum_ = 0
	if re.match('(^[0]+)', s_) is not None:
		sum_ = len(re.match('(^[0]+)', s_).group(1))
	return sum_ + (n - 1 - pos1) + (n - 2 - pos2)
s = input()
ans = min((cal(s, ch1, ch2) for (ch1, ch2) in [('0', '0'), ('2', '5'), ('5', '0'), ('7', '5')]))
print(-1 if ans == Inf else ans)


from bisect import bisect_right, bisect_left
n = input().strip()
res = 1e+18

def solve(x, y):
	global res
	cur = [x for x in n]
	c = 0
	for i in range(x, len(cur) - 1):
		(cur[i], cur[i + 1]) = (cur[i + 1], cur[i])
		c += 1
	for i in range(y - (y > x), len(cur) - 2):
		(cur[i], cur[i + 1]) = (cur[i + 1], cur[i])
		c += 1
	p = -1
	for (i, e) in enumerate(cur):
		if e != '0':
			p = i
			break
	for i in range(p, 0, -1):
		(cur[i], cur[i - 1]) = (cur[i - 1], cur[i])
		c += 1
	if int(''.join(cur)) % 25 == 0:
		res = min(res, c)
for i in range(len(n)):
	for j in range(len(n)):
		if i != j:
			solve(i, j)
if res == 1e+18:
	print(-1)
else:
	print(res)


maxint = 2147483647

class mylist(list):

	def index(self, obj, *args):
		try:
			if len(args) == 0:
				return super(mylist, self).index(obj)
			if len(args) == 1:
				return super(mylist, self).index(obj, args[0])
			return super(mylist, self).index(obj, args[0], args[1])
		except ValueError:
			return -1

def cost(target, index1, index2):
	if index1 == -1 or index2 == -1:
		return maxint
	if target == '00':
		return index1 + index2 - 1
	else:
		return index1 + index2 - 1 if index2 < index1 else index1 + index2

def calcCosts(rn):
	i0 = rn.index('0')
	i00 = rn.index('0', i0 + 1)
	i2 = rn.index('2')
	i5 = rn.index('5')
	i7 = rn.index('7')
	costs = [('00', cost('00', i0, i00)), ('50', cost('50', i5, i0)), ('25', cost('25', i2, i5)), ('75', cost('75', i7, i5))]
	costs.sort(key=lambda tup: tup[1])
	return costs

def swapCritical(rn, i):
	if i == len(rn) - 1:
		for index in range(i - 1, -1, -1):
			if rn[index] != '0':
				break
	else:
		index = i - 1
	(rn[index], rn[index + 1]) = (rn[index + 1], rn[index])
	return rn

def swap(rn, typ):
	if typ == '00':
		i0 = rn.index('0')
		i00 = rn.index('0', i0 + 1)
		if i0 != 0:
			(rn[i0 - 1], rn[i0]) = (rn[i0], rn[i0 - 1])
			return (rn, 1)
		if i00 == 1:
			return (rn, 0)
		(rn[i00 - 1], rn[i00]) = (rn[i00], rn[i00 - 1])
		return (rn, 1)
	if typ == '50':
		i0 = rn.index('0')
		i5 = rn.index('5')
		if i0 != 0:
			if i0 < i5 or i5 == 0:
				(rn[i0 - 1], rn[i0]) = (rn[i0], rn[i0 - 1])
				return (rn, 1)
			(rn[i5 - 1], rn[i5]) = (rn[i5], rn[i5 - 1])
			return (rn, 1)
		if i5 == 1:
			return (rn, 0)
		if i5 != len(rn) - 1:
			(rn[i5 - 1], rn[i5]) = (rn[i5], rn[i5 - 1])
			return (rn, 1)
		rn = swapCritical(rn, i5)
		return (rn, 1)
	if typ == '25':
		i2 = rn.index('2')
		i5 = rn.index('5')
		if i5 != 0:
			if i5 < i2 or i2 == 0:
				swapCritical(rn, i5)
				return (rn, 1)
			(rn[i2 - 1], rn[i2]) = (rn[i2], rn[i2 - 1])
			return (rn, 1)
		if i2 == 1:
			return (rn, 0)
		if i2 != len(rn) - 1:
			(rn[i2 - 1], rn[i2]) = (rn[i2], rn[i2 - 1])
			return (rn, 1)
		rn = swapCritical(rn, i2)
		return (rn, 1)
	if typ == '75':
		i7 = rn.index('7')
		i5 = rn.index('5')
		if i5 != 0:
			if i5 < i7 or i7 == 0:
				swapCritical(rn, i5)
				return (rn, 1)
			(rn[i7 - 1], rn[i7]) = (rn[i7], rn[i7 - 1])
			return (rn, 1)
		if i7 == 1:
			return (rn, 0)
		if i7 != len(rn) - 1:
			(rn[i7 - 1], rn[i7]) = (rn[i7], rn[i7 - 1])
			return (rn, 1)
		rn = swapCritical(rn, i7)
		return (rn, 1)

def realCost(rn, maximum):
	costs = calcCosts(rn)
	bestCost = maximum
	for cost in costs:
		if cost[1] >= bestCost:
			return bestCost
		newrn = mylist(rn)
		(newrn, tempcost) = swap(newrn, cost[0])
		if tempcost == 0:
			return 0
		thiscost = tempcost + realCost(newrn, bestCost - 1)
		if thiscost < bestCost:
			bestCost = thiscost
n = input()
rn = n[::-1]
rn = mylist(rn)
rcost = realCost(rn, maxint)
print(rcost if rcost < maxint else -1)


n = input().rstrip()
l = len(n)
c0 = 0
ii = 1
while ii < l and n[ii] == '0':
	c0 += 1
	ii += 1
ans = 10 ** 9
if n.count('0') >= 2:
	idx = []
	for i in range(l)[::-1]:
		if n[i] == '0':
			idx.append(l - 1 - i)
		if len(idx) >= 2:
			break
	c00 = idx[0] + idx[1] - 1
	ans = min(ans, c00)
if n.count('0') and n.count('5'):
	c50 = 10 ** 9
	idx0 = []
	idx5 = []
	for i in range(l)[::-1]:
		if n[i] == '0':
			idx0.append(l - 1 - i)
		if n[i] == '5':
			idx5.append(l - 1 - i)
	for i0 in idx0:
		for i5 in idx5:
			if i5 > i0:
				c50 = min(c50, i0 + i5 - 1)
			else:
				c50 = min(c50, i5 + i0)
	ans = min(ans, c50)
if n.count('7') and n.count('5'):
	c75 = 10 ** 9
	idx7 = []
	idx5 = []
	for i in range(l)[::-1]:
		if n[i] == '7':
			idx7.append(l - 1 - i)
		if n[i] == '5':
			idx5.append(l - 1 - i)
	for i7 in idx7:
		for i5 in idx5:
			if i7 > i5:
				if i7 == l - 1:
					c75 = min(c75, i7 + i5 - 1 + c0)
				else:
					c75 = min(c75, i7 + i5 - 1)
			elif i5 == l - 1:
				c75 = min(c75, i7 + i5 + c0)
			else:
				c75 = min(c75, i7 + i5)
	ans = min(ans, c75)
if n.count('2') and n.count('5'):
	c25 = 10 ** 9
	idx2 = []
	idx5 = []
	for i in range(l)[::-1]:
		if n[i] == '2':
			idx2.append(l - 1 - i)
		if n[i] == '5':
			idx5.append(l - 1 - i)
	for i2 in idx2:
		for i5 in idx5:
			if i2 > i5:
				if i2 == l - 1:
					c25 = min(c25, i2 + i5 - 1 + c0)
				else:
					c25 = min(c25, i2 + i5 - 1)
			elif i5 == l - 1:
				c25 = min(c25, i2 + i5 + c0)
			else:
				c25 = min(c25, i2 + i5)
	ans = min(ans, c25)
if ans == 10 ** 9:
	print(-1)
else:
	print(ans)


def check():
	res = 0
	if cnt[0] > 1:
		res += 1
	if cnt[0] > 0 and cnt[5] > 0:
		res += 2
	if cnt[5] > 0 and (cnt[2] > 0 or cnt[7] > 0):
		res += 4
	return res
num = int(input())
cnt = []
str = []
for i in range(10):
	cnt.append(0)
while num != 0:
	i = num % 10
	str.append(i)
	cnt[i] += 1
	num //= 10
k = check()
ans = 40
if k == 0:
	print('-1')
if k & 1 == 1:
	tmp = index = 0
	cok = 2
	for i in str:
		if i == 0:
			tmp += index
			cok -= 1
		index += 1
		if cok == 0:
			break
	ans = min(ans, tmp - 1)
if k & 2 == 2:
	index = 0
	zero = five = -1
	for i in str:
		if i == 0 and zero == -1:
			zero = index
		elif i == 5 and five == -1:
			five = index
		index += 1
	if zero < five:
		ans = min(ans, zero + five - 1)
	else:
		ans = min(ans, zero + five)
if k & 4 == 4:
	index = 0
	five = tos = -1
	for i in str:
		if i == 5 and five == -1:
			five = index
		elif tos == -1 and (i == 2 or i == 7):
			tos = index
		index += 1
	if five < tos:
		ans = min(ans, five + tos - 1)
	elif five == len(str) - 1:
		index = five - 1
		while index >= 0 and (index == tos or str[index] == 0):
			index -= 1
		else:
			if index == five - 1:
				ans = min(ans, five + tos)
			elif index != -1:
				tmp = five - index
				five -= 1
				if index < tos:
					tos -= 1
				tmp += five + tos
				ans = min(ans, tmp)
			elif len(str) == 2:
				ans = 1
	else:
		ans = min(ans, five + tos)
if k != 0:
	print(ans)


def permutate(s):
	answer = 1000
	n = len(s)
	z = 0
	if s.count('2') >= 1 and s.count('5') >= 1:
		k1 = s.rfind('2')
		k2 = s.rfind('5')
		s1 = s[:k1] + s[k1 + 1:]
		m = s1.rfind('5')
		s2 = s1[:m] + s1[m + 1:]
		if len(s2) > 0 and s2[0] == '0':
			for i in range(1, len(s2)):
				if s2[i] != '0':
					z = i
					break
		if k2 < k1:
			answer = min(answer, 2 * n - k2 - k1 - 2 + z)
		elif k2 > k1:
			answer = min(answer, 2 * n - k2 - k1 - 3 + z)
	z = 0
	if s.count('5') >= 1 and s.count('0') >= 1:
		k3 = s.rfind('5')
		k4 = s.rfind('0')
		s1 = s[:k3] + s[k3 + 1:]
		m = s1.rfind('0')
		s2 = s1[:m] + s1[m + 1:]
		if len(s2) > 0 and s2[0] == '0':
			for i in range(1, len(s2)):
				if s2[i] != '0':
					z = i
					break
		if k4 < k3:
			answer = min(answer, 2 * n - k4 - k3 - 2 + z)
		elif k4 > k3:
			answer = min(answer, 2 * n - k4 - k3 - 3 + z)
	z = 0
	if s.count('7') >= 1 and s.count('5') >= 1:
		k5 = s.rfind('7')
		k6 = s.rfind('5')
		s1 = s[:k5] + s[k5 + 1:]
		m = s1.rfind('5')
		s2 = s1[:m] + s1[m + 1:]
		if len(s2) > 0 and s2[0] == '0':
			for i in range(1, len(s2)):
				if s2[i] != '0':
					z = i
					break
		if k6 < k5:
			answer = min(answer, 2 * n - k5 - k6 - 2 + z)
		elif k6 > k5:
			answer = min(answer, 2 * n - k6 - k5 - 3 + z)
	z = 0
	if s.count('0') > 1:
		k7 = s.rfind('0')
		k8 = s[:s.rfind('0')].rfind('0')
		s1 = s[:k7] + s[k7 + 1:]
		m = s1.rfind('0')
		s2 = s1[:m] + s1[m + 1:]
		if len(s2) > 0 and s2[0] == '0':
			for i in range(1, len(s2)):
				if s2[i] != '0':
					z = i
					break
		answer = min(answer, 2 * n - 3 - k7 - k8 + z)
	if answer == 1000:
		return -1
	return answer
l = input()
print(permutate(l))


n = input()
ans = 10000
n1 = list(n)
n2 = list(n)
l = len(n2)
curr = 0
for i in range(l - 1, -1, -1):
	if n1[i] == '0':
		curr += l - 1 - i
		for j in range(i, l - 1):
			(n1[j], n1[j + 1]) = (n1[j + 1], n1[j])
		for j in range(l - 2, -1, -1):
			if n1[j] == '5' or n1[j] == '0':
				curr += l - 2 - j
				ans = min(ans, curr)
				break
		else:
			continue
		break
curr = 0
for i in range(l - 1, -1, -1):
	if n2[i] == '5':
		curr += l - 1 - i
		if i == 0 and n2[1] == '0':
			for j in range(1, l):
				if n2[j] != '0':
					curr += j - 1
					(n2[1], n2[j]) = (n2[j], n2[1])
					break
			else:
				break
		for j in range(i, l - 1):
			(n2[j], n2[j + 1]) = (n2[j + 1], n2[j])
		for j in range(l - 2, -1, -1):
			if n2[j] == '2' or n2[j] == '7':
				curr += l - 2 - j
				ans = min(ans, curr)
				break
		else:
			continue
		break
if ans > 1000:
	ans = -1
print(ans)


INF = 1000

def compute_pair(s, pair):
	n = len(s)
	try:
		l2 = s.rindex(pair[1])
		l1 = s.rindex(pair[0])
		if pair[0] == '0':
			l1 = s[:l2].rindex(pair[1])
	except:
		return INF
	steps = abs(n - 2 - l1) + abs(n - 1 - l2)
	if l1 > l2 and l1 != n - 1:
		steps += 1
	if l1 == n - 1:
		steps -= 1
	(l1, l2) = (min(l1, l2), max(l1, l2))
	new_s = s[:l1] + s[l1 + 1:l2] + s[l2 + 1:]
	if new_s.startswith('0'):
		for (i, q) in enumerate(new_s):
			if q != '0':
				steps += i
				break
	return steps

def main():
	s = input()
	best = INF
	if int(s) < 10:
		print(-1)
		return
	for pair in ['00', '25', '50', '75']:
		l = compute_pair(s, pair)
		if l < best:
			best = l
	if best == INF:
		best = -1
	print(best)
main()


from sys import stdin

def f(n, x, y):
	ans = 0
	cur = n.rfind(y)
	if cur < 0:
		return 10 ** 18
	ans += len(n) - 1 - cur
	n = n[:cur] + n[cur + 1:] + y
	cur = n[:-1].rfind(x)
	if cur < 0:
		return 10 ** 18
	ans += len(n) - 2 - cur
	n = n[:-1][:cur] + n[:-1][cur + 1:] + x + y
	cur = 0
	has0 = False
	while cur < len(n) - 2 and n[cur] == '0':
		has0 = True
		cur += 1
	if has0 and cur >= len(n) - 2:
		return 10 ** 18
	return ans + cur

def main():
	for line in stdin.readlines():
		n = line.strip()
		ret = [f(n, '2', '5'), f(n, '7', '5'), f(n, '5', '0'), f(n, '0', '0')]
		if min(ret) == 10 ** 18:
			print(-1)
		else:
			print(min(ret))
main()


from copy import deepcopy

def nonzeroind(l):
	l.reverse()
	for i in range(1, len(l)):
		if l[i] != 0:
			return len(l) - 1 - i
		i = i + 1
	return -1

def parse(nl, num):
	try:
		i = nl.index(num)
	except:
		return (60, nl)
	if i == len(nl) - 1 and nl[i - 1] == 0:
		ind = nonzeroind(nl)
		nl.reverse()
		if ind == -1:
			return (60, nl)
		else:
			nl[i - 1] = nl[ind]
			nl[ind] = 0
			intermed = parse(deepcopy(nl), num)
			return (intermed[0] + i - 1 - ind, intermed[1])
	else:
		nl.pop(i)
		return (i, nl)
num = input('')
numarr = []
for dig in num:
	numarr.append(int(dig))
numarr.reverse()
o11 = parse(deepcopy(numarr), 5)
o12 = parse(o11[1], 7)
o21 = parse(deepcopy(numarr), 5)
o22 = parse(o21[1], 2)
o31 = parse(deepcopy(numarr), 0)
o32 = parse(o31[1], 5)
o41 = parse(deepcopy(numarr), 0)
o42 = parse(o41[1], 0)
ans = min(o11[0] + o12[0], o21[0] + o22[0], o31[0] + o32[0], o41[0] + o42[0])
if ans > 40:
	print(-1)
else:
	print(ans)


from sys import stdout, stdin, setrecursionlimit
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from os import *

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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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:
			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')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))

def fast():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in fast().split()]
(z, zz) = (fast, lambda : map(int, z().split()))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer, end='\n'):
	stdout.write(str(answer) + end)
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]

def solve(n):
	if int(n) % 25 == 0:
		return 0
	if n in {'52', '57'}:
		return 1
	ans = float('inf')
	for (x, y) in ['00', '25', '50', '75']:
		for i in range(len(n)):
			if n[i] == '0':
				continue
			for j in range(len(n)):
				if i == j or n[j] != x:
					continue
				for k in range(len(n)):
					if k == i or k == j or n[k] != y:
						continue
					ans = min(ans, len(n) * 2 - 3 + i - j - k + (j > k) - (i > j) - (i > k))
	return ans if ans < float('inf') else -1
print(solve(fast()))


def main():
	ar = list(input())
	size = len(ar)
	ans = 100000.0
	for i in range(size):
		for j in range(size):
			if i == j:
				continue
			s = list(ar)
			cost = 0
			for k in range(i, size - 1):
				tmp = s[k]
				s[k] = s[k + 1]
				s[k + 1] = tmp
				cost += 1
			if i < j:
				for k in range(j - 1, size - 2):
					tmp = s[k]
					s[k] = s[k + 1]
					s[k + 1] = tmp
					cost += 1
			else:
				for k in range(j, size - 2):
					tmp = s[k]
					s[k] = s[k + 1]
					s[k + 1] = tmp
					cost += 1
			pos = -1
			for k in range(size):
				if s[k] != '0':
					pos = k
					break
			for k in range(pos, 0, -1):
				tmp = s[k]
				s[k] = s[k - 1]
				s[k - 1] = tmp
				cost += 1
			curr_num = int(''.join(s))
			if curr_num % 25 == 0:
				ans = min(ans, cost)
	if ans == 100000.0:
		print(-1)
	else:
		print(ans)
main()


def swap(ar, ind1, ind2):
	if ind2 > ind1:
		for i in range(ind1, ind2):
			temp = ar[i]
			ar[i] = ar[i + 1]
			ar[i + 1] = temp
	else:
		for i in range(ind1, ind2, -1):
			temp = ar[i]
			ar[i] = ar[i - 1]
			ar[i - 1] = temp
n = list(input())
n2 = len(n)
ans = 100
for i in range(n2):
	for j in range(n2):
		nc = [0] * n2
		for i2 in range(n2):
			nc[i2] = n[i2]
		swap(nc, i, n2 - 1)
		swap(nc, j, n2 - 2)
		num = 0
		for z in range(n2):
			if nc[z] != '0':
				num = z
				break
		swap(nc, 0, num)
		if int(''.join(nc)) % 25 == 0:
			ans = min(ans, abs(n2 - 2 - j) + abs(n2 - 1 - i) + num)
if ans == 100:
	print(-1)
else:
	print(ans)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
import heapq
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def f(s, a, b, c):
	arr = list(s)
	cost1 = c
	l = len(arr) - 1
	f = 0
	for need in [b, a]:
		for i in range(l, -1, -1):
			if arr[i] == need:
				f += 1
				x = i
				while x != l:
					cost1 += 1
					(arr[x], arr[x + 1]) = (arr[x + 1], arr[x])
					x += 1
				break
		l -= 1
	if f != 2 or arr[0] == '0':
		cost1 = math.inf
	return cost1

def main():
	n = ii()
	ans = math.inf
	s = str(n)
	i = 0
	while i < len(s):
		if s[i] != '0':
			tmp = list(s)
			x = i
			c = 0
			while x != 0:
				(tmp[x], tmp[x - 1]) = (tmp[x - 1], tmp[x])
				x -= 1
				c += 1
			tmp = ''.join(tmp)
			ans = min(ans, f(tmp, '0', '0', c), f(tmp, '2', '5', c), f(tmp, '5', '0', c), f(tmp, '7', '5', c))
		i += 1
	if ans == math.inf:
		print(-1)
	else:
		print(ans)
main()


def solve(n):
	if int(n) % 25 == 0:
		return 0
	if n in {'52', '57'}:
		return 1
	ans = float('inf')
	for (x, y) in ['00', '25', '50', '75']:
		for i in range(len(n)):
			if n[i] == '0':
				continue
			for j in range(len(n)):
				if i == j or n[j] != x:
					continue
				for k in range(len(n)):
					if k in {i, j} or n[k] != y:
						continue
					ans = min(ans, len(n) * 2 - 3 + i - j - k + (j > k) - (i > j) - (i > k))
	return ans if ans < float('inf') else -1
print(solve(input()))


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
S = list(map(int, input()))
N = len(S)
if N <= 1:
	print(-1)
	exit()

def calc(num1, num2):
	if S[-2] == num1 and S[-1] == num2:
		return 0
	if S[-1] == num1 and S[-2] == num2:
		return 1
	res = 0
	T = S[:]
	if T[-1] != num2:
		for i in range(N - 2, -1, -1):
			if T[i] == num2:
				for j in range(i, N - 1):
					(T[j], T[j + 1]) = (T[j + 1], T[j])
					res += 1
				break
		else:
			return INF
	if T[-2] != num1:
		for i in range(N - 3, -1, -1):
			if T[i] == num1:
				for j in range(i, N - 2):
					(T[j], T[j + 1]) = (T[j + 1], T[j])
					res += 1
				break
		else:
			return INF
	if T[0] == 0:
		for i in range(N - 2):
			if T[i] != 0:
				break
			else:
				res += 1
		else:
			return INF
	return res
ans = min(calc(5, 0), calc(2, 5), calc(7, 5), calc(0, 0))
if ans == INF:
	print(-1)
else:
	print(ans)


s = input()
globans = 10 ** 9
for i_ in range(len(s)):
	for j_ in range(len(s)):
		i = i_
		j = j_
		if j == i:
			continue
		if i == len(s) - 1:
			ans = abs(len(s) - j - 1)
		else:
			ans = abs(len(s) - i - 2) + abs(len(s) - j - 1) + int(j < i)
		if i > j:
			(i, j) = (j, i)
		nw = s[:i] + s[i + 1:j] + s[j + 1:] + s[i_] + s[j_]
		for x in range(len(s)):
			if nw[x] == '0':
				continue
			ans += x
			nw = nw[x] + nw[:x] + nw[x + 1:]
			break
		if int(nw) % 25 == 0:
			globans = min(globans, ans)
if globans == 10 ** 9:
	print(-1)
else:
	print(globans)


def func(l, x, y):
	ans = 0
	ll = l.copy()
	i = ll.index(x)
	ans = ans + i
	ll = [ll[i]] + ll[:i] + ll[i + 1:]
	i = ll.index(y, 1)
	ans = ans + (i - 1)
	ll = ll[:1] + [ll[i]] + ll[1:i] + ll[i + 1:]
	i = len(ll) - 1
	while ll[i] == 0:
		i = i - 1
	ans = ans + len(l) - 1 - i
	ll = ll[:i] + ll[i + 1:] + [ll[i]]
	if (10 * ll[1] + ll[0]) % 25 == 0:
		return ans
	else:
		return -1
l = [int(i) for i in list(input())]
l = l[::-1]
ans = 1000000
if l.count(0) >= 2:
	x = func(l, 0, 0)
	if x != -1:
		ans = min(x, ans)
if l.count(0) >= 1 and l.count(5) >= 1:
	x = func(l, 0, 5)
	if x != -1:
		ans = min(x, ans)
if l.count(5) >= 1 and l.count(2) >= 1:
	x = func(l, 5, 2)
	if x != -1:
		ans = min(x, ans)
if l.count(5) >= 1 and l.count(7) >= 1:
	x = func(l, 5, 7)
	if x != -1:
		ans = min(x, ans)
if ans == 1000000:
	print(-1)
else:
	print(ans)


import math
import copy
n = list(map(int, input()))
n.reverse()
Min = 2 ** 10
A = copy.copy(n)
B = copy.copy(n)
C = copy.copy(n)
D = copy.copy(n)
if n[0] == 0 and n[1] == 0:
	print('0')
	exit()
if n.count(0) > 1:
	num = A.index(0)
	A.pop(num)
	A.insert(0, 1)
	num += A.index(0) - 1
	A.pop(A.index(0))
	A.insert(1, 0)
	if A[len(n) - 1] == 0:
		x = len(n) - 1
		while A[x] == 0:
			num += 1
			x -= 1
	Min = min(Min, num)
if 0 in n and 5 in n:
	num = B.index(0)
	B.pop(num)
	B.insert(0, 0)
	num += B.index(5) - 1
	B.pop(B.index(5))
	B.insert(1, 5)
	if B[len(n) - 1] == 0:
		x = len(n) - 1
		while B[x] == 0:
			num += 1
			x -= 1
	Min = min(Min, num)
if 2 in n and 5 in n:
	num = C.index(5)
	C.pop(num)
	C.insert(0, 5)
	num += C.index(2) - 1
	C.pop(C.index(2))
	C.insert(1, 2)
	if C[len(n) - 1] == 0:
		x = len(n) - 1
		while C[x] == 0:
			num += 1
			x -= 1
	Min = min(Min, num)
if 7 in n and 5 in n:
	num = D.index(5)
	D.pop(num)
	D.insert(0, 5)
	num += D.index(7) - 1
	D.pop(D.index(7))
	D.insert(1, 7)
	x = len(n) - 1
	while D[x] == 0:
		num += 1
		x -= 1
	Min = min(Min, num)
if Min == 2 ** 10:
	print('-1')
	exit()
else:
	print(Min)


def swap(N, a, b):
	A = N[a]
	B = N[b]
	N[a] = B
	N[b] = A

def getToEnd(N, a, b):
	bInd = -1
	for i in range(len(N) - 1, -1, -1):
		if N[i] == b:
			bInd = i
			break
	total = 0
	if bInd == -1:
		return q
	for i in range(bInd, len(N) - 1):
		total += 1
		swap(N, i, i + 1)
	aInd = -1
	for i in range(len(N) - 2, -1, -1):
		if N[i] == a:
			aInd = i
			break
	if aInd == -1:
		return q
	for i in range(aInd, len(N) - 2):
		total += 1
		swap(N, i, i + 1)
	if N[0] == '0':
		worked = False
		for i in range(len(N) - 2):
			if N[i] == '0':
				total += 1
			else:
				worked = True
				break
		if not worked:
			return q
	return total
q = 999999
N = list(input())
a = getToEnd(list(N), '0', '0')
b = getToEnd(list(N), '2', '5')
c = getToEnd(list(N), '5', '0')
d = getToEnd(list(N), '7', '5')
if a == q and b == q and (c == q) and (d == q):
	print(-1)
else:
	print(min(a, b, c, d))


s = input()[::-1]
l = len(s)

def f(c, i=0):
	return s.find(c, i) + 1 or 50
i0_1 = f('0')
i5 = f('5')
i05 = min(f('0', i0_1), i5)
m1 = i0_1 + i05 + (i05 < i0_1) - 3
i27 = min(f('2'), f('7'))
m2 = i5 + i27 + (i27 < i5) - 3
if l == i5:
	while s[i5 - 2] == '0':
		i5 -= 1
	m2 += l - i5
m = min(m1, m2)
print((-1, m)[m < 40])


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007

def mover(s, ind1, ind2):
	s = list(s)
	count = 0
	if ind2 > ind1:
		ind2 -= 1
	for i in range(ind1, len(s) - 1):
		(s[i], s[i + 1]) = (s[i + 1], s[i])
		count += 1
	for i in range(ind2, len(s) - 2):
		(s[i], s[i + 1]) = (s[i + 1], s[i])
		count += 1
	return [count, ''.join(s)]

def check(s):
	count = 0
	for i in range(len(s) - 2):
		if s[i] != '0':
			return count
		else:
			count += 1
	return -1
n = is1()
res = float('inf')
cond = [['2', '5'], ['5', '0'], ['7', '5']]
for (i, j) in cond:
	if i in n and j in n:
		two = n.rfind(i)
		five = n.rfind(j)
		(count, s) = mover(n, five, two)
		temp = check(s)
		if temp != -1:
			res = min(res, count + temp)
		elif len(n) == 2:
			res = count
if res != 0 and n.count('0') >= 2:
	zero1 = n.rfind('0')
	zero2 = n[:zero1].rfind('0')
	(count, s) = mover(n, zero1, zero2)
	temp = check(s)
	if temp != -1:
		res = min(res, count + temp)
	elif len(n) == 2:
		res = count
if res == float('inf'):
	print(-1)
else:
	print(res)


n = input()[::-1]
c0 = n.count('0')
c2 = n.count('2')
c5 = n.count('5')
c7 = n.count('7')
s = 1000000000.0
if c0 > 1:
	i = n.find('0')
	j = n.find('0', i + 1)
	s = min(s, i + j - 1)
if c2 and c5:
	i = n.find('2')
	j = n.find('5')
	r = j + i - 1 + (j > i)
	(i, j) = sorted([i, j])
	m = n[:i] + n[i + 1:j] + n[j + 1:]
	if not m:
		s = min(s, r)
	elif int(m) == 0:
		pass
	else:
		d = -1
		while m[d] == '0':
			d -= 1
		s = min(s, r - d - 1)
if c5 and c0:
	i = n.find('5')
	j = n.find('0')
	s = min(s, j + i - 1 + (j > i))
if c7 and c5:
	i = n.find('7')
	j = n.find('5')
	r = j + i - 1 + (j > i)
	(i, j) = sorted([i, j])
	m = n[:i] + n[i + 1:j] + n[j + 1:]
	if not m:
		s = min(s, r)
	elif int(m) == 0:
		pass
	else:
		d = -1
		while m[d] == '0':
			d -= 1
		s = min(s, r - d - 1)
print(-1 if s == 1000000000.0 else s)


s = input()
arr = []
for i in s:
	arr.append(i)
ans = 100000000000
for i in range(len(s)):
	for j in range(len(s)):
		if i == j:
			continue
		t = arr[:]
		curr = 0
		for k in range(i, len(s) - 1):
			tt = t[k]
			t[k] = t[k + 1]
			t[k + 1] = tt
			curr += 1
		tey = 0
		if j > i:
			tey += 1
		for k in range(j - tey, len(s) - 2, 1):
			tt = t[k]
			t[k] = t[k + 1]
			t[k + 1] = tt
			curr += 1
		pos = -1
		for k in range(len(s)):
			if t[k] != '0':
				pos = k
				break
		for k in range(pos, 0, -1):
			tt = t[k]
			t[k] = t[k - 1]
			t[k - 1] = tt
			curr += 1
		if int(''.join(t)) % 25 == 0:
			ans = min(ans, curr)
if ans == 100000000000:
	ans = -1
print(ans)


inf = float('inf')

def find_right(s, c, j=-1):
	for i in range(len(s) - 1, -1, -1):
		if i != j and s[i] == c:
			return i
	return inf

def swaps(s, c1, c2):
	if len(s) > 1 and s[-2:] == c1 + c2:
		return 0
	if len(s) > 1 and s[-2:] == c2 + c1:
		return 1
	i2 = find_right(s, c2)
	if i2 == inf:
		return inf
	i1 = find_right(s, c1, i2)
	if i1 == inf:
		return inf
	ans = 0
	if i2 < i1:
		ans += abs(len(s) - 2 - (i1 - 1)) + abs(len(s) - 1 - i2)
	else:
		ans += abs(len(s) - 2 - i1) + abs(len(s) - 1 - i2)
	s0 = ''.join([si for (i, si) in enumerate(s) if i != i1 and i != i2])
	for i in range(len(s0)):
		if s0[i] != '0':
			ans += i
			break
	else:
		return inf
	return ans
n = input().strip()
ans = min(swaps(n, '0', '0'), swaps(n, '2', '5'), swaps(n, '5', '0'), swaps(n, '7', '5'))
print(ans if ans != inf else -1)


INF = 10000
n = input()
ans = INF
k = len(n)
for i in range(k):
	for j in range(k):
		if i == j:
			continue
		s = n
		tmp = 0
		for p in range(i, k - 1):
			s = s[:p] + s[p + 1] + s[p] + s[p + 2:]
			tmp += 1
		for p in range(j - (j > i), k - 2):
			s = s[:p] + s[p + 1] + s[p] + s[p + 2:]
			tmp += 1
		pos = -1
		for p in range(k):
			if s[p] != '0':
				pos = p
				break
		for p in range(pos, 0, -1):
			s = s[:p - 1] + s[p] + s[p - 1] + s[p + 1:]
			tmp += 1
		if int(s) % 25 == 0:
			ans = min(ans, tmp)
print(ans if ans != INF else -1)


def solve1():
	n = m
	try:
		x = n.index('0')
		n = n[x] + n[:x] + n[x + 1:]
		x += len(n) - len(n.rstrip('0'))
		y = n.index('5')
		return x + y - 1
	except:
		return float('inf')

def solve2():
	n = m
	try:
		x = n.index('0')
		y = n.find('0', x + 1)
		if y == -1:
			raise Exception
		return x + y - 1
	except:
		return float('inf')

def solve3():
	n = m
	try:
		x = n.index('5')
		n = n[x] + n[:x] + n[x + 1:]
		x += len(n) - len(n.rstrip('0'))
		y = n.index('2')
		return x + y - 1
	except:
		return float('inf')

def solve4():
	n = m
	try:
		x = n.index('5')
		n = n[x] + n[:x] + n[x + 1:]
		x += len(n) - len(n.rstrip('0'))
		y = n.index('7')
		return x + y - 1
	except:
		return float('inf')
m = input()[::-1]
x = min(solve1(), solve2(), solve3(), solve4())
if x == float('inf'):
	print(-1)
else:
	print(x)


A_str = input()
A = list(A_str)
B = [['2', '5'], ['5', '0'], ['7', '5']]
result = 10 ** 9
if int(A_str) % 100 == 0:
	print(0)
else:
	for (a, b) in B:
		if a not in A or b not in A:
			continue
		cresult = 0
		tmp_str = A_str[:]
		i = tmp_str.rfind(b)
		cresult += len(tmp_str) - 1 - i
		tmp_str = ''.join(A[:i] + A[i + 1:] + [A[i]])
		i = tmp_str.rfind(a)
		cresult += len(tmp_str) - 2 - i
		tmp_str = list(tmp_str)
		tmp_str = ''.join(tmp_str[:i] + tmp_str[i + 1:-1] + [tmp_str[i]] + [tmp_str[-1]])
		for i in range(len(tmp_str)):
			if tmp_str[i] != '0':
				break
		result = min([result, cresult + i])
	if A.count('0') > 1:
		cresult = 0
		i = A_str.rfind('0')
		cresult += len(A_str) - 1 - i
		A_str = A_str[:i] + A_str[i + 1:] + A[i]
		i = A_str[:-1].rfind('0')
		cresult += len(A_str) - 2 - i
		result = min([result, cresult])
	if result < 10 ** 9:
		print(result)
	else:
		print(-1)


def go(n, c1, c2):
	l = len(n)
	pos2 = n.rfind(c2)
	pos1 = n.rfind(c1)
	if c1 == c2:
		pos1 = n[:pos2].rfind(c1)
	if pos1 == -1 or pos2 == -1:
		return 10 ** 10
	nxt = 0
	if min(pos1, pos2) == 0:
		nxt = 1
		if max(pos1, pos2) == 1:
			nxt = 2
	ans = 0
	if nxt != l:
		while nxt < l and n[nxt] == '0':
			ans += 1
			nxt += 1
		if nxt == l:
			return 10 ** 10
	ans += l - pos2 - 1
	if pos1 > pos2:
		pos1 -= 1
	ans += l - pos1 - 2
	return ans
n = input()
ans = min([go(n, '2', '5'), go(n, '0', '0'), go(n, '7', '5'), go(n, '5', '0')])
if ans == 10 ** 10:
	print(-1)
else:
	print(ans)


a = input()[::-1]
d = ['52', '05', '57']
if len(a) < 2:
	print(-1)
elif a[:2] in d:
	print(0)
else:
	ans = 99999999
	if len([i for i in a if i == '0']) >= 2:
		(x, y) = (-1, -1)
		for i in range(len(a)):
			if a[i] == '0':
				if x < 0:
					x = i
				elif y < 0:
					y = i
				else:
					break
		ans = x + y - 1
	for p in d:
		(x, y) = (-1, -1)
		for i in range(len(a)):
			if a[i] == p[0] and x < 0:
				x = i
			if a[i] == p[1] and y < 0:
				y = i
		if x < 0 or y < 0:
			continue
		c = 0
		if x > y:
			c += 1
			(x, y) = (y, x)
		if y == len(a) - 1 and a[-2] == '0' and (x != len(a) - 2):
			z = -1
			for i in range(len(a) - 1):
				if a[i] != '0' and i != x:
					z = i
			if z > 0:
				c += len(a) - 2 - z
		c += x + y - 1
		ans = min(ans, c)
	if ans == 99999999:
		ans = -1
	print(ans)


n = list(input())
ends = ['00', '25', '50', '75']
res = -1

def can_be_result(indeces):
	if -1 in indeces:
		return False
	t = [x for (i, x) in enumerate(n) if i not in indeces]
	if len(t) == 0:
		return True
	if len([x for x in t if x != '0']) == 0:
		return False
	return True
for end in ends:
	indeces = [-1 for _ in end]
	for i in reversed(range(len(n))):
		for j in reversed(range(len(end))):
			if indeces[j] == -1 and n[i] == end[j]:
				indeces[j] = i
				break
	if not can_be_result(indeces):
		continue
	temp = [x for x in n]
	preres = 0
	while len([0 for (i, x) in enumerate(reversed(end)) if temp[-(i + 1)] == x]) != len(end):
		for i in reversed(range(len(indeces))):
			if indeces[i] != len(n) - len(end) + i:
				left = indeces[i]
				right = left + 1
				if left == 0:
					while temp[right] == '0':
						left += 1
						right += 1
				(temp[left], temp[right]) = (temp[right], temp[left])
				indeces = [x + 1 if x == left else x - 1 if x == right else x for (i, x) in enumerate(indeces)]
				preres += 1
				break
	if res == -1 or preres < res:
		res = preres
print(res)


def g(qw, a, i=0):
	return a.find(str(qw), i) + 1 or 50

def main():
	a = input()[::-1]
	s = len(a)
	(z, r) = (g(0, a), g(5, a))
	x = min(g(0, a, z), r)
	q = z + x + (x < z)
	x = min(g(2, a), g(7, a))
	c = r + x + (x < r)
	if r == s:
		while a[r - 2] == '0':
			r -= 1
		c += s - r
	q = min(q, c)
	print((-1, q - 3)[q < 40])
main()


n = list(input())[::-1]
INF = 10 ** 9
if ''.join(n) in ['52', '05', '57']:
	print(0)
	exit(0)
if ''.join(n) in ['25', '75']:
	print(1)
	exit(0)

def solve(a, b):
	if a not in n or b not in n:
		return INF
	nn = n[:]
	ia = nn.index(a)
	nn = [a] + nn[:ia] + nn[ia + 1:]
	ib = nn.index(b)
	nn = [a, b] + nn[1:ib] + nn[ib + 1:]
	cnt = 0
	for i in range(len(n) - 1, 1, -1):
		if nn[i] != '0':
			break
		cnt += 1
	else:
		return INF
	return ia + ib + cnt - 1
if n.count('0') >= 2:
	ans = -1
	one = False
	for i in range(len(n)):
		if n[i] == '0':
			ans += i
			if one:
				break
			one = True
else:
	ans = INF
ans = min(ans, solve('5', '2'))
ans = min(ans, solve('0', '5'))
ans = min(ans, solve('5', '7'))
if ans == INF:
	print(-1)
else:
	print(ans)


import sys

def sol(a, s):
	s1 = s[::-1]
	try:
		ai = s1.index(a)
		if a == '0':
			s2 = a + s1[0:ai] + s1[ai + 1:]
			return [True, ai, s2[::-1]]
	except:
		return [False]
	x = len(s) - 2
	while x > -1 and s1[x] == '0' and (ai == len(s) - 1) and (len(s) != 1):
		x -= 1
	s = a + s1[0:ai] + s1[ai + 1:]
	if x == -1 and len(s) != 1:
		return [False]
	if s1[x + 1] == '0':
		s = s[0:x + 1] + s[x + 2:] + s1[x]
	return [True, ai + len(s) - 2 - x, s[::-1]]
s = input()
ans = 10 ** 6
x = sol('0', s)
if x[0]:
	y = sol('0', x[2][0:-1])
	if y[0] and int(y[2] + '0') % 25 == 0:
		ans = min(x[1] + y[1], ans)
x = sol('0', s)
if x[0]:
	y = sol('5', x[2][0:-1])
	if y[0] and int(y[2] + '0') % 25 == 0:
		ans = min(x[1] + y[1], ans)
x = sol('5', s)
if x[0]:
	y = sol('7', x[2][0:-1])
	if y[0] and int(y[2] + '5') % 25 == 0:
		ans = min(x[1] + y[1], ans)
x = sol('5', s)
if x[0]:
	y = sol('2', x[2][0:-1])
	if y[0] and int(y[2] + '5') % 25 == 0:
		ans = min(x[1] + y[1], ans)
if s == '0':
	print(s)
elif ans == 10 ** 6:
	print(-1)
else:
	print(ans)


from itertools import dropwhile
n = [int(i) for i in input()]
l = len(n)
if len(n) == 1:
	print(-1)
	exit()
if n in [[2, 5], [7, 5], [5, 0]]:
	print(0)
	exit()
if n in [[5, 2], [5, 7]]:
	print(1)
	exit()
inf = 10 ** 12

def calc1(x, y):
	global n, l, inf
	m = n[::-1]
	if x not in n or y not in n:
		return inf
	(ix, iy) = (m.index(x), m.index(y))
	if max(ix, iy) < l - 1:
		if iy < ix:
			return iy + (ix - 1)
		else:
			return iy + (ix - 1) + 1
	else:
		ret = 0
		if ix == l - 1:
			for i in range(l - 2, -1, -1):
				if m[i] != 0 and i != iy:
					break
			else:
				return inf
			if i < iy:
				iy -= 1
				ret += l - 1 - i
				ix -= 1
				ret += iy + (ix - 1)
				return ret
			else:
				ret += iy
				ret += l - 1 - i
				ix -= 1
				ret += ix - 1
				return ret
		else:
			for i in range(l - 2, -1, -1):
				if m[i] != 0 and i != ix:
					break
			else:
				return inf
			if i < ix:
				iy -= 1
				ret += l - 1 - i
				ix -= 1
				ret += iy + (ix - 1) + 1
				return ret
			else:
				ret += iy
				ret += l - 1 - i
				ix -= 1
				ret += ix - 1 + 1
				return ret

def calc2(x, y):
	global n, l, inf
	if n.count(0) < 2:
		return inf
	m = [l - 1 - i for i in range(l) if n[i] == 0][::-1]
	return m[0] + m[1] - 1
ans = min([calc1(2, 5), calc1(7, 5), calc1(5, 0), calc2(0, 0)])
print(-1 if ans == inf else ans)


n = int(input())
l = list(str(n))[::-1]
g = []

def verif(l):
	if l[-1] != '0':
		return 0
	else:
		return 1 + verif(l[:-1])

def permut(l, c, c1):
	i = l.copy()
	k = 0
	while True:
		if i.index(c) == 0:
			break
		else:
			j = i.index(c)
			(i[j], i[j - 1]) = (i[j - 1], i[j])
			k += 1
	m = i[1:]
	while True:
		if m.index(c1) == 0:
			break
		else:
			j = m.index(c1)
			(m[j], m[j - 1]) = (m[j - 1], m[j])
			k += 1
	k += verif(i)
	return k
if '2' in l and '5' in l:
	g.append(permut(l, '5', '2'))
if '5' in l and '0' in l:
	g.append(permut(l, '0', '5'))
if '7' in l and '5' in l:
	g.append(permut(l, '5', '7'))
if l.count('0') > 1:
	g.append(permut(l, '0', '0'))
if g == []:
	print(-1)
else:
	print(min(g))