It is given a positive integer $n$. In $1$ move, one can select any single digit and remove it (i.e. one selects some position in the number and removes the digit located at this position). The operation cannot be performed if only one digit remains. If the resulting number contains leading zeroes, they are automatically removed.

E.g. if one removes from the number $32925$ the $3$-rd digit, the resulting number will be $3225$. If one removes from the number $20099050$ the first digit, the resulting number will be $99050$ (the $2$ zeroes going next to the first digit are automatically removed).

What is the minimum number of steps to get a number such that it is divisible by $25$ and positive? It is guaranteed that, for each $n$ occurring in the input, the answer exists. It is guaranteed that the number $n$ has no leading zeros.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.

Each test case consists of one line containing one integer $n$ ($25 \le n \le 10^{18}$). It is guaranteed that, for each $n$ occurring in the input, the answer exists. It is guaranteed that the number $n$ has no leading zeros.


-----Output-----

For each test case output on a separate line an integer $k$ ($k \ge 0$) — the minimum number of steps to get a number such that it is divisible by $25$ and positive.


-----Examples-----

Input
5
100
71345
3259
50555
2050047
Output
0
3
1
3
2


-----Note-----

In the first test case, it is already given a number divisible by $25$.

In the second test case, we can remove the digits $1$, $3$, and $4$ to get the number $75$.

In the third test case, it's enough to remove the last digit to get the number $325$.

In the fourth test case, we can remove the three last digits to get the number $50$.

In the fifth test case, it's enough to remove the digits $4$ and $7$.

Difficulty: EASY
Tags: Dynamic programming

Graph traversal

Mathematics

Greedy algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
import operator

def main():

	def solve(s):
		mn = 20
		n = len(s)
		bn = n - 2
		foundFive = False
		foundZero = False
		for i in range(n - 1, -1, -1):
			if s[i] == '0':
				if foundZero:
					mn = bn - i
					break
				foundZero = True
			elif s[i] == '5':
				if foundZero:
					mn = bn - i
					break
				foundFive = True
			elif s[i] in ['2', '7']:
				if foundFive:
					mn = bn - i
					break
		return str(mn)
	t = int(input())
	for _ in range(t):
		s = input().strip()
		print(solve(s))
import sys, threading
input = sys.stdin.readline
thread = threading.Thread(target=main)
thread.start()
thread.join()


def divisbleBy25(n):
	count = {}
	ans = 0
	for i in range(len(n) - 1, -1, -1):
		if n[i] not in ('7', '2', '0', '5'):
			continue
		if n[i] == '0' or n[i] == '5':
			if '0' in count:
				ans += count['0'] - i - 1
				ans += len(n) - 1 - count['0']
				break
			count[n[i]] = i
		elif n[i] == '7' or n[i] == '2':
			if '5' in count:
				ans += count['5'] - i - 1
				ans += len(n) - 1 - count['5']
				break
	return ans
output = []
for i in range(int(input())):
	output.append(divisbleBy25(input()))
print(*output, sep='\n')


for s in [*open(0)][1:]:
	a = []
	i = 0
	while (x := s[(i := (i - 1))]) not in a:
		a += '05' * (x == '0') + '27' * (x == '5')
	print(-i - 3)


SEARCH_FIRST_DIGIT = 0
COMPLETE_50 = 1
COMPLETE_25 = 2

def solve(n):
	n = n[::-1]
	f0 = -1
	f5 = -1
	for i in range(len(n)):
		if f0 == -1 and n[i] == '0':
			f0 = i
			continue
		if f5 == -1 and n[i] == '5':
			f5 = i
			if f0 == -1:
				continue
		if f0 != -1 and n[i] in '05':
			return i - 1
		if f5 != -1 and n[i] in '27':
			return i - 1

def main():
	test_cases = int(input())
	ans = [None] * test_cases
	for t in range(test_cases):
		n = input()
		a = solve(n)
		ans[t] = a
	print(*ans, sep='\n')
main()


import sys, math
input = sys.stdin.readline
for _ in range(int(input())):
	s = input()
	z = None
	f = None
	for i in range(len(s) - 2, -1, -1):
		if s[i] == '5' and f == None:
			f = i
		if z == None and s[i] == '0':
			z = i
		elif z and (s[i] == '5' or s[i] == '0'):
			print(len(s) - 3 - i)
			break
		if f and (s[i] == '2' or s[i] == '7'):
			print(len(s) - 3 - i)
			break


from sys import stdin

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

def main():
	Q = int(_stdin())
	for i in range(0, Q):
		N = int(_stdin())
		if N % 25 == 0:
			print(0)
			continue
		ok = False
		S = str(N)
		b = len(S)
		total = 0
		for j in range(b - 1, -1, -1):
			if S[j] == '5':
				for k in range(j - 1, -1, -1):
					if S[k] == '2' or S[k] == '7':
						ok = True
						break
					total += 1
				break
			total += 1
		total1 = 0
		for j in range(b - 1, -1, -1):
			if S[j] == '0':
				for k in range(j - 1, -1, -1):
					if S[k] == '0' or S[k] == '5':
						break
					total1 += 1
				break
			total1 += 1
		print(min(total, total1))
main()


t = int(input())
D = {}
for i in range(t):
	s = input()
	d = {0: 0, 5: 0}
	c = 0
	for j in s[-1::-1]:
		if j == '0':
			if d[0] > 0:
				D[i] = c - 1
				break
			else:
				d[0] += 1
		elif j == '2':
			if d[5] > 0:
				D[i] = c - 1
				break
		elif j == '5':
			if d[0] > 0:
				D[i] = c - 1
				break
			else:
				d[5] += 1
		elif j == '7':
			if d[5] > 0:
				D[i] = c - 1
				break
		c += 1
for i in D:
	print(D[i])


import sys
input = sys.stdin.readline
INF = 10 ** 9

def solve():
	s = input()[::-1]
	ans = INF
	for (x, y) in ['00', '25', '50', '75']:
		try:
			i = s.index(y)
			j = s.index(x, i + 1)
			ans = min(ans, i - 1 + (j - i - 1))
		except ValueError:
			pass
	print(ans)
for _ in range(int(input())):
	solve()


cant = int(input())
lista = []
for i in range(cant):
	lista.append(int(input()))
for n in lista:
	n = str(n)[::-1]
	cont0 = 0
	cont5 = 0
	c0 = 0
	c5 = 0
	for i in n:
		if i == '0' and cont0 != 1:
			cont0 = 1
			c5 = c5 + 1
			continue
		elif cont0 == 1 and i == '0':
			print(c0)
			break
		elif cont0 == 1 and i == '5':
			print(c0)
			break
		elif i == '5' and cont5 != 1:
			cont5 = 1
			c0 = c0 + 1
			continue
		elif cont5 == 1 and i == '2':
			print(c5)
			break
		elif cont5 == 1 and i == '7':
			print(c5)
			break
		else:
			c5 = c5 + 1
			c0 = c0 + 1


result = []
for _ in range(int(input())):
	s = input()
	cnt = 0
	while s[-1] not in '05':
		s = s[:-1]
		cnt += 1
	cnt5 = cnt
	cnt0 = cnt
	flag0 = 0
	flag5 = 0
	for i in s[::-1]:
		if flag5 == 1:
			if i not in '27':
				cnt5 += 1
			else:
				break
		else:
			cnt5 += 1
		if flag0 == 1:
			if i not in '50':
				cnt0 += 1
			else:
				break
		else:
			cnt0 += 1
		if i == '5' and flag5 == 0:
			flag5 = 1
			cnt5 -= 1
		if i == '0' and flag0 == 0:
			flag0 = 1
			cnt0 -= 1
	result.append(min(cnt5, cnt0))
print(*result, sep='\n')


li = []
for i in range(int(input())):
	li.append(int(input()))
for n in li:
	n = str(n)[::-1]
	cont_0 = 0
	cont_5 = 0
	c0 = 0
	c5 = 0
	for i in n:
		if i == '0' and cont_0 != 1:
			cont_0 = 1
			c5 += 1
			continue
		elif cont_0 == 1 and i == '0':
			print(c0)
			break
		elif cont_0 == 1 and i == '5':
			print(c0)
			break
		elif i == '5' and cont_5 != 1:
			cont_5 = 1
			c0 += 1
			continue
		elif cont_5 == 1 and i == '2':
			print(c5)
			break
		elif cont_5 == 1 and i == '7':
			print(c5)
			break
		else:
			c5 = c5 + 1
			c0 = c0 + 1


n = []
for i in range(int(input())):
	n.append(str(input()))
for j in n:
	s = 100
	s_t = 100
	k = j[::-1]
	j = k
	if j.find('0') != -1:
		s = 0
		s += j.index('0')
		j = j[j.index('0') + 1:]
		va = j.find('0')
		vb = j.find('5')
		if va == -1 and vb == -1:
			s = 100
		else:
			if va != -1:
				va = j.index('0')
			else:
				va = 100
			if vb != -1:
				vb = j.index('5')
			else:
				vb = 100
			if va < vb and va != -1:
				s += va
			else:
				s += vb
	if k.find('5') != -1:
		s_t = 0
		s_t += k.index('5')
		k = k[k.index('5') + 1:]
		vx = k.find('2')
		vy = k.find('7')
		if vx == -1 and vy == -1:
			s_t = 100
		else:
			if vx != -1:
				vx = k.index('2')
			else:
				vx = 100
			if vy != -1:
				vy = k.index('7')
			else:
				vy = 100
			if vx < vy and vx != -1:
				s_t += vx
			else:
				s_t += vy
	print(min(s, s_t))


def fn(n):
	m = n[::-1]
	zeroes = []
	fives = []
	twos = []
	for i in range(len(m)):
		if m[i] == '0':
			zeroes.append(i)
		elif m[i] == '5':
			fives.append(i)
		elif m[i] == '2' or m[i] == '7':
			twos.append(i)
	a = len(m)
	if len(zeroes) > 1:
		a = zeroes[1] - 1
	b = c = len(m)
	if len(fives) != 0 and len(zeroes) != 0:
		for oppa in fives:
			if oppa > zeroes[0]:
				c = oppa - 1
				break
	if len(fives) != 0 and len(twos) != 0:
		for opp in twos:
			if opp > fives[0]:
				b = opp - 1
				break
	return min(a, b, c)
a = int(input())
k = []
for i in range(a):
	q = input()
	k.append(fn(q))
for m in k:
	print(m)


nums = int(input())
arr = []
for i in range(nums):
	arr.append(int(input()))
for n in arr:
	n = str(n)[::-1]
	(a, b) = (0, 0)
	(a2, b2) = (0, 0)
	for i in n:
		if i == '0' and a != 1:
			a = 1
			b2 = b2 + 1
			continue
		elif a == 1 and i == '0':
			print(a2)
			break
		elif a == 1 and i == '5':
			print(a2)
			break
		elif i == '5' and b != 1:
			b = 1
			a2 = a2 + 1
			continue
		elif b == 1 and i == '2':
			print(b2)
			break
		elif b == 1 and i == '7':
			print(b2)
			break
		else:
			b2 = b2 + 1
			a2 = a2 + 1


for s in [*open(0)][1:]:
	a = []
	n = i = len(s)
	while i and (x := s[(i := (i - 1))]) not in a:
		a += '05' * (x == '0') + '27' * (x == '5')
	print(n - i - 3)


n = int(input())
for i in range(n):
	a = list(input())
	k1 = 0
	k2 = 0
	s = 0
	for j in a[::-1]:
		if j == '0':
			k2 += 1
		if j == '5' and k1 != 1:
			k1 = 1
		if (j == '2' or j == '7') and k1 == 1:
			k1 = 2
		if j == '5' and k2 == 1:
			k2 += 1
		s += 1
		if k1 == 2 or k2 == 2:
			print(s - 2)
			break


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	k = int(input())
	t = k
	cnt0 = 0
	while t:
		if t % 10 != 0:
			cnt0 += 1
		else:
			t //= 10
			break
		t //= 10
	while t:
		if t % 5 != 0:
			cnt0 += 1
		else:
			break
		t //= 10
	t = k
	cnt1 = 0
	while t:
		if t % 10 != 5:
			cnt1 += 1
		else:
			t //= 10
			break
		t //= 10
	while t:
		if t % 5 != 2:
			cnt1 += 1
		else:
			break
		t //= 10
	print(min(cnt0, cnt1))


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp_vars_int = lambda : map(int, input().split())
inp_list_int = lambda : list(map(int, input().split()))
t = int(input())
for _ in range(t):
	n = input().strip()
	(found_x5, found_5) = (False, False)
	(r1, r2) = (0, 0)
	for (i, c) in enumerate(reversed(n)):
		if not found_5:
			if c == '5':
				found_5 = True
			else:
				r1 += 1
		elif c in ('2', '7'):
			found_x5 = True
			break
		else:
			r1 += 1
	(found_x0, found_0) = (False, False)
	for (i, c) in enumerate(reversed(n)):
		if not found_0:
			if c == '0':
				found_0 = True
			else:
				r2 += 1
		elif c in ('5', '0'):
			found_x0 = True
			break
		else:
			r2 += 1
	print(min(r1, r2))


t = int(input())
A = []
for i in range(t):
	n = input()
	if int(n) % 25 == 0:
		A.append(0)
		continue
	n = n[::-1]
	ans = [-1, -1, -1, -1, -1]
	ret = 100
	for i in range(len(n)):
		if n[i] == '0':
			if ans[0] != -1:
				ret = min(ret, i - 1)
			ans[0] = i
		if n[i] == '2':
			ans[1] = i
			if ans[2] != -1:
				ret = min(ret, i - 1)
		if n[i] == '5':
			ans[2] = i
			if ans[0] != -1:
				ret = min(ret, i - 1)
		if n[i] == '7':
			ans[3] = i
			if ans[2] != -1:
				ret = min(ret, i - 1)
	A.append(ret)
for i in A:
	print(i)


import sys

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

def search(value1, value2, string):
	for s in range(len(string) - 1, -1, -1):
		if string[s] == value1:
			return s
		if string[s] == value2:
			return s
	return -1

def main():
	n = int(sys.stdin.readline().strip())
	numbers = []
	for i in range(n):
		numbers.append(get_string())
	ans = []
	for num in numbers:
		l = len(num)
		first = second = l
		for d in range(l - 1, -1, -1):
			if num[d] == '0':
				x = search('0', '5', num[:d])
				if x != -1 and first == l:
					first = l - 1 - x - 1
			if num[d] == '5':
				x = search('2', '7', num[:d])
				if x != -1 and second == l:
					second = l - 1 - x - 1
		if first < second:
			ans.append(first)
		else:
			ans.append(second)
	for a in ans:
		print(a)
main()


import math as m
i = input()
inp = []
for a in range(0, int(i)):
	inp += [input()]

def min_moves(s, sub):
	s = s[::-1]
	sub = sub[::-1]
	count = 0
	for c in sub:
		try:
			n = s.index(c)
			count += n
			s = s[n + 1:]
		except:
			return -1
	return count
for s in inp:
	check = ['00', '25', '50', '75']
	moves = []
	for sub in check:
		x = min_moves(s, sub)
		if x >= 0:
			moves += [x]
	print(min(moves))


import math
import sys
import itertools
import fractions
wtf = sys.stdin.read()
wtf = wtf.strip().split('\n')
t = int(wtf[0])
for s in wtf[1:]:
	n = len(s)
	ans = [0, 0]
	z = ''.join([x for x in reversed(s)])
	fid = z.find('5')
	ans[0] = fid
	if fid != -1:
		for i in range(fid + 1, n):
			if z[i] != '2' and z[i] != '7':
				ans[0] += 1
			else:
				break
	else:
		ans[0] = n
	zid = z.find('0')
	ans[1] = zid
	if zid != -1:
		for i in range(zid + 1, n):
			if z[i] != '0' and z[i] != '5':
				ans[1] += 1
			else:
				break
	else:
		ans[1] = n
	print(min(ans))


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI1(rows_number):
	return [LI1() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline().rstrip()
inf = 10 ** 16
md = 998244353

def solve():
	cc = list(SI())[::-1]
	ans = inf
	for s in ['00', '52', '05', '57']:
		j = 0
		for (i, c) in enumerate(cc):
			if c == s[j]:
				j += 1
			if j == 2:
				ans = min(ans, i - 1)
				break
	print(ans)
for testcase in range(II()):
	solve()


for s in [*open(0)][1:]:
	n = int(s)
	a1 = 0
	a2 = 0
	l1 = [[0], [5, 0]]
	l2 = [[5], [2, 7]]
	i1 = 0
	i2 = 0
	while n > 0:
		x = n % 10
		if x in l1[i1]:
			i1 += 1
			a1 -= 1
		elif x in l2[i2]:
			i2 += 1
			a2 -= 1
		a1 += 1
		a2 += 1
		if 2 in [i1, i2]:
			break
		n //= 10
	print(min(a1, a2))


t = int(input())
for dlsakjdklsj in range(t):
	n = input()[::-1]
	ans = 999
	pos1 = n.find('0')
	if pos1 != -1:
		pos2 = n.find('0', pos1 + 1)
		if pos2 != -1:
			ans = min(ans, pos2 - 1)
	pos1 = n.find('5')
	if pos1 != -1:
		pos2 = n.find('2', pos1 + 1)
		if pos2 != -1:
			ans = min(ans, pos2 - 1)
	pos1 = n.find('5')
	if pos1 != -1:
		pos2 = n.find('7', pos1 + 1)
		if pos2 != -1:
			ans = min(ans, pos2 - 1)
	pos1 = n.find('0')
	if pos1 != -1:
		pos2 = n.find('5', pos1 + 1)
		if pos2 != -1:
			ans = min(ans, pos2 - 1)
	print(ans)


import sys
from math import factorial, gcd
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest, nsmallest
from itertools import groupby, accumulate
from copy import deepcopy
MOD = 10 ** 9 + 7
INF = float('inf')
input = sys.stdin.readline
rl = lambda : list(map(int, input().split()))
rs = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	count = 0
	zero = 0
	for c in reversed(str(n)):
		if c == '0':
			zero += 1
			if zero == 2:
				break
		elif c == '5':
			if zero > 0:
				break
			else:
				count += 1
		else:
			count += 1
	five = 0
	count2 = 0
	for c in reversed(str(n)):
		if c == '5':
			five += 1
			if five > 1:
				count2 += 1
		elif c == '2' or c == '7':
			if five > 0:
				break
			else:
				count2 += 1
		else:
			count2 += 1
	print(min(count, count2))


t = int(input())
answers = []
for i in range(t):
	n = int(input())
	if n % 25 == 0:
		answers.append(0)
		continue
	try:
		s = str(n)
		a1 = 0
		while s[-1] != '5':
			s = s[:-1]
			a1 += 1
		while s[-2] not in ('2', '7'):
			s = s[:-2] + s[-1]
			a1 += 1
	except:
		pass
	try:
		s = str(n)
		a2 = 0
		while s[-1] != '0':
			s = s[:-1]
			a2 += 1
		while s[-2] not in ('0', '5'):
			s = s[:-2] + s[-1]
			a2 += 1
	except:
		pass
	if a1 > a2:
		answers.append(a2)
	else:
		answers.append(a1)
for i in answers:
	print(i)


for _ in range(int(input())):
	s = input().strip()[::-1]
	ans = 1000000000000
	z = s.find('0')
	if z != -1:
		tmp = s.find('0', z + 1)
		if tmp != -1:
			ans = min(ans, tmp - z - 1 + z)
		temp = s.find('5', z + 1)
		if temp != -1:
			ans = min(ans, temp - z - 1 + z)
	f = s.find('5')
	if f != -1:
		tmp = s.find('2', f + 1)
		if tmp != -1:
			ans = min(ans, tmp - f - 1 + f)
		tmp = s.find('7', f + 1)
		if tmp != -1:
			ans = min(ans, tmp - f - 1 + f)
	print(ans)


for i in range(int(input())):
	n = input()
	r = n[::-1]
	l = len(r)
	a = b = c = d = l
	if '5' in r:
		m = r.find('5')
		n = r.find('2', m, l)
		u = r.find('7', m, l)
		if n != -1:
			a = n - 1
		if u != -1:
			b = u - 1
	if '0' in r:
		m = r.find('0')
		n = r.find('5', m, l)
		u = r.find('0', m + 1, l)
		if n != -1:
			c = n - 1
		if u != -1:
			d = u - 1
	k = min(a, b, c, d)
	print(k)


def test25(string):
	s = len(string) - 1
	while not string[s] == '5':
		s -= 1
		if s < 0:
			return 20
	while not string[s] == '2':
		s -= 1
		if s < 0:
			return 20
	return len(string) - s - 2

def test50(string):
	s = len(string) - 1
	while not string[s] == '0':
		s -= 1
		if s < 0:
			return 20
	while not string[s] == '5':
		s -= 1
		if s < 0:
			return 20
	return len(string) - s - 2

def test75(string):
	s = len(string) - 1
	while not string[s] == '5':
		s -= 1
		if s < 0:
			return 20
	while not string[s] == '7':
		s -= 1
		if s < 0:
			return 20
	return len(string) - s - 2

def test00(string):
	s = len(string) - 1
	while not string[s] == '0':
		s -= 1
		if s < 0:
			return 20
	s -= 1
	while not string[s] == '0':
		s -= 1
		if s < 0:
			return 20
	return len(string) - s - 2
t = int(input())
arr_input = []
for i in range(t):
	v = input()
	arr_input.append(v)
for i in arr_input:
	if int(i) % 25 == 0:
		print(0)
		continue
	m = min(test25(i), test50(i), test75(i), test00(i))
	print(m)


t = int(input())
for _ in range(t):
	n = str(input())
	f5 = n.rfind('5')
	f0 = n.rfind('0')
	s0 = n[:f0].rfind('0')
	s7 = n[:f5].rfind('7')
	s5 = n[:f0].rfind('5')
	s2 = n[:f5].rfind('2')
	ans00 = len(n)
	ans25 = len(n)
	ans50 = len(n)
	ans75 = len(n)
	if f0 != -1 and s0 != -1:
		ans00 = len(n) - f0 - 1 + f0 - s0 - 1
	if f5 != -1 and s2 != -1:
		ans25 = len(n) - s2 - 2
	if f0 != -1 and s5 != -1:
		ans50 = len(n) - s5 - 2
	if f5 != -1 and s7 != -1:
		ans75 = len(n) - s7 - 2
	print(min(ans00, ans25, ans50, ans75))


t = int(input())
import math
for i in range(t):
	s = input()
	s = s[::-1]
	th1 = -1
	th2 = -1
	ko = s.find('0')
	if ko != -1:
		ko1 = s[ko + 1:].find('0') + 1
		nam1 = s[ko + 1:].find('5') + 1
		if ko1 != 0 and nam1 != 0:
			th1 = min(ko1, nam1) + ko - 1
		elif ko1 != 0:
			th1 = ko1 + ko - 1
		elif nam1 != 0:
			th1 = nam1 + ko - 1
	nam = s.find('5')
	if nam != -1:
		hai = s[nam + 1:].find('2') + 1
		bay = s[nam + 1:].find('7') + 1
		if hai != 0 and bay != 0:
			th2 = min(hai, bay) + nam - 1
		elif hai != 0:
			th2 = hai + nam - 1
		elif bay != 0:
			th2 = bay + nam - 1
	if th1 != -1 and th2 != -1:
		print(min(th1, th2))
	elif th1 != -1:
		print(th1)
	else:
		print(th2)


import sys
lines = sys.stdin.readlines()
for num in lines[1:]:
	num_int = int(num)
	if num_int % 25 == 0:
		print(0)
		continue
	num = list(reversed(list(num.rstrip())))
	k = []
	l = len(num)
	for idx in range(l):
		if num[idx] == '0':
			for idx2 in range(idx + 1, l):
				if num[idx2] in ['0', '5']:
					s = idx2 - 1
					k.append(s)
					break
		elif num[idx] == '5':
			for idx2 in range(idx + 1, l):
				if num[idx2] in ['2', '7']:
					s = idx2 - 1
					k.append(s)
					break
	print(min(k))


def func():
	num = 0
	line = input()[::-1]
	a = False
	b = False
	for i in range(len(line)):
		if a and (line[i] == '0' or line[i] == '5'):
			print(i - 1)
			break
		if b and (line[i] == '2' or line[i] == '7'):
			print(i - 1)
			break
		if line[i] == '0':
			a = True
		if line[i] == '5':
			b = True
t = int(input())
for i in range(t):
	func()


for _ in range(int(input())):
	n = input()
	ind5 = n.rfind('5')
	ind0 = n.rfind('0')
	min_k = len(n)
	if ind5 != -1:
		ind25 = n.rfind('2', 0, ind5)
		ind75 = n.rfind('7', 0, ind5)
		if ind25 != -1:
			min_k = min(min_k, len(n) - ind25 - 2)
		if ind75 != -1:
			min_k = min(min_k, len(n) - ind75 - 2)
	if ind0 != -1:
		ind00 = n.rfind('0', 0, ind0)
		ind50 = n.rfind('5', 0, ind0)
		if ind00 != -1:
			min_k = min(min_k, len(n) - ind00 - 2)
		if ind50 != -1:
			min_k = min(min_k, len(n) - ind50 - 2)
	print(min_k)


for iteration in range(int(input())):
	five_found = False
	zero_found = False
	number_reversed = input()[::-1]
	length = len(number_reversed)
	for index in range(length):
		if zero_found and (number_reversed[index] == '0' or number_reversed[index] == '5'):
			print(index - 1)
			break
		if number_reversed[index] == '0':
			zero_found = True
		if five_found and (number_reversed[index] == '2' or number_reversed[index] == '7'):
			print(index - 1)
			break
		if number_reversed[index] == '5':
			five_found = True


t = input()
val1 = 99
val2 = 99
val3 = 99
val4 = 99
for i in range(int(t)):
	num = input()
	five = num.rfind('5')
	two = num.rfind('2', 0, five)
	seven = num.rfind('7', 0, five)
	zero = num.rfind('0')
	fiveO = num.rfind('5', 0, zero)
	zeroO = num.rfind('0', 0, zero)
	length = len(num)
	val1 = 99
	val2 = 99
	val3 = 99
	val4 = 99
	if five != -1 and two != -1:
		val1 = length - five + (five - two) - 2
	if five != -1 and seven != -1:
		val2 = length - five + (five - seven) - 2
	if zero != -1 and zeroO != -1:
		val3 = length - zero + (zero - zeroO) - 2
	if fiveO != -1 and zero != -1:
		val4 = length - zero + (zero - fiveO) - 2
	print(min(val1, val2, val3, val4))


def solve(s):
	(zero, five) = (False, False)
	s = s[::-1]
	counter = 0
	for i in s:
		counter += 1
		if (i == '5' or i == '0') and zero or ((i == '2' or i == '7') and five):
			counter -= 1
			break
		if i == '0' and (not zero):
			counter -= 1
			zero = True
			continue
		if i == '5' and (not five):
			counter -= 1
			five = True
			continue
	if zero and five:
		counter += 1
	return counter
for a in range(int(input())):
	print(solve(input()))


def minchar(s):
	a = ''
	i = 0
	while (x := s[(i := (i - 1))]) not in a:
		a += '05' * (x == '0') + '27' * (x == '5')
	print(-i - 2)
t = int(input())
t = t + 1
while (t := (t - 1)):
	minchar(input())


def divisible(q):
	last = q[-2:]
	if last == '75' or last == '25' or last == '50' or (last == '00'):
		return 0
	ans = 0
	l = len(q)
	last = [False, False]
	for i in range(l - 1, -1, -1):
		if q[i] == '0' or q[i] == '5':
			if not last[-1] and q[i] == '0':
				last[-1] = True
				continue
			elif not last[0] and q[i] == '5':
				last[0] = True
				if last[1]:
					ans += l - i - 2
					return ans
				continue
		if last[0] and (q[i] == '2' or q[i] == '7'):
			ans += l - i - 2
			return ans
		if last[-1] and (q[i] == '0' or q[i] == '5'):
			ans += l - i - 2
			return ans
	return ans
t = int(input())
for _ in range(t):
	n = input()
	result = divisible(n)
	print(result)


def f1(a):
	t = 0
	ob = 0
	for i in a[::-1]:
		if i == '5' and ob == 0:
			ob = 1
		elif i in ['2', '7'] and ob == 1:
			break
		else:
			t += 1
	else:
		return 99
	return t

def f2(a):
	t = 0
	ob = 0
	for i in a[::-1]:
		if i == '0' and ob == 0:
			ob = 1
		elif i in ['5', '0'] and ob == 1:
			break
		else:
			t += 1
	else:
		return 99
	return t
c = int(input())
for times in range(c):
	a = input()
	t1 = f1(a)
	t2 = f2(a)
	print(t1 if t1 < t2 else t2)


import sys
n = int(input())
es = []
for _ in range(n):
	es.append(int(input()))
for e in es:
	k = 0
	s = str(e)[::-1]
	pos_7 = -1
	pos_2 = -1
	pos_0 = -1
	pos_5 = -1
	k5 = -1
	k0 = -1
	for c in s:
		if c == '5':
			if k5 == -1 and pos_2 == -1 and (pos_7 == -1):
				try:
					pos_7 = s.index('7', k + 1)
				except:
					pass
				try:
					pos_2 = s.index('2', k + 1)
				except:
					pass
				if pos_7 != -1 or pos_2 != -1:
					k5 = k
		elif c == '0':
			if k0 == -1 and pos_0 == -1 and (pos_5 == -1):
				try:
					pos_0 = s.index('0', k + 1)
				except:
					pass
				try:
					pos_5 = s.index('5', k + 1)
				except:
					pass
				if pos_0 != -1 or pos_5 != -1:
					k0 = k
		k += 1
	steps = -1
	if k0 != -1:
		steps = k0 + min(pos_5 - k0 - 1 if pos_5 != -1 else sys.maxsize, pos_0 - k0 - 1 if pos_0 != -1 else sys.maxsize)
	if k5 != -1:
		steps = min(steps if steps != -1 else sys.maxsize, k5 + min(pos_2 - k5 - 1 if pos_2 != -1 else sys.maxsize, pos_7 - k5 - 1 if pos_7 != -1 else sys.maxsize))
	print(steps if steps != -1 else 0)


t = int(input())
for i in range(t):
	number = input()
	result = 0
	is_five = False
	is_zero = False
	for digit in reversed(number):
		if digit == '2' or digit == '7':
			if is_five:
				break
		if digit == '5':
			is_five = True
			if is_zero:
				break
		if digit == '0':
			if is_zero:
				break
			else:
				is_zero = True
		result += 1
	result -= 1
	print(result)


q = int(input())
for _ in range(q):
	astring = input()
	n = len(astring)
	asum = 0
	adict = {}
	for i in reversed(range(n)):
		if astring[i] not in ['2', '5', '0', '7']:
			continue
		if astring[i] == '0':
			if astring[i] in adict:
				asum += adict[astring[i]] - i - 1
				asum += n - adict['0'] - 1
				break
			else:
				adict[astring[i]] = i
		if astring[i] == '5':
			if '0' in adict:
				asum += adict['0'] - i - 1
				asum += n - adict['0'] - 1
				break
			else:
				adict[astring[i]] = i
		if astring[i] in ['2', '7']:
			if '5' in adict:
				asum += adict['5'] - i - 1
				asum += n - adict['5'] - 1
				break
	print(asum)


for _ in range(int(input())):
	num = input()
	num = num[::-1]
	first5 = num.find('5') if num.find('5') != -1 else 9999999
	first0 = num.find('0') if num.find('0') != -1 else 9999999
	for52 = num.find('2', first5 + 1)
	for57 = num.find('7', first5 + 1)
	for00 = num.find('0', first0 + 1)
	for05 = num.find('5', first0 + 1)
	ans = 99999999
	if first5 != -1:
		if for52 != -1:
			ans = min(ans, for52)
		if for57 != -1:
			ans = min(ans, for57)
	if first0 != -1:
		if for00 != -1:
			ans = min(ans, for00)
		if for05 != -1:
			ans = min(ans, for05)
	print(ans - 1)


for _ in range(int(input())):
	cost = 35
	number = input()[::-1]
	first_zero = number.index('0') if '0' in number else -1
	first_five = number.index('5') if '5' in number else -1
	nearest_two = number[first_five:].index('2') if '2' in number[first_five:] else -1
	nearest_seven = number[first_five:].index('7') if '7' in number[first_five:] else -1
	nearest_zero = number[first_zero + 1:].index('0') + 1 if '0' in number[first_zero + 1:] else -1
	nearest_five = number[first_zero:].index('5') if '5' in number[first_zero:] else -1
	if first_zero != -1:
		if nearest_five != -1:
			cost = min(cost, nearest_five + first_zero - 1)
		if nearest_zero != -1:
			cost = min(cost, nearest_zero + first_zero - 1)
	if first_five != -1:
		if nearest_two != -1:
			cost = min(cost, nearest_two + first_five - 1)
		if nearest_seven != -1:
			cost = min(cost, nearest_seven + first_five - 1)
	print(cost)


for _ in range(int(input())):
	n = input()
	i_5 = n.rfind('5')
	i_0 = n.rfind('0')
	(x1, x2) = (-1, -1)
	if i_5 != -1:
		x1 = max(n[:i_5].rfind('2'), n[:i_5].rfind('7'))
	if i_0 != -1:
		x2 = max(n[:i_0].rfind('0'), n[:i_0].rfind('5'))
	ans = max(x1, x2)
	print(len(n) - ans - 2)


n = int(input())
for i in range(n):
	s = input()
	s = s[::-1]
	ln = len(s)
	x = [-1, -1]
	for i in range(ln):
		if s[i] == '0':
			if x[0] == -1:
				x[0] = i
			else:
				print(i - 1)
				break
		elif s[i] == '5':
			if x[0] == -1:
				x[1] = i
			else:
				print(i - 1)
				break
		elif s[i] == '2' or s[i] == '7':
			if x[1] != -1:
				print(i - 1)
				break


t = int(input())
for _ in range(t):
	s = input()[::-1]
	f_0 = False
	f_5 = False
	res = 0
	for i in range(len(s)):
		if s[i] == '0':
			if f_0:
				res = i - 1
				break
			else:
				f_0 = True
		elif s[i] == '5':
			if f_0:
				res = i - 1
				break
			else:
				f_5 = True
		elif s[i] in ('2', '7'):
			if f_5:
				res = i - 1
				break
	print(res)


INF = 10000000

def check_num(n, t):
	pos = n.rfind(t[-1])
	if pos == -1:
		return INF
	ans = len(n) - pos - 1
	n = n[:pos]
	pos = n.rfind(t[0])
	if pos == -1:
		return INF
	ans += len(n) - pos - 1
	return ans

def solve():
	n = input()
	if n[-2:] in ['00', '25', '50', '75']:
		print(0)
		return
	ans = check_num(n, '00')
	ans = min(ans, check_num(n, '25'))
	ans = min(ans, check_num(n, '50'))
	ans = min(ans, check_num(n, '75'))
	print(ans)
t = int(input())
for _ in range(t):
	solve()


def solve():
	n = int(input())
	s = str(n)
	i = len(s) - 1
	ans1 = 0
	flag = 0
	while i >= 0:
		if s[i] == '0':
			break
		ans1 += 1
		i -= 1
	i -= 1
	while i >= 0:
		if s[i] == '0' or s[i] == '5':
			flag = 1
			break
		ans1 += 1
		i -= 1
	if flag == 0:
		ans1 = 10 ** 9
	i = len(s) - 1
	ans2 = 0
	while i >= 0:
		if s[i] == '5':
			break
		ans2 += 1
		i -= 1
	i -= 1
	flag = 0
	while i >= 0:
		if s[i] == '2' or s[i] == '7':
			flag = 1
			break
		ans2 += 1
		i -= 1
	if flag == 0:
		ans2 = 10 ** 9
	print(min(ans1, ans2))
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	n = input().strip()
	end_zero = -1
	end_five = -1
	zero_count = 0
	five_count = 0
	for i in range(len(n) - 1, -1, -1):
		if end_zero == -1 and n[i] == '0':
			end_zero = 1
		elif end_zero == 1 and (n[i] == '0' or n[i] == '5'):
			print(zero_count)
			break
		else:
			zero_count += 1
		if end_five == -1 and n[i] == '5':
			end_five = 1
		elif end_five == 1 and (n[i] == '2' or n[i] == '7'):
			print(five_count)
			break
		else:
			five_count += 1


import sys

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

def visited(x, y):
	if x > -1 and y > -1:
		return 1
	return 0
t = int(input())
for _ in range(t):
	x = input()
	check = [-1, -1, -1, -1]
	zero_cnt = 0
	for idx in range(len(x) - 1, -1, -1):
		if x[idx] == '0':
			if not zero_cnt:
				check[0] = idx
				zero_cnt += 1
			else:
				print(len(x) - idx - 2)
				break
		elif x[idx] == '2':
			check[1] = idx
		elif x[idx] == '5':
			check[2] = idx
		elif x[idx] == '7':
			check[3] = idx
		if visited(check[2], check[3]) and check[2] > check[3]:
			print(len(x) - idx - 2)
			break
		if visited(check[1], check[2]) and check[2] > check[1]:
			print(len(x) - idx - 2)
			break
		if visited(check[0], check[2]) and check[0] > check[2]:
			print(len(x) - idx - 2)
			break


t = int(input())
for i in range(t):
	n = input()
	s = len(n)
	i = s - 1
	while n[i] != '0' and n[i] != '5':
		i -= 1
	n = n[:i + 1]
	i -= 1
	ans = 10000
	if n[-1] == '5':
		nn = n[:]
		while i > -1 and n[i] != '2' and (n[i] != '7'):
			i -= 1
		if i != -1:
			n = n[:i + 1] + n[-1]
			ans = s - len(n)
		i = len(nn) - 2
		while i > -1 and nn[i] != '0':
			i -= 1
		if i != -1:
			nn = nn[:i + 1]
			i -= 1
			while i > -1 and nn[i] != '0' and (nn[i] != '5'):
				i -= 1
			if i != -1:
				nn = nn[:i + 1] + nn[-1]
				ans = min(ans, s - len(nn))
	else:
		while i > -1 and n[i] != '5' and (n[i] != '0'):
			i -= 1
		if i != -1:
			n = n[:i + 1] + n[-1]
			ans = s - len(n)
	print(ans)


t = int(input())
for w in range(t):
	s = input()
	ans = 99
	si = len(s) - 1
	count = 0
	while si >= 0:
		if s[si] == '0':
			si -= 1
			break
		count += 1
		si -= 1
	while si >= 0:
		if s[si] == '5' or s[si] == '0':
			break
		count += 1
		si -= 1
	ans = min(ans, count)
	si = len(s) - 1
	count = 0
	while si >= 0:
		if s[si] == '5':
			si -= 1
			break
		count += 1
		si -= 1
	while si >= 0:
		if s[si] == '2' or s[si] == '7':
			break
		count += 1
		si -= 1
	ans = min(ans, count)
	print(ans)


for s in range(int(input())):
	n = input()
	m = n[::-1]
	ans = 1000
	if '5' in m:
		for i in range(m.index('5') + 1, len(m)):
			if m[i] == '2' or m[i] == '7':
				ans = min(ans, i - 1)
				break
	if '0' in m:
		for i in range(m.index('0') + 1, len(m)):
			if m[i] == '0' or m[i] == '5':
				ans = min(ans, i - 1)
				break
	print(ans)


t = int(input())
for p in range(t):
	s = input()[::-1]
	res = 13213123
	flag = 1
	p1 = s.find('0')
	if p1 != -1:
		p2 = s.find('5', p1 + 1)
		if p2 != -1:
			res = min(res, p2 - 1)
	if flag == 1:
		flag = 0
	p1 = s.find('0')
	if p1 != -1:
		p2 = s.find('0', p1 + 1)
		if p2 != -1:
			res = min(res, p2 - 1)
	p1 = s.find('5')
	if p1 != -1:
		p2 = s.find('7', p1 + 1)
		if p2 != -1:
			res = min(res, p2 - 1)
	p1 = s.find('5')
	if p1 != -1:
		p2 = s.find('2', p1 + 1)
		if p2 != -1:
			res = min(res, p2 - 1)
	print(res)


for _ in range(int(input())):
	s = input()
	r = s[::-1]
	a = b = c = d = len(s)
	if '5' in r:
		m = r.find('5')
		n = r.find('2', m, len(s))
		p = r.find('7', m, len(s))
		if n != -1:
			a = n - 1
		if p != -1:
			b = p - 1
	if '0' in r:
		m = r.find('0')
		n = r.find('5', m, len(s))
		p = r.find('0', m + 1, len(s))
		if n != -1:
			c = n - 1
		if p != -1:
			d = p - 1
	print(min(a, b, c, d))


t = int(input())
while t > 0:
	n = str(input())
	flag1 = False
	flag2 = False
	count1 = 0
	count2 = 0
	if int(n) % 25 == 0:
		print(0)
	else:
		for s in reversed(n):
			if s == '0' and (not flag1):
				flag1 = True
			elif s in ('0', '5') and flag1:
				print(count1)
				break
			else:
				count1 = count1 + 1
			if s == '5' and (not flag2):
				flag2 = True
			elif s in ('2', '7') and flag2:
				print(count2)
				break
			else:
				count2 = count2 + 1
	t = t - 1


def plase(k1, k2, s: list):
	if k1 not in s:
		return 1000
	r1 = s.index(k1)
	if r1 < len(s) - 1 and k2 in s[r1 + 1:]:
		r2 = s[r1 + 1:].index(k2)
	else:
		return 1000
	return r1 + r2
a = [str(s * 25) for s in range(1, 41)]
t = int(input())
for _ in range(t):
	n = list(input())
	n = n[::-1]
	d = [0] * 4
	d[0] = plase('5', '2', n)
	d[1] = plase('0', '5', n)
	d[2] = plase('5', '7', n)
	d[3] = plase('0', '0', n)
	print(min(d))


def div(n):
	ans = []
	st = str(n)[::-1]
	z = st.find('00')
	if z != -1:
		ans.append(z)
	k = [st.find('5'), float('inf')]
	if k[0] != -1:
		for l in range(len(st)):
			if st[l] in ['2', '7']:
				if l > k[0]:
					k[1] = min(k[1], l)
					break
	z1 = k[1] - 1
	if k[0] != -1:
		ans.append(z1)
	l1 = st.find('0')
	l2 = float('inf')
	for l in range(len(st)):
		if st[l] in ['0', '5'] and l > l1:
			l2 = min(l2, l)
			break
	z2 = l2 - 1
	if l1 != -1:
		ans.append(z2)
	return min(ans)
for l in range(int(input())):
	print(div(input()))


x = int(input())
for i in range(x):
	y = input()
	e = y
	y = y[::-1]
	ans = -1
	index0 = y.find('0')
	if index0 != -1:
		y = y[0:index0] + '1' + y[index0 + 1:]
		index5 = y[index0:].find('5')
		if index5 != -1:
			index5 = index5 + index0
		index0 = y.find('0')
		indexi = min(index0, index5)
		if indexi == -1:
			index0 = index0 + index5 - indexi
		else:
			index0 = indexi
	index = y.find('5')
	if index != -1:
		index2 = y[index:].find('2')
		if index2 != -1:
			index2 = index2 + index
		index7 = y[index:].find('7')
		if index7 != -1:
			index7 = index7 + index
		index = min(index2, index7)
		if index == -1:
			index = index2 + index7 - index
	indexz = min(index, index0)
	ans = indexz - 1
	if indexz == -1:
		ans = index + index0 - indexz - 1
		if ans < 0:
			ans = len(e)
			if e.find('0') != -1:
				ans = ans - 1
	print(ans)


for t in range(int(input())):
	n = int(input())
	if n % 25 == 0:
		print(0)
	else:
		lst = list(str(n))
		lst = lst[::-1]
		flag = 0
		count = 0
		for i in range(len(lst)):
			if flag == 0:
				if lst[i] in ['5', '0']:
					flag = 1
					temp = lst[i]
				else:
					count += 1
			elif flag == 1:
				if temp == '5':
					if lst[i] in ['2', '7']:
						break
					elif lst[i] == '0':
						if lst[i + 1] in ['0', '5']:
							count += 1
							break
						elif lst[i + 1] in ['2', '7']:
							count += 1
							break
						else:
							flag = 2
					else:
						count += 1
				elif lst[i] in ['5', '0']:
					break
				else:
					count += 1
			elif lst[i] in ['2', '5', '7', '0']:
				count += 1
				break
			else:
				count += 1
		print(count)


n = int(input())
for i in range(n):
	s = input()
	s0 = s.rfind('0')
	count0 = -1
	if s0 != -1:
		count0 = len(s) - s0 - 1
		for j in range(s0 - 1, -1, -1):
			if s[j] == '0' or s[j] == '5':
				break
			count0 += 1
	s5 = s.rfind('5')
	count5 = -1
	if s5 != -1:
		count5 = len(s) - s5 - 1
		for j in range(s5 - 1, -1, -1):
			if s[j] == '7' or s[j] == '2':
				break
			count5 += 1
	if count5 != -1 and count0 != -1:
		print(min(count5, count0))
	elif count5 == -1:
		print(count0)
	else:
		print(count5)


t = int(input())
for _ in range(t):
	n = input()
	if int(n) % 25 == 0:
		print(0)
		continue
	ans = 0
	while n[-1] not in ['0', '5']:
		n = n[:-1]
		ans += 1
	if n == '':
		print(ans)
		continue
	s = set()
	while len(n):
		i = len(n) - 1
		if n[i] == '5':
			if '0' in s:
				ans -= 1
				break
			s.add(n[i])
		elif n[i] == '0':
			if '0' in s:
				ans -= 1
				break
			s.add(n[i])
		elif n[i] in ['7', '2']:
			if '5' in s:
				ans -= 1
				break
			s.add(n[i])
		ans += 1
		n = n[:-1]
	print(ans)


def ans():
	n = int(input())
	x = 0
	y = 0
	c = 0
	while n > 0:
		d = n % 10
		if x == 1 and n % 10 in [2, 7]:
			return c - 1
		if y == 1 and n % 10 in [0, 5]:
			return c - 1
		if n % 10 == 5:
			x = 1
		if n % 10 == 0:
			y = 1
		n = n // 10
		c += 1
for _ in range(int(input())):
	print(ans())


def main(num):
	i = len(num) - 1
	while i >= 0 and num[i] != '0':
		i -= 1
	j = i - 1
	while j >= 0 and num[j] not in ['5', '0']:
		j -= 1
	end0 = len(num) - 1 - i + i - j - 1 if i >= 0 and j >= 0 else len(num)
	i = len(num) - 1
	while i >= 0 and num[i] != '5':
		i -= 1
	j = i - 1
	while j >= 0 and num[j] not in ['2', '7']:
		j -= 1
	end5 = len(num) - 1 - i + i - j - 1 if i >= 0 and j >= 0 else len(num)
	return min(end0, end5)
for _ in range(int(input())):
	num = str(int(input()))
	print(main(num))


t = int(input())
for i in range(t):
	n = input()
	n = list(n)
	n.reverse()
	jzer = 20
	jfiv = 20
	if '0' in n:
		jz0 = 30
		jz5 = 30
		inz = n.index('0')
		nzer = n[inz + 1:]
		if '5' in nzer:
			jz5 = nzer.index('5')
		if '0' in nzer:
			jz0 = nzer.index('0')
		jzer = inz + min(jz0, jz5)
	if '5' in n:
		jz2 = 30
		jz7 = 30
		inf = n.index('5')
		nfiv = n[inf + 1:]
		if '2' in nfiv:
			jz2 = nfiv.index('2')
		if '7' in nfiv:
			jz7 = nfiv.index('7')
		jfiv = inf + min(jz2, jz7)
	print(min(jfiv, jzer))


def test(s):
	n = len(s)
	s = s[::-1]
	(start1, end1, d1) = (-1, -1, n)
	for i in range(n):
		if s[i] == '5':
			start1 = i
			break
	if start1 != -1:
		for i in range(start1 + 1, n):
			if s[i] == '2' or s[i] == '7':
				end1 = i
				break
	if end1 != -1:
		d1 = end1 - 1
	(start2, end2, d2) = (-1, -1, n)
	for i in range(n):
		if s[i] == '0':
			start2 = i
			break
	if start2 != -1:
		for i in range(start2 + 1, n):
			if s[i] == '0' or s[i] == '5':
				end2 = i
				break
	if end2 != -1:
		d2 = end2 - 1
	return min(d1, d2)
T = int(input())
for i in range(T):
	s = input()
	ans = test(s)
	print(ans)


t = int(input())
for _ in range(t):
	n = input()
	c0 = 10 ** 19
	c25 = 10 ** 19
	c50 = 10 ** 19
	c75 = 10 ** 19
	n = n[::-1]
	f_0 = n.find('0')
	t_0 = n.find('0', f_0 + 1)
	if f_0 > -1 and t_0 > -1:
		c0 = t_0 - 1
	f_5 = n.find('5')
	t_2 = n.find('2', f_5 + 1)
	if f_5 > -1 and t_2 > -1:
		c25 = t_2 - 1
	f_0 = n.find('0')
	t_5 = n.find('5', f_0 + 1)
	if f_0 > -1 and t_5 > -1:
		c50 = t_5 - 1
	f_5 = n.find('5')
	t_7 = n.find('7', f_5 + 1)
	if f_5 > -1 and t_7 > -1:
		c75 = t_7 - 1
	print(min(c0, c25, c50, c75))


def Solve():
	a = str(input())
	a = a[::-1]
	count = 1000000
	count_0 = a.count('0')
	count_5 = a.count('5')
	count_2 = a.count('2')
	count_7 = a.count('7')
	if count_0 > 0:
		if count_5 > 0:
			if a.find('5', a.find('0') + 1) != -1:
				count = min(count, a.index('5', a.index('0') + 1) - 1)
		if count_0 > 1:
			count = min(count, a.index('0', a.index('0') + 1) - 1)
	if count_5 > 0:
		if count_2 > 0:
			if a.find('2', a.find('5') + 1) != -1:
				count = min(count, a.index('2', a.index('5') + 1) - 1)
		if count_7 > 0:
			if a.find('7', a.find('5') + 1) != -1:
				count = min(count, a.index('7', a.index('5') + 1) - 1)
	if count == 1000000:
		print(len(a) - count_0)
	else:
		print(count)
x = int(input(''))
for i in range(x):
	Solve()


test = int(input())
for i in range(test):
	num = int(input())
	if num % 25 == 0:
		print(0)
	else:
		b0 = -1
		b5 = -1
		num = str(num)
		l = len(num)
		j = l - 1
		while j >= 0:
			if b0 != -1 and (num[j] == '0' or num[j] == '5'):
				print(l - j - 2)
				break
			if b5 != -1 and (num[j] == '2' or num[j] == '7'):
				print(l - j - 2)
				break
			if num[j] == '0' and b0 == -1:
				b0 = j
			elif num[j] == '5' and b5 == -1:
				b5 = j
			j -= 1


x = int(input())
for i in range(x):
	y = int(input())
	y = str(y)
	y = y[::-1]
	z = y.find('5')
	if z == -1:
		z = 99999999999
	u = y.find('0')
	if u == -1:
		u = 99999999998
	t = y.find('5', u + 1)
	s = y.find('0', u + 1)
	r = y.find('2', z + 1)
	q = y.find('7', z + 1)
	if t == -1:
		t = 99999999997
	if s == -1:
		s = 99999999996
	if r == -1:
		r = 99999999995
	if q == -1:
		q = 99999999994
	if u < z:
		if z > s:
			print(s - 1)
		else:
			print(z - 1)
	elif q < r and q < t and (q < s):
		print(q - 1)
	elif r < t and r < s:
		print(r - 1)
	elif t < s:
		print(t - 1)
	else:
		print(s - 1)


t = int(input())
for i in range(t):
	minimum = 1000
	number = input()
	if number.rfind('0') != -1:
		if number.rfind('0', 0, number.rfind('0')) != -1:
			minimum = min(minimum, len(number) - number.rfind('0', 0, number.rfind('0')) - 2)
		if number.rfind('5', 0, number.rfind('0')) != -1:
			minimum = min(minimum, len(number) - number.rfind('5', 0, number.rfind('0')) - 2)
	if number.rfind('5') != -1:
		if number.rfind('2', 0, number.rfind('5')) != -1:
			minimum = min(minimum, len(number) - number.rfind('2', 0, number.rfind('5')) - 2)
		if number.rfind('7', 0, number.rfind('5')) != -1:
			minimum = min(minimum, len(number) - number.rfind('7', 0, number.rfind('5')) - 2)
	print(minimum)


x = int(input())
while x > 0:
	x -= 1
	n = input()
	n = n[::-1]
	(a1, a2, a3, a4) = (0, 0, 0, 0)
	if n.count('5') > 0:
		l = n.split('5', 1)
		t = len(l[0])
		if l[1].count('2') > 0:
			l_ = l[1].split('2', 1)
			a1 = t + len(l_[0])
		else:
			a1 = len(n) + 1
		if l[1].count('7') > 0:
			l_ = l[1].split('7', 1)
			a2 = t + len(l_[0])
		else:
			a2 = len(n) + 1
	else:
		(a1, a2) = (len(n) + 1, len(n) + 1)
	if n.count('0') > 0:
		l = n.split('0', 1)
		t = len(l[0])
		if l[1].count('5') > 0:
			l_ = l[1].split('5', 1)
			a3 = t + len(l_[0])
		else:
			a3 = len(n) + 1
		if l[1].count('0') > 0:
			l_ = l[1].split('0', 1)
			if len(l_[1]) > 0:
				a4 = t + len(l_[0])
			else:
				a4 = len(n) + 1
		else:
			a4 = len(n) + 1
	else:
		(a3, a4) = (len(n) + 1, len(n) + 1)
	print(min([a1, a2, a3, a4]))


from sys import stdin

def read_input():
	n = stdin.readline().rstrip()
	return (n,)

def solve(n):
	answer = 99
	for i in range(len(n) - 1, 0, -1):
		if n[i] == '0':
			for j in range(i - 1, -1, -1):
				if n[j] == '0' or n[j] == '5':
					answer = min(answer, i - j - 1 + (len(n) - i - 1))
		elif n[i] == '5':
			for j in range(i - 1, -1, -1):
				if n[j] == '2' or n[j] == '7':
					answer = min(answer, i - j - 1 + (len(n) - i - 1))
	return answer

def main():
	t = int(stdin.readline())
	for _ in range(t):
		input = read_input()
		answer = solve(*input)
		print(answer)
main()