import sys

n = sys.stdin.readline().rstrip()
l = len(n)

def main():
    dp0 = [None] * (l + 1)
    dp1 = [None] * (l + 1)
    dp0[0] = 0
    dp1[0] = 1

    for i in range(l):
        d = int(n[i])
        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)
        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)
    
    return dp0[l]

if __name__ == '__main__':
    ans = main()
    print(ans) import sys sys n = sys.stdin.readline().rstrip() n sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip l = len(n) l len(n) len n def main():
    dp0 = [None] * (l + 1)
    dp1 = [None] * (l + 1)
    dp0[0] = 0
    dp1[0] = 1

    for i in range(l):
        d = int(n[i])
        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)
        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)
    
    return dp0[l] main dp0 = [None] * (l + 1) dp0 [None] * (l + 1) [None] None l + 1 l 1 dp1 = [None] * (l + 1) dp1 [None] * (l + 1) [None] None l + 1 l 1 dp0[0] = 0 dp0[0] dp0 0 0 dp1[0] = 1 dp1[0] dp1 0 1 for i in range(l):
        d = int(n[i])
        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)
        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)
    
     i range(l) range l d = int(n[i]) d int(n[i]) int n[i] n i dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d) dp0[i+1] dp0 i+1 i 1 min(dp0[i]+d, dp1[i]+10-d) min dp0[i]+d dp0[i] dp0 i d dp1[i]+10-d dp1[i]+10 dp1[i] dp1 i 10 d dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1) dp1[i+1] dp1 i+1 i 1 min(dp0[i]+d+1, dp1[i]+10-d-1) min dp0[i]+d+1 dp0[i]+d dp0[i] dp0 i d 1 dp1[i]+10-d-1 dp1[i]+10-d dp1[i]+10 dp1[i] dp1 i 10 d 1 return dp0[l] dp0[l] dp0 l if __name__ == '__main__':
    ans = main()
    print(ans) __name__ == '__main__' __name__ '__main__' ans = main() ans main() main print(ans) print ans n = sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() n l = len(n) len(n) l def main():
    dp0 = [None] * (l + 1)
    dp1 = [None] * (l + 1)
    dp0[0] = 0
    dp1[0] = 1

    for i in range(l):
        d = int(n[i])
        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)
        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)
    
    return dp0[l] def main():
    dp0 = [None] * (l + 1)
    dp1 = [None] * (l + 1)
    dp0[0] = 0
    dp1[0] = 1

    for i in range(l):
        d = int(n[i])
        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)
        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)
    
    return dp0[l] main ans = main() main() ans
import sys

n = sys.stdin.readline().rstrip()
l = len(n)

def main():
    dp0 = [None] * (l + 1)
    dp1 = [None] * (l + 1)
    dp0[0] = 0
    dp1[0] = 1

    for i in range(l):
        d = int(n[i])
        dp0[i+1] = min(dp0[i]+d, dp1[i]+10-d)
        dp1[i+1] = min(dp0[i]+d+1, dp1[i]+10-d-1)
    
    return dp0[l]

if __name__ == '__main__':
    ans = main()
    print(ans)