#!/usr/bin/env python3
a = [0] + list(map(int, list(input())))
n = len(a)
dp = [[10 ** 9] * 2 for _ in range(n + 1)]
dp[0][0] = 0
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] + 10 - a[i] - 1)
print(dp[n][0]) a = [0] + list(map(int, list(input()))) a [0] + list(map(int, list(input()))) [0] 0 list(map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input n = len(a) n len(a) len a [10 ** 9] * 2 for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) [10 ** 9] * 2 [10 ** 9] 10 ** 9 10 9 2 dp = [[10 ** 9] * 2 for _ in range(n + 1)] dp [[10 ** 9] * 2 for _ in range(n + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 dp[0][1] = 1 dp[0][1] [0] 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] + 10 - a[i] - 1) i range(n) range n dp[i + 1][0] = min(dp[i][0] + a[i], dp[i][1] + 10 - a[i]) dp[i + 1][0] [i + 1] dp i + 1 i 1 0 min(dp[i][0] + a[i], dp[i][1] + 10 - a[i]) min dp[i][0] + a[i] dp[i][0] [i] dp i 0 a[i] a i dp[i][1] + 10 - a[i] dp[i][1] + 10 dp[i][1] [i] dp i 1 10 a[i] a i dp[i + 1][1] = min(dp[i][0] + a[i] + 1, dp[i][1] + 10 - a[i] - 1) dp[i + 1][1] [i + 1] dp i + 1 i 1 1 min(dp[i][0] + a[i] + 1, dp[i][1] + 10 - a[i] - 1) min dp[i][0] + a[i] + 1 dp[i][0] + a[i] dp[i][0] [i] dp i 0 a[i] a i 1 dp[i][1] + 10 - a[i] - 1 dp[i][1] + 10 - a[i] dp[i][1] + 10 dp[i][1] [i] dp i 1 10 a[i] a i 1 print(dp[n][0]) print dp[n][0] [n] dp n 0 a = [0] + list(map(int, list(input()))) [0] + list(map(int, list(input()))) a dp = [[10 ** 9] * 2 for _ in range(n + 1)] [[10 ** 9] * 2 for _ in range(n + 1)] dp n = len(a) len(a) n
#!/usr/bin/env python3
a = [0] + list(map(int, list(input())))
n = len(a)
dp = [[10 ** 9] * 2 for _ in range(n + 1)]
dp[0][0] = 0
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] + 10 - a[i] - 1)
print(dp[n][0])
