#!/usr/bin/env python3
import sys
# sys.setrecursionlimit(10000000)
# INF = 1<<32

def solve(N: "List[int]"):
    # N=36を考える
    # l = list(map(int, list(str(N))))
    dp = 0,1
    for i in N:
        # a = f(3), f(36)
        a = min(dp[0]+i, dp[1]+10-i)
        # b = f(4), f(40)
        b = min(dp[0]+i+1, dp[1]+10-(i+1))
        dp = a, b

    print(dp[0])
    return



def main():
    stdin = open(0)
    N = [int(x) for x in stdin.read().rstrip()]
    solve(N)

if __name__ == '__main__':
    main() import sys sys def solve(N: "List[int]"):
    # N=36を考える
    # l = list(map(int, list(str(N))))
    dp = 0,1
    for i in N:
        # a = f(3), f(36)
        a = min(dp[0]+i, dp[1]+10-i)
        # b = f(4), f(40)
        b = min(dp[0]+i+1, dp[1]+10-(i+1))
        dp = a, b

    print(dp[0])
    return solve dp = 0,1 dp dp = 0,1 0 1 for i in N:
        # a = f(3), f(36)
        a = min(dp[0]+i, dp[1]+10-i)
        # b = f(4), f(40)
        b = min(dp[0]+i+1, dp[1]+10-(i+1))
        dp = a, b

     i N a = min(dp[0]+i, dp[1]+10-i) a min(dp[0]+i, dp[1]+10-i) min dp[0]+i dp[0] dp 0 i dp[1]+10-i dp[1]+10 dp[1] dp 1 10 i b = min(dp[0]+i+1, dp[1]+10-(i+1)) b min(dp[0]+i+1, dp[1]+10-(i+1)) min dp[0]+i+1 dp[0]+i dp[0] dp 0 i 1 dp[1]+10-(i+1) dp[1]+10 dp[1] dp 1 10 i+1 i 1 dp = a, b dp dp = a, b a b print(dp[0]) print dp[0] dp 0 return N: "List[int]" N def main():
    stdin = open(0)
    N = [int(x) for x in stdin.read().rstrip()]
    solve(N) main stdin = open(0) stdin open(0) open 0 int(x) for x in stdin.read().rstrip() for x in stdin.read().rstrip() x stdin.read().rstrip() stdin.read().rstrip stdin.read() stdin.read stdin read rstrip for x in stdin.read().rstrip() int(x) int x N = [int(x) for x in stdin.read().rstrip()] N [int(x) for x in stdin.read().rstrip()] solve(N) solve N if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def solve(N: "List[int]"):
    # N=36を考える
    # l = list(map(int, list(str(N))))
    dp = 0,1
    for i in N:
        # a = f(3), f(36)
        a = min(dp[0]+i, dp[1]+10-i)
        # b = f(4), f(40)
        b = min(dp[0]+i+1, dp[1]+10-(i+1))
        dp = a, b

    print(dp[0])
    return def solve(N: "List[int]"):
    # N=36を考える
    # l = list(map(int, list(str(N))))
    dp = 0,1
    for i in N:
        # a = f(3), f(36)
        a = min(dp[0]+i, dp[1]+10-i)
        # b = f(4), f(40)
        b = min(dp[0]+i+1, dp[1]+10-(i+1))
        dp = a, b

    print(dp[0])
    return solve def main():
    stdin = open(0)
    N = [int(x) for x in stdin.read().rstrip()]
    solve(N) def main():
    stdin = open(0)
    N = [int(x) for x in stdin.read().rstrip()]
    solve(N) main
#!/usr/bin/env python3
import sys
# sys.setrecursionlimit(10000000)
# INF = 1<<32

def solve(N: "List[int]"):
    # N=36を考える
    # l = list(map(int, list(str(N))))
    dp = 0,1
    for i in N:
        # a = f(3), f(36)
        a = min(dp[0]+i, dp[1]+10-i)
        # b = f(4), f(40)
        b = min(dp[0]+i+1, dp[1]+10-(i+1))
        dp = a, b

    print(dp[0])
    return



def main():
    stdin = open(0)
    N = [int(x) for x in stdin.read().rstrip()]
    solve(N)

if __name__ == '__main__':
    main()
