m = input()[::-1]
n = []
for i in range(len(m)):
    n.append(int(m[i]))

dp = [[0, 0] for _ in range(len(n)+1)]

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

s = len(n)
print(min(dp[s][0], (dp[s][1]+1))) m = input()[::-1] m input()[::-1] () input ::-1 -1 n = [] n [] for i in range(len(m)):
    n.append(int(m[i])) i range(len(m)) range len(m) len m n.append(int(m[i])) n.append n append int(m[i]) int m[i] m i [0, 0] for _ in range(len(n)+1) for _ in range(len(n)+1) _ range(len(n)+1) range len(n)+1 len(n) len n 1 for _ in range(len(n)+1) [0, 0] 0 0 dp = [[0, 0] for _ in range(len(n)+1)] dp [[0, 0] for _ in range(len(n)+1)] for i in range(len(n)):
    dp[i+1][0] = min((dp[i][0]+n[i]), (dp[i][1]+n[i]+1))
    if i == 0:
        dp[i+1][1] = dp[i][0]+10-n[i]
    else:
        dp[i+1][1] = min((dp[i][0]+10-n[i]), (dp[i][1]+9-n[i])) i range(len(n)) range len(n) len n dp[i+1][0] = min((dp[i][0]+n[i]), (dp[i][1]+n[i]+1)) dp[i+1][0] [i+1] dp i+1 i 1 0 min((dp[i][0]+n[i]), (dp[i][1]+n[i]+1)) min dp[i][0]+n[i] dp[i][0] [i] dp i 0 n[i] n i dp[i][1]+n[i]+1 dp[i][1]+n[i] dp[i][1] [i] dp i 1 n[i] n i 1 if i == 0:
        dp[i+1][1] = dp[i][0]+10-n[i]
    else:
        dp[i+1][1] = min((dp[i][0]+10-n[i]), (dp[i][1]+9-n[i])) i == 0 i 0 dp[i+1][1] = dp[i][0]+10-n[i] dp[i+1][1] [i+1] dp i+1 i 1 1 dp[i][0]+10-n[i] dp[i][0]+10 dp[i][0] [i] dp i 0 10 n[i] n i dp[i+1][1] = min((dp[i][0]+10-n[i]), (dp[i][1]+9-n[i])) dp[i+1][1] [i+1] dp i+1 i 1 1 min((dp[i][0]+10-n[i]), (dp[i][1]+9-n[i])) min dp[i][0]+10-n[i] dp[i][0]+10 dp[i][0] [i] dp i 0 10 n[i] n i dp[i][1]+9-n[i] dp[i][1]+9 dp[i][1] [i] dp i 1 9 n[i] n i s = len(n) s len(n) len n print(min(dp[s][0], (dp[s][1]+1))) print min(dp[s][0], (dp[s][1]+1)) min dp[s][0] [s] dp s 0 dp[s][1]+1 dp[s][1] [s] dp s 1 1 s = len(n) len(n) s n = [] [] n dp = [[0, 0] for _ in range(len(n)+1)] [[0, 0] for _ in range(len(n)+1)] dp m = input()[::-1] input()[::-1] m
m = input()[::-1]
n = []
for i in range(len(m)):
    n.append(int(m[i]))

dp = [[0, 0] for _ in range(len(n)+1)]

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

s = len(n)
print(min(dp[s][0], (dp[s][1]+1)))