N = input()
L=len(N)

DP=[[N]*2 for _ in range(L+1)]
DP[0][0]=0
DP[0][1]=1

for i,n in enumerate(N,1):
    DP[i][0]= min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n))
    DP[i][1] = min(DP[i-1][0]+int(n)+1,DP[i-1][1]+9-int(n))

print(DP[L][0]) N = input() N input() input L=len(N) L len(N) len N [N]*2 for _ in range(L+1) for _ in range(L+1) _ range(L+1) range L+1 L 1 for _ in range(L+1) [N]*2 [N] N 2 DP=[[N]*2 for _ in range(L+1)] DP [[N]*2 for _ in range(L+1)] DP[0][0]=0 DP[0][0] [0] DP 0 0 0 DP[0][1]=1 DP[0][1] [0] DP 0 1 1 for i,n in enumerate(N,1):
    DP[i][0]= min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n))
    DP[i][1] = min(DP[i-1][0]+int(n)+1,DP[i-1][1]+9-int(n)) i n enumerate(N,1) enumerate N 1 DP[i][0]= min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n)) DP[i][0] [i] DP i 0 min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n)) min DP[i-1][0]+int(n) DP[i-1][0] [i-1] DP i-1 i 1 0 int(n) int n DP[i-1][1]+10-int(n) DP[i-1][1]+10 DP[i-1][1] [i-1] DP i-1 i 1 1 10 int(n) int n DP[i][1] = min(DP[i-1][0]+int(n)+1,DP[i-1][1]+9-int(n)) DP[i][1] [i] DP i 1 min(DP[i-1][0]+int(n)+1,DP[i-1][1]+9-int(n)) min DP[i-1][0]+int(n)+1 DP[i-1][0]+int(n) DP[i-1][0] [i-1] DP i-1 i 1 0 int(n) int n 1 DP[i-1][1]+9-int(n) DP[i-1][1]+9 DP[i-1][1] [i-1] DP i-1 i 1 1 9 int(n) int n print(DP[L][0]) print DP[L][0] [L] DP L 0 L=len(N) len(N) L N = input() input() N DP=[[N]*2 for _ in range(L+1)] [[N]*2 for _ in range(L+1)] DP
N = input()
L=len(N)

DP=[[N]*2 for _ in range(L+1)]
DP[0][0]=0
DP[0][1]=1

for i,n in enumerate(N,1):
    DP[i][0]= min(DP[i-1][0]+int(n),DP[i-1][1]+10-int(n))
    DP[i][1] = min(DP[i-1][0]+int(n)+1,DP[i-1][1]+9-int(n))

print(DP[L][0])
