def dp_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]
    dp[0][0]=0
    for i in range(l+1):
        for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
    return(dp[l+1])

s=input()
ans=dp_solver(s)
print(ans[0]) def dp_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]
    dp[0][0]=0
    for i in range(l+1):
        for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
    return(dp[l+1]) dp_solver l=len(tmp) l len(tmp) len tmp rev=tmp[::-1]+"0" rev tmp[::-1]+"0" tmp[::-1] tmp ::-1 -1 "0" [float("inf") for _ in range(2)] for _ in range(l+2) for _ in range(l+2) _ range(l+2) range l+2 l 2 for _ in range(l+2) [float("inf") for _ in range(2)] dp=[[float("inf") for _ in range(2)] for _ in range(l+2)] dp [[float("inf") for _ in range(2)] for _ in range(l+2)] dp[0][0]=0 dp[0][0] [0] dp 0 0 0 for i in range(l+1):
        for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
     i range(l+1) range l+1 l 1 for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
     j range(2) range 2 num=int(rev[i]) num int(rev[i]) int rev[i] rev i num+=j num j if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
             num<10 num 10 dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i+1][0],dp[i][j]+num) min dp[i+1][0] [i+1] dp i+1 i 1 0 dp[i][j]+num dp[i][j] [i] dp i j num if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
     num>0 num 0 dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num)) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i+1][1],dp[i][j]+(10-num)) min dp[i+1][1] [i+1] dp i+1 i 1 1 dp[i][j]+(10-num) dp[i][j] [i] dp i j 10-num 10 num return(dp[l+1]) dp[l+1] dp l+1 l 1 tmp tmp s=input() s input() input ans=dp_solver(s) ans dp_solver(s) dp_solver s print(ans[0]) print ans[0] ans 0 ans=dp_solver(s) dp_solver(s) ans def dp_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]
    dp[0][0]=0
    for i in range(l+1):
        for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
    return(dp[l+1]) def dp_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]
    dp[0][0]=0
    for i in range(l+1):
        for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
    return(dp[l+1]) dp_solver s=input() input() s
def dp_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]
    dp[0][0]=0
    for i in range(l+1):
        for j in range(2):
            num=int(rev[i])
            num+=j
            if num<10:
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
            if num>0:
                dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))

    #import numpy as np
    #dp=np.array(dp)
    return(dp[l+1])

s=input()
ans=dp_solver(s)
print(ans[0])