N=[int(c) for c in input()][::-1]

dp=[[0]*2 for _ in range(len(N)+1)]
dp[0][1]=1

for i in range(len(N)):
    dp[i+1][1]=min(dp[i][0]+N[i]+1,dp[i][1]+10-N[i]-1)
    dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+10-N[i])

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

dp=[[0]*2 for _ in range(len(N)+1)]
dp[0][1]=1

for i in range(len(N)):
    dp[i+1][1]=min(dp[i][0]+N[i]+1,dp[i][1]+10-N[i]-1)
    dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+10-N[i])

print(dp[len(N)][0])