n=input()[::-1]
n+="0"
lt=len(n)
dp=[[float('inf') for _ in range(2)] for j in range(lt+1)]
dp[0][0]=0

for i in range(0,lt):
    for j in range(2):
        x=int(n[i])
        x+=j #繰り上がり処理，あとに+11でもOK
        for num in range(0,10):
            ni = i + 1
            nj = 0
            b=num-x
            if b<0:
                b+=10
                nj=1
            dp[ni][nj]=min(dp[ni][nj],dp[i][j]+num+b)

print(dp[lt][0]) n=input()[::-1] n input()[::-1] () input ::-1 -1 n+="0" n "0" lt=len(n) lt len(n) len n [float('inf') for _ in range(2)] for j in range(lt+1) for j in range(lt+1) j range(lt+1) range lt+1 lt 1 for j in range(lt+1) [float('inf') for _ in range(2)] dp=[[float('inf') for _ in range(2)] for j in range(lt+1)] dp [[float('inf') for _ in range(2)] for j in range(lt+1)] dp[0][0]=0 dp[0][0] [0] dp 0 0 0 for i in range(0,lt):
    for j in range(2):
        x=int(n[i])
        x+=j #繰り上がり処理，あとに+11でもOK
        for num in range(0,10):
            ni = i + 1
            nj = 0
            b=num-x
            if b<0:
                b+=10
                nj=1
            dp[ni][nj]=min(dp[ni][nj],dp[i][j]+num+b) i range(0,lt) range 0 lt for j in range(2):
        x=int(n[i])
        x+=j #繰り上がり処理，あとに+11でもOK
        for num in range(0,10):
            ni = i + 1
            nj = 0
            b=num-x
            if b<0:
                b+=10
                nj=1
            dp[ni][nj]=min(dp[ni][nj],dp[i][j]+num+b) j range(2) range 2 x=int(n[i]) x int(n[i]) int n[i] n i x+=j x j for num in range(0,10):
            ni = i + 1
            nj = 0
            b=num-x
            if b<0:
                b+=10
                nj=1
            dp[ni][nj]=min(dp[ni][nj],dp[i][j]+num+b) num range(0,10) range 0 10 ni = i + 1 ni i + 1 i 1 nj = 0 nj 0 b=num-x b num-x num x if b<0:
                b+=10
                nj=1
             b<0 b 0 b+=10 b 10 nj=1 nj 1 dp[ni][nj]=min(dp[ni][nj],dp[i][j]+num+b) dp[ni][nj] [ni] dp ni nj min(dp[ni][nj],dp[i][j]+num+b) min dp[ni][nj] [ni] dp ni nj dp[i][j]+num+b dp[i][j]+num dp[i][j] [i] dp i j num b print(dp[lt][0]) print dp[lt][0] [lt] dp lt 0 x+=j j x nj=1 1 nj b=num-x num-x b n=input()[::-1] input()[::-1] n b+=10 10 b nj = 0 0 nj x=int(n[i]) int(n[i]) x lt=len(n) len(n) lt ni = i + 1 i + 1 ni n+="0" "0" n dp=[[float('inf') for _ in range(2)] for j in range(lt+1)] [[float('inf') for _ in range(2)] for j in range(lt+1)] dp
n=input()[::-1]
n+="0"
lt=len(n)
dp=[[float('inf') for _ in range(2)] for j in range(lt+1)]
dp[0][0]=0

for i in range(0,lt):
    for j in range(2):
        x=int(n[i])
        x+=j #繰り上がり処理，あとに+11でもOK
        for num in range(0,10):
            ni = i + 1
            nj = 0
            b=num-x
            if b<0:
                b+=10
                nj=1
            dp[ni][nj]=min(dp[ni][nj],dp[i][j]+num+b)

print(dp[lt][0])