N = list(map(int, list(input())))

dp=[[-1,-1] for i in range(len(N)+1)]
dp[0]=[0,1]
b=0
N.insert(0,0)

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

print(dp[-1][0]) N = list(map(int, list(input()))) N list(map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input [-1,-1] for i in range(len(N)+1) for i in range(len(N)+1) i range(len(N)+1) range len(N)+1 len(N) len N 1 for i in range(len(N)+1) [-1,-1] -1 -1 dp=[[-1,-1] for i in range(len(N)+1)] dp [[-1,-1] for i in range(len(N)+1)] dp[0]=[0,1] dp[0] dp 0 [0,1] 0 1 b=0 b 0 N.insert(0,0) N.insert N insert 0 0 for i in range(1,len(N)):
    up = 10-N[i]
    dp[i][0]=min(dp[i-1][0]+N[i],dp[i-1][1]+up)
    dp[i][1] = min(dp[i-1][0]+N[i]+1, dp[i-1][1]+up-1)
    i range(1,len(N)) range 1 len(N) len N up = 10-N[i] up 10-N[i] 10 N[i] N i dp[i][0]=min(dp[i-1][0]+N[i],dp[i-1][1]+up) dp[i][0] [i] dp i 0 min(dp[i-1][0]+N[i],dp[i-1][1]+up) min dp[i-1][0]+N[i] dp[i-1][0] [i-1] dp i-1 i 1 0 N[i] N i dp[i-1][1]+up dp[i-1][1] [i-1] dp i-1 i 1 1 up dp[i][1] = min(dp[i-1][0]+N[i]+1, dp[i-1][1]+up-1) dp[i][1] [i] dp i 1 min(dp[i-1][0]+N[i]+1, dp[i-1][1]+up-1) min dp[i-1][0]+N[i]+1 dp[i-1][0]+N[i] dp[i-1][0] [i-1] dp i-1 i 1 0 N[i] N i 1 dp[i-1][1]+up-1 dp[i-1][1]+up dp[i-1][1] [i-1] dp i-1 i 1 1 up 1 print(dp[-1][0]) print dp[-1][0] [-1] dp -1 0 b=0 0 b dp=[[-1,-1] for i in range(len(N)+1)] [[-1,-1] for i in range(len(N)+1)] dp N = list(map(int, list(input()))) list(map(int, list(input()))) N up = 10-N[i] 10-N[i] up
N = list(map(int, list(input())))

dp=[[-1,-1] for i in range(len(N)+1)]
dp[0]=[0,1]
b=0
N.insert(0,0)

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

print(dp[-1][0])
