s=list(map(int,input()[::-1]))
t=[[0,0]for i in range(len(s)+1)]
t[0]=[0,9]
for i in range(len(s)):
    t[i+1][0]=min(t[i][0],t[i][1])+s[i]
    t[i+1][1]=min(11-s[i]+t[i][0],9-s[i]+t[i][1])
print(min(t[-1])) s=list(map(int,input()[::-1])) s list(map(int,input()[::-1])) list map(int,input()[::-1]) map int input()[::-1] () input ::-1 -1 [0,0]for i in range(len(s)+1) for i in range(len(s)+1) i range(len(s)+1) range len(s)+1 len(s) len s 1 for i in range(len(s)+1) [0,0] 0 0 t=[[0,0]for i in range(len(s)+1)] t [[0,0]for i in range(len(s)+1)] t[0]=[0,9] t[0] t 0 [0,9] 0 9 for i in range(len(s)):
    t[i+1][0]=min(t[i][0],t[i][1])+s[i]
    t[i+1][1]=min(11-s[i]+t[i][0],9-s[i]+t[i][1]) i range(len(s)) range len(s) len s t[i+1][0]=min(t[i][0],t[i][1])+s[i] t[i+1][0] [i+1] t i+1 i 1 0 min(t[i][0],t[i][1])+s[i] min(t[i][0],t[i][1]) min t[i][0] [i] t i 0 t[i][1] [i] t i 1 s[i] s i t[i+1][1]=min(11-s[i]+t[i][0],9-s[i]+t[i][1]) t[i+1][1] [i+1] t i+1 i 1 1 min(11-s[i]+t[i][0],9-s[i]+t[i][1]) min 11-s[i]+t[i][0] 11-s[i] 11 s[i] s i t[i][0] [i] t i 0 9-s[i]+t[i][1] 9-s[i] 9 s[i] s i t[i][1] [i] t i 1 print(min(t[-1])) print min(t[-1]) min t[-1] t -1 t=[[0,0]for i in range(len(s)+1)] [[0,0]for i in range(len(s)+1)] t s=list(map(int,input()[::-1])) list(map(int,input()[::-1])) s
s=list(map(int,input()[::-1]))
t=[[0,0]for i in range(len(s)+1)]
t[0]=[0,9]
for i in range(len(s)):
    t[i+1][0]=min(t[i][0],t[i][1])+s[i]
    t[i+1][1]=min(11-s[i]+t[i][0],9-s[i]+t[i][1])
print(min(t[-1]))