s = "0" + input()
n = len(s)
S = []
for i in range(n-1,-1,-1):
    S.append(int(s[i]))

ans = sum(S)
ans0 = ans
D = [[0] * 3 for i in range(n)]
D[0] = [S[0], S[0]+1, 10-S[0]]
for i in range(1, n):
    D[i][0] = min(D[i-1][0], D[i-1][1]) + S[i]
    D[i][1] = D[i-1][2] + S[i] + 1
    D[i][2] = min(D[i-1][0]+10-S[i], D[i-1][1]+10-S[i], D[i-1][2]+9-S[i])

# print(D)
print(min(D[-1][:2])) s = "0" + input() s "0" + input() "0" input() input n = len(s) n len(s) len s S = [] S [] for i in range(n-1,-1,-1):
    S.append(int(s[i])) i range(n-1,-1,-1) range n-1 n 1 -1 -1 S.append(int(s[i])) S.append S append int(s[i]) int s[i] s i ans = sum(S) ans sum(S) sum S ans0 = ans ans0 ans [0] * 3 for i in range(n) for i in range(n) i range(n) range n for i in range(n) [0] * 3 [0] 0 3 D = [[0] * 3 for i in range(n)] D [[0] * 3 for i in range(n)] D[0] = [S[0], S[0]+1, 10-S[0]] D[0] D 0 [S[0], S[0]+1, 10-S[0]] S[0] S 0 S[0]+1 S[0] S 0 1 10-S[0] 10 S[0] S 0 for i in range(1, n):
    D[i][0] = min(D[i-1][0], D[i-1][1]) + S[i]
    D[i][1] = D[i-1][2] + S[i] + 1
    D[i][2] = min(D[i-1][0]+10-S[i], D[i-1][1]+10-S[i], D[i-1][2]+9-S[i])

# print(D) i range(1, n) range 1 n D[i][0] = min(D[i-1][0], D[i-1][1]) + S[i] D[i][0] [i] D i 0 min(D[i-1][0], D[i-1][1]) + S[i] min(D[i-1][0], D[i-1][1]) min D[i-1][0] [i-1] D i-1 i 1 0 D[i-1][1] [i-1] D i-1 i 1 1 S[i] S i D[i][1] = D[i-1][2] + S[i] + 1 D[i][1] [i] D i 1 D[i-1][2] + S[i] + 1 D[i-1][2] + S[i] D[i-1][2] [i-1] D i-1 i 1 2 S[i] S i 1 D[i][2] = min(D[i-1][0]+10-S[i], D[i-1][1]+10-S[i], D[i-1][2]+9-S[i]) D[i][2] [i] D i 2 min(D[i-1][0]+10-S[i], D[i-1][1]+10-S[i], D[i-1][2]+9-S[i]) min D[i-1][0]+10-S[i] D[i-1][0]+10 D[i-1][0] [i-1] D i-1 i 1 0 10 S[i] S i D[i-1][1]+10-S[i] D[i-1][1]+10 D[i-1][1] [i-1] D i-1 i 1 1 10 S[i] S i D[i-1][2]+9-S[i] D[i-1][2]+9 D[i-1][2] [i-1] D i-1 i 1 2 9 S[i] S i print(min(D[-1][:2])) print min(D[-1][:2]) min D[-1][:2] [-1] D -1 :2 2 n = len(s) len(s) n D = [[0] * 3 for i in range(n)] [[0] * 3 for i in range(n)] D ans = sum(S) sum(S) ans S = [] [] S s = "0" + input() "0" + input() s ans0 = ans ans ans0
s = "0" + input()
n = len(s)
S = []
for i in range(n-1,-1,-1):
    S.append(int(s[i]))

ans = sum(S)
ans0 = ans
D = [[0] * 3 for i in range(n)]
D[0] = [S[0], S[0]+1, 10-S[0]]
for i in range(1, n):
    D[i][0] = min(D[i-1][0], D[i-1][1]) + S[i]
    D[i][1] = D[i-1][2] + S[i] + 1
    D[i][2] = min(D[i-1][0]+10-S[i], D[i-1][1]+10-S[i], D[i-1][2]+9-S[i])

# print(D)
print(min(D[-1][:2]))
