N = list(map(int, input()))

# print(N)
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)

print(DP[0] if DP[0] <= DP[1]+1 else DP[1] +1) N = list(map(int, input())) N list(map(int, input())) list map(int, input()) map int input() input ans = 0 ans 0 DP = [0] *2 DP [0] *2 [0] 0 2 DP[0] = 0 DP[0] DP 0 0 DP[1] = 1 DP[1] 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) i N a, b = DP[0] ,DP[1] a DP[0] DP 0 b DP[1] DP 1 DP[0] = a + i if a+i < b +(10-i) else b +(10-i) DP[0] DP 0 a + i if a+i < b +(10-i) else b +(10-i) a+i < b +(10-i) a+i a i b +(10-i) b 10-i 10 i a + i a i b +(10-i) b 10-i 10 i DP[1] = a + i+1 if a + i+1 < b + (9-i) else b + (9-i) DP[1] DP 1 a + i+1 if a + i+1 < b + (9-i) else b + (9-i) a + i+1 < b + (9-i) a + i+1 a + i a i 1 b + (9-i) b 9-i 9 i a + i+1 a + i a i 1 b + (9-i) b 9-i 9 i print(DP[0] if DP[0] <= DP[1]+1 else DP[1] +1) print DP[0] if DP[0] <= DP[1]+1 else DP[1] +1 DP[0] <= DP[1]+1 DP[0] DP 0 DP[1]+1 DP[1] DP 1 1 DP[0] DP 0 DP[1] +1 DP[1] DP 1 1 b = DP[0] ,DP[1] DP[1] b DP = [0] *2 [0] *2 DP N = list(map(int, input())) list(map(int, input())) N a, b = DP[0] ,DP[1] DP[0] a ans = 0 0 ans
N = list(map(int, input()))

# print(N)
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)

print(DP[0] if DP[0] <= DP[1]+1 else DP[1] +1)