N="0"+input()
N=N[::-1]
DP=[[0,0]for i in range(len(N))]

DP[0][0]=int(N[0])
DP[0][1]=10-int(N[0])

#print(DP)
#print(N)
for i in range(1,len(N)):
    DP[i][0]=min(DP[i-1][0]+int(N[i]),DP[i-1][1]+int(N[i])+1)
    DP[i][1]=min(DP[i-1][0]+10-int(N[i]),DP[i-1][1]+10-(int(N[i])+1))
#print(DP)
print(min(DP[-1])) N="0"+input() N "0"+input() "0" input() input N=N[::-1] N N[::-1] N ::-1 -1 [0,0]for i in range(len(N)) for i in range(len(N)) i range(len(N)) range len(N) len N for i in range(len(N)) [0,0] 0 0 DP=[[0,0]for i in range(len(N))] DP [[0,0]for i in range(len(N))] DP[0][0]=int(N[0]) DP[0][0] [0] DP 0 0 int(N[0]) int N[0] N 0 DP[0][1]=10-int(N[0]) DP[0][1] [0] DP 0 1 10-int(N[0]) 10 int(N[0]) int N[0] N 0 for i in range(1,len(N)):
    DP[i][0]=min(DP[i-1][0]+int(N[i]),DP[i-1][1]+int(N[i])+1)
    DP[i][1]=min(DP[i-1][0]+10-int(N[i]),DP[i-1][1]+10-(int(N[i])+1))
#print(DP) i range(1,len(N)) range 1 len(N) len N DP[i][0]=min(DP[i-1][0]+int(N[i]),DP[i-1][1]+int(N[i])+1) DP[i][0] [i] DP i 0 min(DP[i-1][0]+int(N[i]),DP[i-1][1]+int(N[i])+1) min DP[i-1][0]+int(N[i]) DP[i-1][0] [i-1] DP i-1 i 1 0 int(N[i]) int N[i] N i DP[i-1][1]+int(N[i])+1 DP[i-1][1]+int(N[i]) DP[i-1][1] [i-1] DP i-1 i 1 1 int(N[i]) int N[i] N i 1 DP[i][1]=min(DP[i-1][0]+10-int(N[i]),DP[i-1][1]+10-(int(N[i])+1)) DP[i][1] [i] DP i 1 min(DP[i-1][0]+10-int(N[i]),DP[i-1][1]+10-(int(N[i])+1)) min DP[i-1][0]+10-int(N[i]) DP[i-1][0]+10 DP[i-1][0] [i-1] DP i-1 i 1 0 10 int(N[i]) int N[i] N i DP[i-1][1]+10-(int(N[i])+1) DP[i-1][1]+10 DP[i-1][1] [i-1] DP i-1 i 1 1 10 int(N[i])+1 int(N[i]) int N[i] N i 1 print(min(DP[-1])) print min(DP[-1]) min DP[-1] DP -1 N="0"+input() "0"+input() N N=N[::-1] N[::-1] N DP=[[0,0]for i in range(len(N))] [[0,0]for i in range(len(N))] DP
N="0"+input()
N=N[::-1]
DP=[[0,0]for i in range(len(N))]

DP[0][0]=int(N[0])
DP[0][1]=10-int(N[0])

#print(DP)
#print(N)
for i in range(1,len(N)):
    DP[i][0]=min(DP[i-1][0]+int(N[i]),DP[i-1][1]+int(N[i])+1)
    DP[i][1]=min(DP[i-1][0]+10-int(N[i]),DP[i-1][1]+10-(int(N[i])+1))
#print(DP)
print(min(DP[-1]))