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