In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)
To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.
What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?
Assume that you have sufficient numbers of banknotes, and so does the clerk.

-----Constraints-----
 - N is an integer between 1 and 10^{1,000,000} (inclusive).

-----Input-----
Input is given from Standard Input in the following format:
N

-----Output-----
Print the minimum possible number of total banknotes used by you and the clerk.

-----Sample Input-----
36

-----Sample Output-----
8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Difficulty: MEDIUM

Solutions:
N = input()
L = len(N)
ans = [[0 for e in range(2)] for f in range(L)]
k = int(N[L - 1])
ans[0][0] = k
ans[0][1] = 10 - k
if L > 1:
	for i in range(1, L):
		k = int(N[L - 1 - i])
		ans[i][0] = min(ans[i - 1][0] + k, ans[i - 1][1] + k + 1)
		ans[i][1] = min(ans[i - 1][0] + 10 - k, ans[i - 1][1] + 10 - k - 1)
print(min(ans[L - 1][0], ans[L - 1][1] + 1))


N = list(input())
M = [int(i) for i in N[::-1]]
M.append(0)
c = 0
for i in range(len(M) - 1):
	if M[i] >= 6 or (M[i] >= 5 and M[i + 1] >= 5):
		M[i + 1] += 1
	c += min(M[i], 10 - M[i])
c += M[len(M) - 1]
print(c)


N = input()
DP_just = 0
DP_plus = 1
for c in N:
	d = int(c)
	last_just = DP_just
	last_plus = DP_plus
	DP_just = min(last_just + d, last_plus + 10 - d)
	d += 1
	DP_plus = min(last_just + d, last_plus + 10 - d)
print(DP_just)


A = list(map(int, input()))
while A[-1] == 0:
	A.pop()
n = len(A)
dp = [[0] * 2 for _ in range(n)]
dp[0][1] = 1
for (i, a) in enumerate(A[:-1]):
	if i < n - 1:
		dp[i + 1][0] = min(dp[i][0] + a, dp[i][1] + 10 - a)
		dp[i + 1][1] = min(dp[i][0] + a + 1, dp[i][1] + 9 - a)
ans = min(dp[-1][0] + A[-1], dp[-1][1] + 10 - A[-1])
print(ans)


N = [int(c) for c in input()]
dp = (0, 1)
for i in range(len(N)):
	a = min(dp[0] + N[i], dp[1] + 10 - N[i])
	b = min(dp[0] + N[i] + 1, dp[1] + 10 - N[i] - 1)
	dp = (a, b)
print(dp[0])


s = input()
state = [0, 1]
for i in s:
	n = int(i)
	state_new = [min(state[0] + n, state[1] + 10 - n), min(state[0] + n + 1, state[1] + 9 - n)]
	state = state_new
print(state[0])


N = input()
n = len(N)
a = int(N[0])
b = 11 - int(N[0])
for i in range(n - 1):
	a1 = min(a + int(N[i + 1]), b + int(N[i + 1]))
	b1 = min(a + 11 - int(N[i + 1]), b + 9 - int(N[i + 1]))
	a = a1
	b = b1
print(min(a, b))


def main():
	s = input().rstrip()
	s = '0' + s
	dp = [0, float('inf')]
	for c in s:
		x = int(c)
		dp = [min(dp[0] + x, dp[1] + (10 - x)), min(dp[0] + x + 1, dp[1] + (10 - x) - 1)]
	print(dp[0])

def __starting_point():
	main()
__starting_point()


n = '0' + input()
a = [0, 0]
flag = True
for i in reversed(n):
	b = int(i)
	c = [0, 0]
	if flag:
		flag = False
		c[0] = b
		c[1] = 10 - b
	else:
		c[0] = min(a[0] + b, a[1] + b + 1)
		c[1] = min(a[0] + 10 - b, a[1] + 10 - b - 1)
	a = c[:]
print(min(a))


n = input()
c = 0
r = 1
for x in map(int, n):
	tc = min(c + x, r + 10 - x)
	if x == 9:
		tr = r
	else:
		tr = min(c + x + 1, r + 10 - (x + 1))
	(c, r) = (tc, tr)
print(c)


n = [int(x) for x in list(input()[::-1])] + [0]
ans = 0
step = 0
for i in range(len(n) - 1):
	x = n[i] + step
	if x < 5 or (x == 5 and n[i + 1] < 5):
		ans += x
		step = 0
	else:
		ans += 10 - x
		step = 1
print(ans + step)


s = input()[::-1] + '0'
d = [0, 1]
for c in s:
	x = int(c)
	d = [x + min(d[0], 1 + d[1]), min(1 + d[0], d[1]) + 9 - x]
print(min(d))


n = input()
k = len(n)
dp = [[0] * 2 for _ in range(k)]
dp[0][0] = min(int(n[0]), 11 - int(n[0]))
dp[0][1] = min(int(n[0]) + 1, 11 - (int(n[0]) + 1))
for i in range(1, k):
	dig = int(n[i])
	dp[i][0] = min(dp[i - 1][0] + dig, dp[i - 1][1] + 10 - dig)
	dp[i][1] = min(dp[i - 1][0] + dig + 1, dp[i - 1][1] + 10 - (dig + 1))
print(dp[k - 1][0])


N = input()
inf = float('inf')
dp = [[inf] * (len(N) + 1) for i in range(2)]
dp[0][0] = 0
for i in range(len(N)):
	tmp = int(N[-1 - i])
	dp[0][i + 1] = min(dp[0][i], dp[1][i]) + tmp
	dp[1][i + 1] = min(dp[0][i] + 11, dp[1][i] + 9) - tmp
print(min(dp[0][-1], dp[1][-1]))


n = input()
dp = [0, 1]
ans = 0
for i in range(len(n)):
	cur1 = dp[0]
	cur2 = dp[1]
	if i != len(n) - 1:
		dp[0] = min(cur1 + int(n[i]), cur2 + 10 - int(n[i]))
		dp[1] = min(cur1 + int(n[i]) + 1, cur2 + 9 - int(n[i]))
	else:
		ans = min(dp[0] + int(n[i]), dp[1] + 10 - int(n[i]))
print(ans)


N = list(map(int, input()))
N = N[::-1] + [0]
count = 0
for (i, n) in enumerate(N):
	if n < 5:
		count += n
	elif n > 5:
		count += 10 - n
		N[i + 1] += 1
	elif n == 5:
		if N[i + 1] >= 5:
			N[i + 1] += 1
		count += 5
print(count)


S = input()
N = len(S)
dp = [0]
dp_plus1 = [1]
for i in range(N):
	x = int(S[i])
	dp.append(min(dp[i] + x, dp_plus1[i] + (10 - x)))
	dp_plus1.append(min(dp[i] + (x + 1), dp_plus1[i] + (10 - (x + 1))))
print(dp[-1])


s = str(input())
n = len(s)
dp = [[0] * 2 for _ in range(n + 1)]
dp[0][1] = 1
for i in range(n):
	p = int(s[i])
	dp[i + 1][0] = min(dp[i][1] + 10 - p, dp[i][0] + p)
	dp[i + 1][1] = min(dp[i][1] + 10 - p - 1, dp[i][0] + p + 1)
print(dp[-1][0])


S = input()
L = len(S)
dp = [[0 for _ in range(L + 1)] for over in range(2)]
dp[0][0] = 0
dp[1][0] = 1
for (j, s) in enumerate(S, 1):
	n = int(s)
	dp[0][j] = min(dp[0][j - 1] + n, dp[1][j - 1] + (10 - n))
	dp[1][j] = min(dp[0][j - 1] + (n + 1), dp[1][j - 1] + (10 - n - 1))
print(dp[0][-1])


def resolve():
	S = '0' + input()
	N = len(S)
	dp = [[1 << 60] * 2 for _ in range(N + 1)]
	dp[0][0] = 0
	for i in range(N):
		now = int(S[i])
		dp[i + 1][0] = min(dp[i][0] + now, dp[i][1] + (10 - now))
		dp[i + 1][1] = min(dp[i][0] + now + 1, dp[i][1] + (10 - now - 1))
	print(dp[N][0])

def __starting_point():
	resolve()
__starting_point()


s = input()
n = len(s)
s = list(map(int, list(s)))
dp1 = [0] * (n + 1)
dp2 = [0] * (n + 1)
dp2[0] = 1
for k in range(n):
	dp1[k + 1] = min([dp1[k] + s[k], dp2[k] + 10 - s[k]])
	dp2[k + 1] = min([dp1[k] + s[k] + 1, dp2[k] + 10 - s[k] - 1])
print(dp1[-1])


a = [int(x) for x in input()[::-1]] + [0]
n = len(a)
dp = [[0] * 2 for _ in range(n)]
(dp[0][0], dp[0][1]) = (a[0], 10 - a[0])
for i in range(n - 1):
	dp[i + 1][0] = min(a[i + 1] + dp[i][0], a[i + 1] + dp[i][1] + 1)
	dp[i + 1][1] = min(10 - a[i + 1] + dp[i][0], 10 - (a[i + 1] + 1) + dp[i][1])
print(min(dp[n - 1]))


s = input()
d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
d1 = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2]
a0 = d1[int(s[0])]
a1 = d1[int(s[0]) + 1] if int(s[0]) <= 8 else 1
for c in s[1:]:
	c = int(c)
	b0 = min(a0 + c, a1 + (10 - c))
	b1 = min(a0 + c + 1, a1 + (9 - c)) if c <= 8 else a1
	(a0, a1) = (b0, b1)
print(a0)


n = input()[::-1]
dp = [[0, 0] for i in range(len(n) + 1)]
dp[0][1] = 1
for i in range(len(n)):
	dp[i + 1][0] = min(dp[i][0] + int(n[i]), dp[i][1] - int(n[i]) + 10)
	dp[i + 1][1] = min(dp[i][0] + int(n[i]) + 1, dp[i][1] - int(n[i]) + 9)
print(dp[len(n)][0])


nl = list(input())
dp = [[0, 10000] for i in range(len(nl) + 1)]
for (i, n) in enumerate(nl[::-1]):
	n = int(n)
	dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + n + 1)
	dp[i + 1][1] = min(10 - n + dp[i][0], 10 - n - 1 + dp[i][1])
print(min(dp[-1][0], dp[-1][1] + 1))


(d, e) = (0, 0)
for x in list('0' + input()):
	(d, e) = (int(x) + min(e, d), 9 - int(x) + min(e, d + 2))
print(min(d, e))


N = input().strip()
s = 0
carry = 0
prev = None
for c in reversed(N):
	x = int(c) + carry
	if x > 5 or x == prev == 5:
		if prev == 5:
			x += 1
		s += 10 - x
		carry = 1
	else:
		s += x
		carry = 0
	prev = x
s += carry
print(s)


N = [int(c) for c in input()][::-1]
N.append(0)
ans = 0
for i in range(len(N)):
	c = N[i]
	if c <= 4:
		ans += c
	elif 6 <= c:
		ans += 10 - c
		N[i + 1] += 1
	elif N[i + 1] < 5:
		ans += c
	else:
		ans += 10 - c
		N[i + 1] += 1
print(ans)


n = list(map(int, list(input())))[::-1] + [0]
sum1 = sum(n)
for i in range(len(n) - 1):
	if n[i] > 5 or (n[i] == 5 and n[i + 1] > 4):
		n[i] = 10 - n[i]
		n[i + 1] += 1
print(min(sum1, sum(n)))


N = list(input())
N.insert(0, 0)
ans = 0
for i in range(len(N) - 1, -1, -1):
	n = int(N[i])
	if n // 10 >= 1:
		N[i - 1] = str(int(N[i - 1]) + 1)
		continue
	if n == 5:
		if int(N[i - 1]) >= 5:
			N[i - 1] = str(int(N[i - 1]) + 1)
		ans += 5
	elif n >= 6:
		ans += 10 - n
		N[i - 1] = str(int(N[i - 1]) + 1)
	else:
		ans += n
print(ans)


n = input()
a = 0
b = 2
for s in n:
	v = int(s)
	a_ = min(a + v, b + v)
	b_ = min(a + 10 - v + 1, b + 10 - v + 1 - 2)
	a = a_
	b = b_
print(min(a, b))


S = input()
n = len(S)
dp = [[0] * 2 for _ in range(n + 1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(n):
	dp[i + 1][0] = min(int(S[i]) + dp[i][0], 10 - int(S[i]) + dp[i][1])
	dp[i + 1][1] = min(int(S[i]) + 1 + dp[i][0], 10 - int(S[i]) - 1 + dp[i][1])
print(dp[n][0])


n = [0] + list(map(int, input())) + [0]
n = n[::-1]
b0 = [0] * len(n)
b1 = [0] * len(n)
b1[0] = 1
for i in range(1, len(n)):
	num = n[i]
	b0[i] = min(b0[i - 1] + num, b1[i - 1] + num + 1)
	b1[i] = min(b0[i - 1] + 10 - num, b1[i - 1] + 10 - (num + 1))
print(min(b0[-1], b1[-1]))


N = input().replace('\n', '')
L = len(N)
dp = [[0 for _ in range(2)] for i in range(L + 1)]
dp[0][1] = 1
for i in range(L):
	n = int(N[i])
	dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + (10 - n))
	dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + (9 - n))
print(dp[L][0])


n = input()
len_n = len(n)
dp = [[0, 2] for i in range(len_n + 1)]
for (k, v) in enumerate(n[::-1]):
	v = int(v)
	dp[k + 1][0] = min(dp[k][0] + v, dp[k][1] + v)
	dp[k + 1][1] = min(dp[k][0] + 10 - v + 1, dp[k][1] + 10 - v - 1)
print(min(dp[len_n]))


s = input()
dp0 = [0] * (len(s) + 1)
dp1 = [0] * (len(s) + 1)
dp1[0] = 1
for i in range(1, len(s) + 1):
	n = int(s[i - 1:i])
	dp0[i] = min(dp0[i - 1] + n, dp1[i - 1] + 10 - n)
	dp1[i] = min(dp0[i - 1] + (1 if n + 1 == 10 else n + 1), dp1[i - 1] + 10 - n - 1)
print(dp0[-1])


N = [int(i) for i in list(input())]
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1
for i in range(1, len(N) + 1):
	n = N[i - 1]
	dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + 10 - n)
	n += 1
	dp[i][1] = min(dp[i - 1][0] + n, dp[i - 1][1] + 10 - n)
print(dp[-1][0])


n_ = list(input())
n = [int(i) for i in n_]
ans = 0
if len(n_) == 1:
	print(min(n[0], 11 - n[0]))
	exit()
dp1 = 0
dp2 = 1
for i in range(len(n)):
	dp_1 = min(dp1 + n[i], dp2 + 10 - n[i])
	dp_2 = min(dp1 + n[i] + 1, dp2 + 9 - n[i])
	dp1 = dp_1
	dp2 = dp_2
print(dp1)


N = input()
a = 0
k = 0
for i in range(len(N) - 1, -1, -1):
	n = int(N[i]) + k
	if n < 6:
		a += n
		if n == 5 and i != 0 and (int(N[i - 1]) > 4):
			k = 1
		else:
			k = 0
	elif n != 10:
		a += 10 - n
		k = 1
	else:
		a += 0
		k = 1
a += k
print(a)


n = list(map(int, input()))[::-1]
n.append(0)
for i in range(len(n)):
	if n[i] >= 6 or (n[i] == 5 and n[i + 1] >= 5):
		n[i] = 10 - n[i]
		n[i + 1] += 1
print(sum(n))


n = input()
l = len(n)
now = 0
m = 1
p = 0
for i in range(l):
	d = int(n[i])
	M = [m, p]
	P = [m, p]
	M[0] += 9 - d
	P[0] += 10 - d
	M[1] += 1 + d
	P[1] += d
	m = min(M)
	p = min(P)
m += 1
print(min(m, p))


N = list(map(int, list(input())))
dp = [[-1, -1] for i in range(len(N) + 1)]
dp[0] = [0, 1]
b = 0
N.insert(0, 0)
for i in range(1, len(N)):
	up = 10 - N[i]
	dp[i][0] = min(dp[i - 1][0] + N[i], dp[i - 1][1] + up)
	dp[i][1] = min(dp[i - 1][0] + N[i] + 1, dp[i - 1][1] + up - 1)
print(dp[-1][0])


N = list(map(int, list(input())))[::-1]
L = len(N)
answer = 0
for (i, n) in enumerate(N):
	if n < 5:
		answer += n
	elif n > 5:
		answer += 10 - n
		if i < L - 1:
			N[i + 1] += 1
		else:
			answer += 1
	else:
		answer += 5
		if i < L - 1:
			if N[i + 1] >= 5:
				N[i + 1] += 1
print(answer)


N = map(int, input()[::-1])
(a, b) = (0, 9)
for n in N:
	(a, b) = (min(n + a, n + b), min(11 - n + a, 9 - n + b))
print(min(a, b))


s = list(map(int, list(input())))
s = s[::-1]
s = s + [0]
cnt = 0
kuri = 0
for i in range(len(s)):
	n = s[i]
	if n < 5:
		cnt += n
	elif n > 5:
		cnt += 10 - n
		s[i + 1] += 1
	else:
		if s[i + 1] >= 5:
			s[i + 1] += 1
		cnt += 10 - n
print(cnt)


S = input()[::-1]
l = len(S)
dp = [[0] * 2 for _ in range(l + 1)]
for i in range(l):
	dp[i + 1][0] = min(dp[i][0] + int(S[i]), dp[i][1] + int(S[i]) + 1)
	if i != 0:
		dp[i + 1][1] = min(dp[i][0] + 10 - int(S[i]), dp[i][1] + 10 - (int(S[i]) + 1))
	else:
		dp[i + 1][1] = dp[i][0] + 10 - int(S[i])
print(min(dp[-1][0], dp[-1][1] + 1))


def main():
	S = list(map(int, list(input())))
	N = len(S)
	dp = [[0, 0] for _ in range(N + 1)]
	dp[0][1] = 2
	for i in range(N):
		s = S[i]
		dp[i + 1][0] = min(dp[i][0] + s, dp[i][1] + s)
		dp[i + 1][1] = min(dp[i][0] + 11 - s, dp[i][1] + 9 - s)
	print(min(dp[N][0], dp[N][1]))

def __starting_point():
	main()
__starting_point()


n = '0' + input()
X = []
for i in range(len(n)):
	X.append(int(n[i]))
cnt = 0
i = len(n) - 1
while i >= 0:
	if X[i] > 5:
		cnt += 10 - X[i]
		X[i - 1] += 1
	elif X[i] == 5:
		cnt += 5
		if X[i - 1] >= 5:
			X[i - 1] += 1
	else:
		cnt += X[i]
	i -= 1
print(cnt)


def solve(*args: str) -> str:
	N = list(map(int, args[0]))
	(a, b) = (0, 1)
	for n in N:
		(a, b) = (min(b + (10 - n), a + n), min(b + (9 - n), a + n + 1))
	return str(a)

def __starting_point():
	print(solve(*open(0).read().splitlines()))
__starting_point()


s = list(list(map(int, input())))
n = len(s)
dp = [[0, 0] for i in range(n)]
dp[0][0] = s[0]
dp[0][1] = 11 - (s[0] - 0)
for i in range(n - 1):
	d = s[i + 1]
	dp[i + 1][0] = min(dp[i][0], dp[i][1]) + d
	dp[i + 1][1] = min(dp[i][0] + 11 - d, dp[i][1] + 9 - d)
print(min(dp[n - 1][0], dp[n - 1][1]))


N = input()
lim = len(N)
dp = [[0, 0] for _ in range(lim + 1)]
dp[0][1] = 1
for i in range(lim):
	n = int(N[i])
	dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
	dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - n - 1)
print(dp[-1][0])


N = [int(s) for s in input()]
M = len(N)
eq = 0
ov = 1
for n in N:
	a = min(eq + n, ov + 10 - n)
	b = min(eq + (n + 1), ov + 10 - (n + 1))
	(eq, ov) = (a, b)
print(eq)


def main():
	N = [int(x) for x in input()]
	dp0 = 0
	dp1 = 1
	for n in N:
		a = min(dp0 + n, dp1 + 10 - n)
		b = min(dp0 + n + 1, dp1 + 10 - (n + 1))
		(dp0, dp1) = (a, b)
	print(dp0)
main()


s = input()
n = len(s)
ans = 0
sub = 0
judge = False
for i in range(n - 1, -1, -1):
	key = int(s[i]) + sub
	if key <= 4:
		ans += key
		sub = 0
	elif key == 5:
		ans += 5
		if i == 0:
			sub = 0
		elif int(s[i - 1]) <= 4:
			sub = 0
		else:
			sub = 1
	else:
		ans += 10 - key
		sub = 1
ans += sub
print(ans)


s = input()
l = len(s)
cnt = 0
ls = []
ls.append(0)
ls.append(0)
for i in range(l):
	ls.append(int(s[i]))
for i in range(l + 1):
	if 6 <= ls[-i - 1] or (ls[-i - 1] == 5 and ls[-i - 2] >= 5):
		ls[-i - 1] = 10 - ls[-i - 1]
		ls[-i - 2] += 1
print(sum(ls))


N = [int(s) for s in input()]
n = len(N)
a = 0
b = 11
for n in N:
	a1 = min(a + n, b + n)
	b1 = min(a + 11 - n, b + 9 - n)
	a = a1
	b = b1
print(min(a, b))


n = input()
dp = [0, 10000]
for d in map(int, reversed(n)):
	dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]
print(min(dp[0], 1 + dp[1]))


n = input()
(a, b) = (0, 100)
for i in n:
	j = int(i)
	(a, b) = (min(a + j, b + j), min(a + 11 - j, b + 9 - j))
	if j == 0:
		b = a + 100
print(min(a, b))


N = list(map(int, input()))
m = 0
m_ = 1
for n in N:
	(m, m_) = (min(m + n, m_ + 10 - n), min(m + n + 1, m_ + 10 - (n + 1)))
print(m)


n = input()[::-1]
n += '0'
L = len(n)
ans = 0
chk = 0
flg = 0
for i in range(L):
	chk = int(n[i])
	chk += flg
	if chk == 5:
		if int(n[i + 1]) >= 5:
			ans += 5
			flg = 1
		else:
			ans += 5
			flg = 0
	elif chk > 5:
		ans += 10 - chk
		flg = 1
	else:
		ans += chk
		flg = 0
print(ans + flg)


x = list(map(int, list(input())))
n = len(x)
dp = [[0, 1] for _ in range(n + 1)]
for i in range(n):
	dp[i + 1][0] = min(dp[i][0] + x[i], dp[i][1] + 10 - x[i])
	dp[i + 1][1] = min(dp[i][0] + x[i] + 1, dp[i][1] + 9 - x[i])
print(dp[n][0])


n = list(map(int, list(input())))
n = n[::-1] + [0]
d = len(n)
ans = 0
for (i, k) in enumerate(n):
	if k < 5:
		ans += k
	elif k == 5 and n[i + 1] < 5:
		ans += k
	else:
		ans += 10 - k
		n[i + 1] += 1
print(ans)


n = input()
l = len(n)
pre = [0, 1]
for i in range(l):
	c = [0, 0]
	d = int(n[i])
	c[0] = min(pre[0] + d, pre[1] + 10 - d)
	c[1] = min(pre[0] + d + 1, pre[1] + 10 - d - 1)
	pre = c
print(pre[0])


s = input()
s = ''.join([i for i in reversed(s)]) + '0'
INF = 10 ** 32
n = len(s)
dp = [[INF] * 2 for _ in range(n)]
dp[0][0] = 0
for i in range(n - 1):
	x = int(s[i])
	dp[i + 1][0] = min(dp[i][0] + x, dp[i][1] + x)
	dp[i + 1][1] = min(dp[i][0] + 1 + (10 - x), dp[i][1] + (9 - x))
print(min(dp[n - 1][0], dp[n - 1][1]))


def main():
	(*e,) = list(map(int, input()))
	e.reverse()
	inf = 20 * len(e)
	dp = [0, inf]
	for x in e:
		ndp = [-1, -1]
		ndp[0] = min(dp[0] + x, dp[1] + (x + 1 if x < 9 else inf))
		ndp[1] = min(dp[0] + (10 - x if x > 0 else inf), dp[1] + (10 - (x + 1)))
		dp = ndp
	print(min(dp[0], dp[1] + 1))

def __starting_point():
	main()
__starting_point()


n = '0' + input()
n = list(map(int, list(n)))
n.reverse()
pay = 0
turi = 0
pls = 0
for i in range(len(n)):
	val = n[i] + pls
	pls = 0
	if val < 5:
		pay += val
	elif val == 5:
		pay += val
		if n[i + 1] >= 5:
			pls = 1
	else:
		turi += 10 - val
		pls = 1
ans = pay + turi
print(ans)


import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 9)
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
nn = lambda : list(stdin.readline().split())
ns = lambda : stdin.readline().rstrip()
n = ns()
n = list(map(int, n[::-1])) + [0]
ans = 0
for i in range(len(n)):
	a = n[i]
	if a <= 4:
		ans += a
	elif a >= 6:
		ans += 10 - a
		n[i + 1] += 1
	elif n[i + 1] <= 4:
		ans += a
	else:
		ans += a
		n[i + 1] += 1
print(ans)


N = list('0' + input())
l = len(N)
ans = 0
i = l - 1
while i > 0:
	if int(N[i]) >= 6 or (int(N[i]) == 5 and int(N[i - 1]) >= 5):
		ans += 10 - int(N[i])
		i -= 1
		while N[i] == '9':
			i -= 1
		N[i] = str(int(N[i]) + 1)
	else:
		ans += int(N[i])
		i -= 1
ans += int(N[0])
print(ans)


N_list = [int(i) for i in list(input())]
N_list = N_list[::-1] + [0]
for index in range(0, len(N_list)):
	if N_list[index] >= 6 or (N_list[index] == 5 and N_list[index + 1] >= 5):
		N_list[index] = 10 - N_list[index]
		N_list[index + 1] += 1
print(sum(N_list))


N = [int(i) for i in input()]
dp = [0, float('inf')]
for n in N:
	ndp = [0] * 2
	ndp[0] = min(dp[0] + n, dp[1] + n)
	ndp[1] = min(dp[0] + 11 - n, dp[1] + 9 - n)
	dp = ndp
print(min(dp[0], dp[1]))


N = list(input())
N.insert(0, '0')
count = 0
for i in range(len(N) - 1, -1, -1):
	if int(N[i]) <= 4:
		count += int(N[i])
	elif int(N[i]) == 5 and int(N[i - 1]) <= 4:
		count += int(N[i])
	else:
		count += 10 - int(N[i])
		N[i - 1] = str(int(N[i - 1]) + 1)
print(count)


abef = 0
bbef = 1
s = input()
for i in range(len(s)):
	curr = int(s[len(s) - 1 - i])
	a = abef + curr
	b = bbef + 9 - curr
	a = min(a, 1 + bbef + curr)
	b = min(b, abef + 10 - curr)
	abef = a
	bbef = b
curr = 0
a = abef + curr
b = bbef + 9 - curr
a = min(a, 1 + bbef + curr)
b = min(b, abef + 10 - curr)
print(min(a, b))


N = [int(_) for _ in list(input())]
(a, b) = (0, 1)
for n in N:
	(a, b) = (min(a + n, b + 10 - n), min(a + (n + 1), b + 10 - (n + 1)))
print(a)


n = input()
dp1 = [0] * (len(n) + 1)
dp2 = [1] * (len(n) + 1)
for i in range(1, len(n) + 1):
	m = int(n[i - 1])
	dp1[i] = min(dp1[i - 1] + m, dp2[i - 1] + 10 - m)
	dp2[i] = min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m)
print(dp1[-1])


digits = [0] + [int(c) for c in input()]
dp = [0, 1]
for digit in reversed(digits):
	(dp0, dp1) = dp
	dp[0] = min(dp0, dp1 + 1) + digit
	dp[1] = min(dp0, dp1 - 1) + 10 - digit
print(min(dp))


s = [int(i) for i in input()]
n = len(s)
ans = 0
f = 0
for i in range(n - 1, -1, -1):
	j = s[i] + f
	if j < 5:
		ans += j
		f = 0
	elif j > 5:
		ans += 10 - j
		f = 1
	elif i and s[i - 1] >= 5:
		ans += 10 - j
		f = 1
	else:
		ans += j
		f = 0
print(ans + f)


n = input()
lenn = len(n)
dp = [0, 10000]
for d in reversed(n):
	d = int(d)
	dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]
print(min(dp[0], 1 + dp[1]))


N = list(input())
num = 0
ans = 0
for i in range(len(N) - 1, -1, -1):
	n = int(N[i])
	if n + num == 5 and i > 0 and (int(N[i - 1]) >= 5):
		ans += 5
		num = 1
	elif n + num < 6:
		ans += n + num
		num = 0
	else:
		ans += 10 - (n + num)
		num = 1
ans += num
print(ans)


n = input().rstrip()
total = 0
carry = 0
for i in range(len(n) - 1, -1, -1):
	v = int(n[i]) + carry
	if v > 5:
		total += 10 - v
		carry = 1
	elif v == 5:
		if i == 0:
			total += 5
			carry = 0
		elif int(n[i - 1]) >= 5:
			total += 5
			carry = 1
		else:
			total += 5
			carry = 0
	else:
		total += v
		carry = 0
print(total + carry)


n = input()
n = n[::-1] + '0'
inf = 10 ** 9
dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 2
for i in range(len(n)):
	n_i = int(n[i])
	dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
	dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i)
print(min(dp[-1][0], dp[-1][1]))


import sys

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

def main():
	N = input()
	count = 0
	S = len(N)
	flag = 0
	for i in range(S):
		temp = flag + int(N[-1 - i])
		if temp > 5:
			count += 10 - temp
			flag = 1
		elif temp == 5 and i != S - 1 and (int(N[-1 - i - 1]) >= 5):
			count += temp
			flag = 1
		else:
			count += temp
			flag = 0
	print(count + flag)

def __starting_point():
	main()
__starting_point()


n = reversed(list(map(int, input())))
(a, b) = (0, 30)
for x in n:
	(a, b) = (min(a + x, b + x), min(a - x + 11, b - x + 9))
print(min(a, b))


s = input()[::-1]
size = len(s)
s += '4'
ans = 0
bef = 0
for i in range(size):
	v1 = int(s[i])
	v2 = int(s[i + 1])
	if v1 + bef >= 6 or (v1 + bef >= 5 and v2 >= 5):
		ans += 10 - (v1 + bef)
		bef = 1
	else:
		ans += v1 + bef
		bef = 0
ans += bef
print(ans)


S = input()
N = list(map(int, S))
L = len(N)
dp = [[0] * 2 for i in [0] * (L + 1)]
dp[0][1] = 1
for i in range(L):
	n = N[i]
	dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n)
	if n == 9:
		dp[i + 1][1] = dp[i][1]
	else:
		dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n + 1))
print(dp[L][0])


dp = [0, 1]
s = input()
s = s[::-1]
s += '0'
for i in range(len(s)):
	(dp0, dp1) = dp
	dp[0] = min(dp0, dp1 + 1) + int(s[i])
	dp[1] = min(dp0, dp1 - 1) + 10 - int(s[i])
print(min(dp))


N = str(input())
L = len(N)
ans = 0
dp = [[0, 0] for _ in range(L)]
s = list(map(int, N))
dp[0] = [s[0], 11 - s[0]]
for i in range(1, L):
	n = s[i]
	dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + n)
	dp[i][1] = min(dp[i - 1][1] + 9 - n, dp[i - 1][0] + 11 - n)
print(min(dp[-1]))


n = str(input().rstrip())
ans = 0
a = list(map(int, list(n)))
a = a[::-1]
a.append(0)
for i in range(len(a) - 1):
	c = a[i]
	if c < 5:
		ans += c
	elif c == 5 and a[i + 1] < 5:
		ans += 5
	else:
		a[i + 1] += 1
		ans += 10 - c
ans += a[-1]
print(ans)


def resolve():
	S = input()
	n = len(S)
	dp = [[0] * 2 for _ in range(n + 1)]
	dp[0][1] = 1
	for i in range(1, n + 1):
		d = int(S[i - 1])
		dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
		dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
	print(dp[n][0])

def __starting_point():
	resolve()
__starting_point()


(p, q) = (0, 1)
for x in map(int, input()):
	(p, q) = (min(p + x, q + 10 - x), min(p + x + 1, q + 9 - x))
print(p)


n = input()
l = len(n)
(a, b) = (0, 1)
for v in n:
	v = int(v)
	a_next = min(a + v, b + (10 - v))
	b_next = min(a + v + 1, b + (10 - (v + 1)))
	a = a_next
	b = b_next
print(a)


n = list(input())
n.reverse()
n.append('0')
n = list(map(int, n))
ans = 0
dp1 = n[0]
dp2 = 10 - n[0]
for i in n[1:]:
	(dp1, dp2) = (min(i + dp1, i + 1 + dp2), min(10 - i + dp1, 9 - i + dp2))
print(min(dp1, dp2))


n = input()
l = list(map(int, str(n)))
dp = [[0] * 2 for _ in range(len(l) + 1)]
dp[0][1] = 1
for (i, v) in enumerate(l):
	dp[i + 1][0] = min(dp[i][0] + v, dp[i][1] + (10 - v))
	dp[i + 1][1] = min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1)
print(dp[-1][0])


n = input()
n = n[::-1] + '0'
l = len(n)
ans = 0
kuriagari = 0
for (i, v) in enumerate(n):
	v = int(v)
	if kuriagari:
		v += 1
	if v < 5:
		ans += v
		kuriagari = 0
	if v > 5:
		ans += 10 - v
		kuriagari = 1
	if v == 5:
		if int(n[i + 1]) < 5:
			ans += v
			kuriagari = 0
		if int(n[i + 1]) >= 5:
			ans += 10 - v
			kuriagari = 1
print(ans)


N = input()
over = 1
eq = 0
for n in map(int, N):
	o = min(eq + n + 1, over + (10 - n) - 1)
	e = min(eq + n, over + (10 - n))
	over = o
	eq = e
print(eq)


import sys
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)

def run():
	N = list(map(int, list(input())))
	N = N[::-1] + [0]
	count = 0
	seq = 0
	for (idx, n) in enumerate(N[:]):
		if seq == 1 and (n >= 5 or (n == 4 and N[idx + 1] >= 5)):
			count += 9 - n
		elif seq == 0 and (n >= 6 or (n == 5 and N[idx + 1] >= 5)):
			count += 10 - n
			seq = 1
		else:
			count += n + seq
			seq = 0
	print(count)

def __starting_point():
	run()
__starting_point()


n = [int(x) for x in input()]
dp = [[100000000000 for i in range(len(n) + 1)] for j in range(2)]
dp[0][0] = 0
dp[1][0] = 1
for i in range(len(n)):
	d = n[i]
	dp[0][i + 1] = min(dp[0][i + 1], min(d, 11 - d) + dp[0][i], 10 - d + dp[1][i])
	dp[1][i + 1] = min(dp[1][i + 1], d + 1 + dp[0][i], 9 - d + dp[1][i])
print(dp[0][len(n)])


N = list(map(int, input()))
N.insert(0, 0)
count = 0
for i in range(len(N) - 1, -1, -1):
	if N[i] <= 4 or (N[i] == 5 and N[i - 1] <= 4):
		count += N[i]
	else:
		count += 10 - N[i]
		N[i - 1] = N[i - 1] + 1
print(count)


stdin = open(0)
N = [int(x) for x in stdin.read().rstrip()]

def solve(N):
	L = len(N)
	dp = (0, 1)
	for n in N:
		a = min(dp[0] + n, dp[1] + 10 - n)
		b = min(dp[0] + n + 1, dp[1] + 10 - (n + 1))
		dp = (a, b)
	return dp[0]
print(solve(N))


def main():
	N = [int(x) for x in input()]
	l = len(N) + 1
	DP = [[0] * 2 for _ in range(l)]
	DP[0][1] = 1
	for (i, n) in enumerate(N):
		DP[i + 1][0] = min(DP[i][0] + n, DP[i][1] + (10 - n))
		DP[i + 1][1] = min(DP[i][0] + (n + 1), DP[i][1] + (10 - (n + 1)))
	print(DP[-1][0])
main()


N = [int(c) for c in input()]
dp = (0, 1)
for n in N:
	a = min(dp[0] + n, dp[1] + 10 - n)
	b = min(dp[0] + n + 1, dp[1] + 10 - (n + 1))
	dp = (a, b)
print(dp[0])


def main():
	a = list(map(int, list(input())))
	n = len(a)
	dp = [[0, 0] for _ in [0] * (n + 1)]
	dp[0][1] = 1
	for i in range(n):
		dp[i + 1][0] = min(dp[i][0] + a[i], dp[i][1] + 10 - a[i])
		dp[i + 1][1] = min(dp[i][0] + a[i] + 1, dp[i][1] + 9 - a[i])
	print(dp[n][0])
main()


s = input()
n = len(s)
dp = [[0, 0] for i in range(n)]
dp[0][0] = ord(s[0]) - ord('0')
dp[0][1] = 11 - (ord(s[0]) - ord('0'))
for i in range(n - 1):
	d = ord(s[i + 1]) - ord('0')
	dp[i + 1][0] = min(dp[i][0], dp[i][1]) + d
	dp[i + 1][1] = min(dp[i][0] + 11 - d, dp[i][1] + 9 - d)
print(min(dp[n - 1][0], dp[n - 1][1]))


N = input()[::-1]
dp1 = [0]
dp2 = [1]
for x in N:
	x = int(x)
	r = dp1[-1] + x
	if x < 9:
		r = min(r, dp2[-1] + x + 1)
	dp1.append(r)
	dp2.append(min(dp1[-2] + 10 - x, dp2[-1] + 9 - x))
print(min(dp1[-1], dp2[-1] + 1))


n = list(input())
r = list(map(int, n))[::-1]
ans = 0
k = 0
f = 0
for i in r:
	i += k
	if f == 5 and i > 4:
		i += 1
	ans += min(i, 10 - i)
	k = int(i > 5)
	f = i
print(ans + k)


import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	N = input() + '0'
	dp = [[f_inf] * 2 for _ in range(len(N) + 1)]
	dp[0][0] = 0
	dp[0][1] = 1
	for i in range(1, len(N) + 1):
		n = int(N[i - 1])
		dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (10 - n))
		dp[i][1] = min(dp[i - 1][0] + n + 1, dp[i - 1][1] + (10 - n - 1))
	print(dp[-1][0])

def __starting_point():
	resolve()
__starting_point()


n = input()
keta = [0, 0]
for i in range(len(n)):
	keta.append(int(n[i]))
ans = 0
for i in range(len(keta) - 1):
	if keta[-i - 2] >= 5:
		if keta[-i - 1] >= 5:
			ans += 10 - keta[-i - 1]
			keta[-i - 2] += 1
		else:
			ans += keta[-i - 1]
	elif keta[-i - 1] >= 6:
		ans += 10 - keta[-i - 1]
		keta[-i - 2] += 1
	else:
		ans += keta[-i - 1]
print(ans)


n = list(map(int, list(input())))
ln = len(n)
ans = 0
for i in range(-1, -ln, -1):
	if n[i] == 10:
		n[i - 1] += 1
		continue
	elif n[i] < 5:
		ans += n[i]
	elif n[i] > 5:
		ans += 10 - n[i]
		n[i - 1] += 1
	elif n[i - 1] < 5:
		ans += 5
	else:
		ans += 5
		n[i - 1] += 1
if n[0] == 10:
	ans += 1
elif n[0] <= 5:
	ans += n[0]
else:
	ans += 11 - n[0]
print(ans)


N = [int(c) for c in input()][::-1]
dp = [[0] * 2 for _ in range(len(N) + 1)]
dp[0][1] = 1
for i in range(len(N)):
	dp[i + 1][1] = min(dp[i][0] + N[i] + 1, dp[i][1] + 10 - N[i] - 1)
	dp[i + 1][0] = min(dp[i][0] + N[i], dp[i][1] + 10 - N[i])
print(dp[len(N)][0])


N = [int(i) for i in input()]
n = N[::-1] + [0]
for i in range(len(n) - 1):
	if n[i] >= 6 or (n[i] == 5 and n[i + 1] >= 5):
		n[i] = 10 - n[i]
		n[i + 1] += 1
print(sum(n))


dp = [0, 2]
for d in map(int, reversed(input())):
	dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]
print(min(dp[0], 1 + dp[1]))


N = input()
dp0 = [0] * (len(N) + 1)
dp1 = [0] * (len(N) + 1)
dp1[0] = 1
for i in range(len(N)):
	n = int(N[i])
	dp0[i + 1] = min(dp0[i] + n, dp1[i] + (10 - n))
	n += 1
	dp1[i + 1] = min(dp0[i] + n, dp1[i] + (10 - n))
print(dp0[-1])


s = input()
INF = float('inf')
dp = [[INF, INF] for _ in range(len(s) + 1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(len(s)):
	dp[i + 1][0] = min(dp[i][0] + int(s[i]), dp[i][1] + 10 - int(s[i]), dp[i][1] + int(s[i]) + 1)
	dp[i + 1][1] = min(dp[i][0] + int(s[i]) + 1, dp[i][1] + 10 - int(s[i]) - 1)
print(dp[-1][0])


a = b = 0
for n in map(int, '0' + input()):
	(a, b) = (n + min(a, b), 9 - n + min(b, a + 2))
print(min(a, b))


N = '0' + input()
N = N[::-1]
DP = [[0, 0] for i in range(len(N))]
DP[0][0] = int(N[0])
DP[0][1] = 10 - int(N[0])
for i in range(1, len(N)):
	DP[i][0] = min(DP[i - 1][0] + int(N[i]), DP[i - 1][1] + int(N[i]) + 1)
	DP[i][1] = min(DP[i - 1][0] + 10 - int(N[i]), DP[i - 1][1] + 10 - (int(N[i]) + 1))
print(min(DP[-1]))


N = list(map(int, input()))
ans = 0
DP = [0] * 2
DP[0] = 0
DP[1] = 1
for i in N:
	(a, b) = (DP[0], DP[1])
	DP[0] = a + i if a + i < b + (10 - i) else b + (10 - i)
	DP[1] = a + i + 1 if a + i + 1 < b + (9 - i) else b + (9 - i)
print(DP[0] if DP[0] <= DP[1] + 1 else DP[1] + 1)


def resolve():
	N = input().strip()
	carry = 0
	a = 0
	b = 1
	for ch in N:
		n = ord(ch) - ord('0')
		a2 = min(a + n, b + 10 - n)
		b = min(a + n + 1, b + 10 - n - 1)
		a = a2
	print(a)
resolve()


n = list(map(int, reversed(list(input()))))
a = 0
b = 9999
for x in n:
	temp_a = min(a + x, b + x)
	b = min(a + 10 - x + 1, b + 10 - x - 1)
	a = temp_a
print(min(a, b))


n = input()
a = int(n[-1])
b = 10 - int(n[-1])
for i in range(len(n) - 2, -1, -1):
	(a, b) = (min(a + int(n[i]), b + int(n[i]) + 1), min(a + 10 - int(n[i]), b + 9 - int(n[i])))
print(min(a, b + 1))


N = input()
dp = [0, 1]
for n in N:
	n = int(n)
	(dp[0], dp[1]) = (min(dp[0] + n, dp[1] + 10 - n), min(dp[0] + n + 1, dp[1] + 10 - (n + 1)))
print(dp[0])


s = '0' + input()
ans = 0
n = len(s)
f = 0
p = 0
for i in range(1, n + 1):
	n = int(s[-i])
	if p + (n > 4) > 5:
		f = 1
	else:
		f = 0
	n += f
	ans += min(n, 10 - n)
	p = n
print(ans)


N = list(map(int, list(input())))
ans = 0
skfl = 0
nf = 0
for i in range(1, len(N) + 1):
	if i != len(N):
		if N[-(i + 1)] >= 5:
			nf = 1
	if N[-i] + skfl + nf > 5:
		ans += 10 - N[-i] - skfl
		skfl = 1
	else:
		ans += N[-i] + skfl
		skfl = 0
	nf = 0
print(ans + skfl)


n = input()[::-1]
ans = 0
flag = 0
for i in range(len(n)):
	a = int(n[i])
	a += flag
	if a == 5 and i + 1 < len(n) and (int(n[i + 1]) >= 5):
		ans += 10 - a
		flag = 1
	elif a <= 5:
		ans += a
		flag = 0
	else:
		ans += 10 - a
		flag = 1
ans += flag
print(ans)


n = input()
precise = 0
over = 1
for d in n:
	d = int(d)
	new_over = min(precise + d + 1, over + d + 2, over + 9 - d)
	precise = min(over + d + 1, precise + d)
	over = new_over
over += 1
print(min(precise, over))


N = list(input())
N = N[::-1]
N_int = [int(i) for i in N]
N_int.append(0)
maisu = 0
keta = False
for i in range(len(N_int) - 1):
	if keta == True:
		N_int[i] += 1
	if N_int[i] < 5:
		maisu += N_int[i]
		keta = False
	elif N_int[i] == 5:
		if N_int[i + 1] > 4:
			keta = True
			maisu += 10 - N_int[i]
		else:
			keta = False
			maisu += N_int[i]
	else:
		keta = True
		maisu += 10 - N_int[i]
if keta == True:
	maisu += 1
print(maisu)


n = '0' + input()
N = len(n)
plus = 1
just = 0
for i in range(1, N):
	plus_plus = plus + 10 - (int(n[i]) + 1)
	plus_just = plus + 10 - int(n[i])
	just_plus = just + 1 + int(n[i])
	just_just = just + int(n[i])
	plus = min(plus_plus, just_plus)
	just = min(just_just, plus_just)
print(just)


def main():
	n = input()
	(a, b) = (0, 1)
	for i in n:
		x = int(i)
		(a, b) = (min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1))
	print(a)
main()


N = input()[::-1]
l = len(N)
n0 = int(N[0])
dp = [[0, 0] for i in range(l)]
dp[0] = [n0, 11 - n0]
for (i, n) in enumerate(N[1:], 1):
	n = int(n)
	dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + n)
	dp[i][1] = min(dp[i - 1][0] + 1 + (10 - n), dp[i - 1][1] + (9 - n))
print(min(dp[-1]))


N = list(map(int, list(input())))[::-1]
dp0 = [float('inf')] * (len(N) + 1)
dp1 = [float('inf')] * (len(N) + 1)
dp0[0] = 0
dp1[0] = 1
for i in range(len(N)):
	dp0[i + 1] = min(dp0[i] + N[i], dp1[i] + 10 - N[i])
	dp1[i + 1] = min(dp0[i] + N[i] + 1, dp1[i] + 9 - N[i])
print(dp0[-1])


N = input()
dp0 = 0
dp1 = 1
for n in N:
	n = int(n)
	dp0_new = min(dp0 + n, dp1 + (10 - n))
	n = n + 1
	dp1_new = min(dp0 + n, dp1 + (10 - n))
	(dp0, dp1) = (dp0_new, dp1_new)
print(dp0)


N_str = input()
ln = len(N_str)
ans = 0
(no_mu, move_up) = (int(N_str[-1]), 11 - int(N_str[-1]))
for i in range(2, ln + 1):
	n = int(N_str[-i])
	(no_mu, move_up) = (n + min(no_mu, move_up), min(11 - n + no_mu, 9 - n + move_up))
print(min(no_mu, move_up))


n = list(reversed(list(map(int, input()))))
k = len(n)
(dp1, dp2) = ([0] * k, [0] * k)
(dp1[0], dp2[0]) = (n[0], 11 - n[0])
for (i, x) in enumerate(n[1:], 1):
	dp1[i] = min(x + dp1[i - 1], x + dp2[i - 1])
	dp2[i] = min(11 - x + dp1[i - 1], 9 - x + dp2[i - 1])
print(min(dp1[-1], dp2[-1]))


N = input()
N = '0' + N
M = len(N)
INF = 10 ** 12
DP1 = [INF] * (M + 1)
DP2 = [INF] * (M + 1)
DP1[0] = 0
for i in range(M):
	DP1[i + 1] = min(DP1[i] + int(N[i]), DP2[i] + 10 - int(N[i]))
	DP2[i + 1] = min(DP1[i] + int(N[i]) + 1, DP2[i] + 9 - int(N[i]))
print(min(DP1[M], DP2[M] + 1))


S = input()
dp = [0, 1]
for s in S:
	i = int(s)
	a = min(dp[0] + i, dp[1] + 10 - i)
	b = min(dp[0] + i + 1, dp[1] + 10 - (i + 1))
	dp = [a, b]
print(dp[0])


s = input()
n = len(s)
ans = 0
dp1 = [0] * (n + 1)
dp0 = [0] * (n + 1)
dp1[0] = 1
for i in range(n):
	k = int(s[i])
	if k < 5:
		dp0[i + 1] = dp0[i] + k
	elif k > 5:
		dp0[i + 1] = dp1[i] + 10 - k
	elif i == 0:
		dp0[i + 1] = dp0[i] + k
	elif int(s[i - 1]) < 5:
		dp0[i + 1] = dp0[i] + k
	else:
		dp0[i + 1] = dp1[i] + 10 - k
	k += 1
	if k == 10:
		dp1[i + 1] = dp1[i]
	elif k < 5:
		dp1[i + 1] = dp0[i] + k
	elif k > 5:
		dp1[i + 1] = dp1[i] + 10 - k
	elif i == 0:
		dp1[i + 1] = dp0[i] + k
	elif int(s[i - 1]) < 5:
		dp1[i + 1] = dp0[i] + k
	else:
		dp1[i + 1] = dp1[i] + 10 - k
print(dp0[n])


N = list(map(int, input()))
L = len(N)
ans0 = 0
ans1 = 100
for i in range(L):
	k = N[-i - 1]
	(ans0, ans1) = (min(ans0 + k, ans1 + k + 1), min(ans0 + 10 - k, ans1 + 9 - k))
ans = min(ans0, ans1 + 1)
print(ans)


S = input()
B0 = int(S[-1])
B1 = 10 - int(S[-1])
for s in S[::-1][1:]:
	x = int(s)
	(B0, B1) = (min(B0 + x, B1 + x + 1), min(B0 + (10 - x), B1 + (10 - x - 1)))
print(min(B0, B1 + 1))


def main():
	N = input()
	p = int(N[0])
	o = 11 - int(N[0])
	for i in N[1:]:
		j = int(i)
		np = min(p + j, o + j)
		no = min(o + 9 - j, p + 11 - j)
		p = np
		o = no
	print(min(p, o))
main()


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 7)
	from collections import Counter, deque
	from itertools import combinations, permutations, accumulate, groupby, product
	from bisect import bisect_left, bisect_right
	from heapq import heapify, heappop, heappush
	import math
	n = list(input().rstrip())
	n = [0] + n
	n = n[::-1]
	ln = len(n)
	res = 0
	a = 0
	for i in range(ln):
		s = int(n[i])
		s += a
		if 0 <= s <= 4:
			res += s
			a = 0
		elif 6 <= s:
			res += 10 - s
			a = 1
		elif int(n[i + 1]) >= 5:
			res += 10 - s
			a = 1
		else:
			res += s
			a = 0
	print(res)

def __starting_point():
	main()
__starting_point()


N = input()
L = len(N)
DP = [[N] * 2 for _ in range(L + 1)]
DP[0][0] = 0
DP[0][1] = 1
for (i, n) in enumerate(N, 1):
	DP[i][0] = min(DP[i - 1][0] + int(n), DP[i - 1][1] + 10 - int(n))
	DP[i][1] = min(DP[i - 1][0] + int(n) + 1, DP[i - 1][1] + 9 - int(n))
print(DP[L][0])


s = input()
l = len(s)
cnt = 0
A = []
A.append(0)
for i in range(l):
	A.append(int(s[i]))
for i in reversed(range(l + 1)):
	if A[i] < 5 or (A[i] == 5 and A[i - 1] < 5):
		cnt += A[i]
	else:
		A[i - 1] += 1
		cnt += 10 - A[i]
		A[i] = 0
print(cnt)


n = input()
m = list(n)
l = [int(i) for i in m]
l.reverse()
l.append(0)
k = l
ans = 0
for i in range(len(l)):
	if k[i] < 5:
		ans += k[i]
	elif k[i] == 5:
		if k[i + 1] >= 5:
			k[i + 1] += 1
		ans += 5
	else:
		ans += 10 - k[i]
		k[i + 1] += 1
print(ans)


s = list(map(int, input()[::-1]))
t = [[0, 0] for i in range(len(s) + 1)]
t[0] = [0, 9]
for i in range(len(s)):
	t[i + 1][0] = min(t[i][0], t[i][1]) + s[i]
	t[i + 1][1] = min(11 - s[i] + t[i][0], 9 - s[i] + t[i][1])
print(min(t[-1]))


def main():
	s = input()
	dp = [0, 1]
	for c in s:
		x = int(c)
		a = dp[0] + x
		if a > dp[1] + 10 - x:
			a = dp[1] + 10 - x
		b = dp[0] + x + 1
		if b > dp[1] + 10 - x - 1:
			b = dp[1] + 10 - x - 1
		dp[0] = a
		dp[1] = b
	dp[1] += 1
	print(min(dp))

def __starting_point():
	main()
__starting_point()


N = input()
(a, b) = (0, 1)
for i in N:
	x = int(i)
	(a, b) = (min(a + x, b + 10 - x), min(a + x + 1, b + 9 - x))
print(a)


INF = float('inf')

def main():
	n = list(map(int, list(input())))
	n.reverse()
	n.append(0)
	N = len(n) - 1
	DP = [[INF, INF] for i in range(N + 2)]
	DP[-1][1] = 0
	for i in range(N + 1):
		DP[i][0] = min(DP[i - 1][0] + (9 - n[i]), DP[i - 1][1] + (10 - n[i]))
		DP[i][1] = min(DP[i - 1][0] + n[i] + 1, DP[i - 1][1] + n[i])
	ans = min(DP[-2])
	print(ans)

def __starting_point():
	main()
__starting_point()


n = input()
nl = [0]
for i in range(len(n)):
	nl.append(int(n[i]))
y = 0
for i in range(1, len(nl) + 1):
	if nl[-i] <= 4 or (nl[-i] == 5 and nl[-(i + 1)] <= 4):
		y += nl[-i]
	else:
		y += 10 - nl[-i]
		nl[-(i + 1)] += 1
print(y)


s = input()
s = s[::-1]
s += '0'
ans = 0
dp = [0, 10 ** 9]
for c in s:
	i = int(c)
	a = dp[0] + i
	if i < 9 and a > dp[1] + i + 1:
		a = dp[1] + i + 1
	b = dp[1] + 9 - i
	if i > 0 and b > dp[0] + 10 - i:
		b = dp[0] + 10 - i
	dp = [a, b]
print(dp[0])


s = input()
l = len(s)
INF = float('inf')
dp = [[INF] * 2 for _ in range(l + 1)]
dp[0][0] = 0
dp[0][1] = 1
for i in range(l):
	num = int(s[i])
	dp0 = dp[i][0]
	dp1 = dp[i][1]
	dp[i + 1][0] = min(dp0 + num, dp1 + min(10 - num, num + 1))
	dp[i + 1][1] = min(dp0 + num + 1, dp1 + 10 - num - 1)
print(dp[-1][0])


n = input()
a = n[::-1] + '0'
prev_a = int(a[0])
prev_b = 10 - int(a[0])
for i in range(1, len(a)):
	x = int(a[i])
	y = x + 1
	aa = min([x + prev_a, y + prev_b])
	bb = min([10 - x + prev_a, 10 - y + prev_b])
	prev_a = aa
	prev_b = bb
print(min(aa, bb))