S = input().strip()
INFTY=10**8
dp = [[INFTY for _ in range(2)] for _ in range(len(S))]
for k in range(10):
    n = int(S[-1])
    if k>= n:
        dp[0][0] = min(dp[0][0],2*k-n)
    else:
        dp[0][1] = min(dp[0][1],2*k-n+10)
for i in range(1,len(S)):
    n = int(S[-(1+i)])
    for k in range(10):
        if k>n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1])
        elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
        elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1])
print(min(dp[len(S)-1][0],dp[len(S)-1][1]+1)) S = input().strip() S input().strip() ().strip () input strip INFTY=10**8 INFTY 10**8 10 8 [INFTY for _ in range(2)] for _ in range(len(S)) for _ in range(len(S)) _ range(len(S)) range len(S) len S for _ in range(len(S)) [INFTY for _ in range(2)] dp = [[INFTY for _ in range(2)] for _ in range(len(S))] dp [[INFTY for _ in range(2)] for _ in range(len(S))] for k in range(10):
    n = int(S[-1])
    if k>= n:
        dp[0][0] = min(dp[0][0],2*k-n)
    else:
        dp[0][1] = min(dp[0][1],2*k-n+10) k range(10) range 10 n = int(S[-1]) n int(S[-1]) int S[-1] S -1 if k>= n:
        dp[0][0] = min(dp[0][0],2*k-n)
    else:
        dp[0][1] = min(dp[0][1],2*k-n+10) k>= n k n dp[0][0] = min(dp[0][0],2*k-n) dp[0][0] [0] dp 0 0 min(dp[0][0],2*k-n) min dp[0][0] [0] dp 0 0 2*k-n 2*k 2 k n dp[0][1] = min(dp[0][1],2*k-n+10) dp[0][1] [0] dp 0 1 min(dp[0][1],2*k-n+10) min dp[0][1] [0] dp 0 1 2*k-n+10 2*k-n 2*k 2 k n 10 for i in range(1,len(S)):
    n = int(S[-(1+i)])
    for k in range(10):
        if k>n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1])
        elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
        elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1]) i range(1,len(S)) range 1 len(S) len S n = int(S[-(1+i)]) n int(S[-(1+i)]) int S[-(1+i)] S -(1+i) 1+i 1 i for k in range(10):
        if k>n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1])
        elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
        elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1]) k range(10) range 10 if k>n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1])
        elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
        elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1]) k>n k n dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1]) dp[i][0] [i] dp i 0 min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1]) min dp[i][0] [i] dp i 0 2*k-n+dp[i-1][0] 2*k-n 2*k 2 k n dp[i-1][0] [i-1] dp i-1 i 1 0 k+(k-1)-n+dp[i-1][1] k+(k-1)-n k+(k-1) k k-1 k 1 n dp[i-1][1] [i-1] dp i-1 i 1 1 elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
         k==n k n dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0]) dp[i][0] [i] dp i 0 min(dp[i][0],2*k-n+dp[i-1][0]) min dp[i][0] [i] dp i 0 2*k-n+dp[i-1][0] 2*k-n 2*k 2 k n dp[i-1][0] [i-1] dp i-1 i 1 0 if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
         n==0 n 0 dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1]) dp[i][1] [i] dp i 1 min(dp[i][1],k+(k-1)%10-n+dp[i-1][1]) min dp[i][1] [i] dp i 1 k+(k-1)%10-n+dp[i-1][1] k+(k-1)%10-n k+(k-1)%10 k (k-1)%10 k-1 k 1 10 n dp[i-1][1] [i-1] dp i-1 i 1 1 dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1]) dp[i][1] [i] dp i 1 min(dp[i][1],k+10-n+k-1+dp[i-1][1]) min dp[i][1] [i] dp i 1 k+10-n+k-1+dp[i-1][1] k+10-n+k-1 k+10-n+k k+10-n k+10 k 10 n k 1 dp[i-1][1] [i-1] dp i-1 i 1 1 elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1]) k<n k n dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1]) dp[i][1] [i] dp i 1 min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1]) min dp[i][1] [i] dp i 1 k+10-n+k+dp[i-1][0] k+10-n+k k+10-n k+10 k 10 n k dp[i-1][0] [i-1] dp i-1 i 1 0 k+10-n+k-1+dp[i-1][1] k+10-n+k-1 k+10-n+k k+10-n k+10 k 10 n k 1 dp[i-1][1] [i-1] dp i-1 i 1 1 print(min(dp[len(S)-1][0],dp[len(S)-1][1]+1)) print min(dp[len(S)-1][0],dp[len(S)-1][1]+1) min dp[len(S)-1][0] [len(S)-1] dp len(S)-1 len(S) len S 1 0 dp[len(S)-1][1]+1 dp[len(S)-1][1] [len(S)-1] dp len(S)-1 len(S) len S 1 1 1 INFTY=10**8 10**8 INFTY dp = [[INFTY for _ in range(2)] for _ in range(len(S))] [[INFTY for _ in range(2)] for _ in range(len(S))] dp S = input().strip() input().strip() S n = int(S[-(1+i)]) int(S[-(1+i)]) n n = int(S[-1]) int(S[-1]) n
S = input().strip()
INFTY=10**8
dp = [[INFTY for _ in range(2)] for _ in range(len(S))]
for k in range(10):
    n = int(S[-1])
    if k>= n:
        dp[0][0] = min(dp[0][0],2*k-n)
    else:
        dp[0][1] = min(dp[0][1],2*k-n+10)
for i in range(1,len(S)):
    n = int(S[-(1+i)])
    for k in range(10):
        if k>n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0],k+(k-1)-n+dp[i-1][1])
        elif k==n:
            dp[i][0] = min(dp[i][0],2*k-n+dp[i-1][0])
            if n==0:
                dp[i][1] = min(dp[i][1],k+(k-1)%10-n+dp[i-1][1])
            else:
                dp[i][1] = min(dp[i][1],k+10-n+k-1+dp[i-1][1])
        elif k<n:
            dp[i][1] = min(dp[i][1],k+10-n+k+dp[i-1][0],k+10-n+k-1+dp[i-1][1])
print(min(dp[len(S)-1][0],dp[len(S)-1][1]+1))