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