ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
inf = 10 **9 + 5
def main():
    s = str(input())
    s = s[::-1]
    dp1 = [inf] *(10 ** 6 +3)
    dp2 = [inf] *(10 ** 6 +3)
    dp1[0] = int(s[0])
    dp2[0] =  10 -int(s[0])
    for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
    print(min(dp1[len(s)-1],dp2[len(s)-1] + 1))

main() ii = lambda : int(input()) ii lambda : int(input()) int(input()) int input() input mi = lambda : map(int,input().split()) mi lambda : map(int,input().split()) map(int,input().split()) map int input().split() ().split () input split li = lambda : list(map(int,input().split())) li lambda : list(map(int,input().split())) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split inf = 10 **9 + 5 inf 10 **9 + 5 10 **9 10 9 5 def main():
    s = str(input())
    s = s[::-1]
    dp1 = [inf] *(10 ** 6 +3)
    dp2 = [inf] *(10 ** 6 +3)
    dp1[0] = int(s[0])
    dp2[0] =  10 -int(s[0])
    for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
    print(min(dp1[len(s)-1],dp2[len(s)-1] + 1)) main s = str(input()) s str(input()) str input() input s = s[::-1] s s[::-1] s ::-1 -1 dp1 = [inf] *(10 ** 6 +3) dp1 [inf] *(10 ** 6 +3) [inf] inf 10 ** 6 +3 10 ** 6 10 6 3 dp2 = [inf] *(10 ** 6 +3) dp2 [inf] *(10 ** 6 +3) [inf] inf 10 ** 6 +3 10 ** 6 10 6 3 dp1[0] = int(s[0]) dp1[0] dp1 0 int(s[0]) int s[0] s 0 dp2[0] =  10 -int(s[0]) dp2[0] dp2 0 10 -int(s[0]) 10 int(s[0]) int s[0] s 0 for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
     i range(1,len(s)) range 1 len(s) len s dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i])) dp1[i] dp1 i min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i])) min dp2[i-1] + int(s[i]) + 1 dp2[i-1] + int(s[i]) dp2[i-1] dp2 i-1 i 1 int(s[i]) int s[i] s i 1 dp1[i-1] + int(s[i]) dp1[i-1] dp1 i-1 i 1 int(s[i]) int s[i] s i dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i])) dp2[i] dp2 i min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i])) min dp2[i-1] + 9 - int(s[i]) dp2[i-1] + 9 dp2[i-1] dp2 i-1 i 1 9 int(s[i]) int s[i] s i dp1[i-1] + 10 - int(s[i]) dp1[i-1] + 10 dp1[i-1] dp1 i-1 i 1 10 int(s[i]) int s[i] s i print(min(dp1[len(s)-1],dp2[len(s)-1] + 1)) print min(dp1[len(s)-1],dp2[len(s)-1] + 1) min dp1[len(s)-1] dp1 len(s)-1 len(s) len s 1 dp2[len(s)-1] + 1 dp2[len(s)-1] dp2 len(s)-1 len(s) len s 1 1 main() main li = lambda : list(map(int,input().split())) lambda : list(map(int,input().split())) li def main():
    s = str(input())
    s = s[::-1]
    dp1 = [inf] *(10 ** 6 +3)
    dp2 = [inf] *(10 ** 6 +3)
    dp1[0] = int(s[0])
    dp2[0] =  10 -int(s[0])
    for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
    print(min(dp1[len(s)-1],dp2[len(s)-1] + 1)) def main():
    s = str(input())
    s = s[::-1]
    dp1 = [inf] *(10 ** 6 +3)
    dp2 = [inf] *(10 ** 6 +3)
    dp1[0] = int(s[0])
    dp2[0] =  10 -int(s[0])
    for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
    print(min(dp1[len(s)-1],dp2[len(s)-1] + 1)) main ii = lambda : int(input()) lambda : int(input()) ii inf = 10 **9 + 5 10 **9 + 5 inf mi = lambda : map(int,input().split()) lambda : map(int,input().split()) mi
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
inf = 10 **9 + 5
def main():
    s = str(input())
    s = s[::-1]
    dp1 = [inf] *(10 ** 6 +3)
    dp2 = [inf] *(10 ** 6 +3)
    dp1[0] = int(s[0])
    dp2[0] =  10 -int(s[0])
    for i in range(1,len(s)):
        dp1[i] = min(dp2[i-1] + int(s[i]) + 1,dp1[i-1] + int(s[i]))
        dp2[i] = min(dp2[i-1] + 9 - int(s[i]),dp1[i-1] + 10 - int(s[i]))
    print(min(dp1[len(s)-1],dp2[len(s)-1] + 1))

main()
