import sys

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

def main():
    dp = [0, 1]
    for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)

    return dp[0]

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 def main():
    dp = [0, 1]
    for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)

    return dp[0] main dp = [0, 1] dp [0, 1] 0 1 for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)

     d n d = int(d) d int(d) int d dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1) dp[0] dp 0 min(dp[0]+d, dp[1]+10-d) min dp[0]+d dp[0] dp 0 d dp[1]+10-d dp[1]+10 dp[1] dp 1 10 d dp[1] dp 1 min(dp[0]+d+1, dp[1]+10-d-1) min dp[0]+d+1 dp[0]+d dp[0] dp 0 d 1 dp[1]+10-d-1 dp[1]+10-d dp[1]+10 dp[1] dp 1 10 d 1 return dp[0] dp[0] dp 0 if __name__ == '__main__':
    ans = main()
    print(ans) __name__ == '__main__' __name__ '__main__' ans = main() ans main() main print(ans) print ans ans = main() main() ans def main():
    dp = [0, 1]
    for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)

    return dp[0] def main():
    dp = [0, 1]
    for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)

    return dp[0] main n = sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() n
import sys

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

def main():
    dp = [0, 1]
    for d in n:
        d = int(d)
        dp[0], dp[1] = min(dp[0]+d, dp[1]+10-d), min(dp[0]+d+1, dp[1]+10-d-1)

    return dp[0]

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