S = input()
N = list(map(int,S))
L = len(N)

dp = [[0]*2 for i in [0]*(L+1)]
dp[0][1] = 1

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

print(dp[L][0])

 S = input() S input() input N = list(map(int,S)) N list(map(int,S)) list map(int,S) map int S L = len(N) L len(N) len N [0]*2 for i in [0]*(L+1) for i in [0]*(L+1) i [0]*(L+1) [0] 0 L+1 L 1 for i in [0]*(L+1) [0]*2 [0] 0 2 dp = [[0]*2 for i in [0]*(L+1)] dp [[0]*2 for i in [0]*(L+1)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(L):
    n = N[i]
    dp[i+1][0] = min(dp[i][0] + n, dp[i][1] + 10-n)
    if n == 9:
        dp[i+1][1] = dp[i][1]
    else:
        dp[i+1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n+1)) i range(L) range L n = N[i] n N[i] N i dp[i+1][0] = min(dp[i][0] + n, dp[i][1] + 10-n) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + n, dp[i][1] + 10-n) min dp[i][0] + n dp[i][0] [i] dp i 0 n dp[i][1] + 10-n dp[i][1] + 10 dp[i][1] [i] dp i 1 10 n if n == 9:
        dp[i+1][1] = dp[i][1]
    else:
        dp[i+1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n+1)) n == 9 n 9 dp[i+1][1] = dp[i][1] dp[i+1][1] [i+1] dp i+1 i 1 1 dp[i][1] [i] dp i 1 dp[i+1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - (n+1)) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + n + 1, dp[i][1] + 10 - (n+1)) min dp[i][0] + n + 1 dp[i][0] + n dp[i][0] [i] dp i 0 n 1 dp[i][1] + 10 - (n+1) dp[i][1] + 10 dp[i][1] [i] dp i 1 10 n+1 n 1 print(dp[L][0]) print dp[L][0] [L] dp L 0 N = list(map(int,S)) list(map(int,S)) N L = len(N) len(N) L dp = [[0]*2 for i in [0]*(L+1)] [[0]*2 for i in [0]*(L+1)] dp n = N[i] N[i] n S = input() input() S
S = input()
N = list(map(int,S))
L = len(N)

dp = [[0]*2 for i in [0]*(L+1)]
dp[0][1] = 1

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

print(dp[L][0])


