N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
ans[0][0]=int(N[L-1])
ans[0][1]=10-int(N[L-1])
if L>1:
 for i in range(1,L):
  ans[i][0]=min(ans[i-1][0]+int(N[L-1-i]),ans[i-1][1]+int(N[L-1-i])+1)
  ans[i][1]=min(ans[i-1][0]+10-int(N[L-1-i]),ans[i-1][1]+10-int(N[L-1-i])-1)
print(min(ans[L-1][0],ans[L-1][1]+1)) N=input() N input() input L=len(N) L len(N) len N [0 for e in range(2)] for f in range(L) for f in range(L) f range(L) range L for f in range(L) [0 for e in range(2)] ans=[[0 for e in range(2)] for f in range(L)] ans [[0 for e in range(2)] for f in range(L)] ans[0][0]=int(N[L-1]) ans[0][0] [0] ans 0 0 int(N[L-1]) int N[L-1] N L-1 L 1 ans[0][1]=10-int(N[L-1]) ans[0][1] [0] ans 0 1 10-int(N[L-1]) 10 int(N[L-1]) int N[L-1] N L-1 L 1 if L>1:
 for i in range(1,L):
  ans[i][0]=min(ans[i-1][0]+int(N[L-1-i]),ans[i-1][1]+int(N[L-1-i])+1)
  ans[i][1]=min(ans[i-1][0]+10-int(N[L-1-i]),ans[i-1][1]+10-int(N[L-1-i])-1) L>1 L 1 for i in range(1,L):
  ans[i][0]=min(ans[i-1][0]+int(N[L-1-i]),ans[i-1][1]+int(N[L-1-i])+1)
  ans[i][1]=min(ans[i-1][0]+10-int(N[L-1-i]),ans[i-1][1]+10-int(N[L-1-i])-1) i range(1,L) range 1 L ans[i][0]=min(ans[i-1][0]+int(N[L-1-i]),ans[i-1][1]+int(N[L-1-i])+1) ans[i][0] [i] ans i 0 min(ans[i-1][0]+int(N[L-1-i]),ans[i-1][1]+int(N[L-1-i])+1) min ans[i-1][0]+int(N[L-1-i]) ans[i-1][0] [i-1] ans i-1 i 1 0 int(N[L-1-i]) int N[L-1-i] N L-1-i L-1 L 1 i ans[i-1][1]+int(N[L-1-i])+1 ans[i-1][1]+int(N[L-1-i]) ans[i-1][1] [i-1] ans i-1 i 1 1 int(N[L-1-i]) int N[L-1-i] N L-1-i L-1 L 1 i 1 ans[i][1]=min(ans[i-1][0]+10-int(N[L-1-i]),ans[i-1][1]+10-int(N[L-1-i])-1) ans[i][1] [i] ans i 1 min(ans[i-1][0]+10-int(N[L-1-i]),ans[i-1][1]+10-int(N[L-1-i])-1) min ans[i-1][0]+10-int(N[L-1-i]) ans[i-1][0]+10 ans[i-1][0] [i-1] ans i-1 i 1 0 10 int(N[L-1-i]) int N[L-1-i] N L-1-i L-1 L 1 i ans[i-1][1]+10-int(N[L-1-i])-1 ans[i-1][1]+10-int(N[L-1-i]) ans[i-1][1]+10 ans[i-1][1] [i-1] ans i-1 i 1 1 10 int(N[L-1-i]) int N[L-1-i] N L-1-i L-1 L 1 i 1 print(min(ans[L-1][0],ans[L-1][1]+1)) print min(ans[L-1][0],ans[L-1][1]+1) min ans[L-1][0] [L-1] ans L-1 L 1 0 ans[L-1][1]+1 ans[L-1][1] [L-1] ans L-1 L 1 1 1 ans=[[0 for e in range(2)] for f in range(L)] [[0 for e in range(2)] for f in range(L)] ans L=len(N) len(N) L N=input() input() N
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
ans[0][0]=int(N[L-1])
ans[0][1]=10-int(N[L-1])
if L>1:
 for i in range(1,L):
  ans[i][0]=min(ans[i-1][0]+int(N[L-1-i]),ans[i-1][1]+int(N[L-1-i])+1)
  ans[i][1]=min(ans[i-1][0]+10-int(N[L-1-i]),ans[i-1][1]+10-int(N[L-1-i])-1)
print(min(ans[L-1][0],ans[L-1][1]+1))