def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 2
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print( min(dp[N][0], dp[N][1]))
if __name__ == '__main__':
    main() def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 2
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print( min(dp[N][0], dp[N][1])) main S = list( map( int, list( input()))) S list( map( int, list( input()))) list map( int, list( input())) map int list( input()) list input() input N = len(S) N len(S) len S [0,0] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [0,0] 0 0 dp = [[0,0] for _ in range(N+1)] dp [[0,0] for _ in range(N+1)] dp[0][1] = 2 dp[0][1] [0] dp 0 1 2 for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
     i range(N) range N s = S[i] s S[i] S i dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + s, dp[i][1]+s) min dp[i][0] + s dp[i][0] [i] dp i 0 s dp[i][1]+s dp[i][1] [i] dp i 1 s dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + 11-s, dp[i][1]+9-s) min dp[i][0] + 11-s dp[i][0] + 11 dp[i][0] [i] dp i 0 11 s dp[i][1]+9-s dp[i][1]+9 dp[i][1] [i] dp i 1 9 s print( min(dp[N][0], dp[N][1])) print min(dp[N][0], dp[N][1]) min dp[N][0] [N] dp N 0 dp[N][1] [N] dp N 1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 2
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print( min(dp[N][0], dp[N][1])) def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 2
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print( min(dp[N][0], dp[N][1])) main
def main():
    S = list( map( int, list( input())))
    N = len(S)
    dp = [[0,0] for _ in range(N+1)]
    dp[0][1] = 2
    for i in range(N):
        s = S[i]
        dp[i+1][0] = min(dp[i][0] + s, dp[i][1]+s)
        dp[i+1][1] = min(dp[i][0] + 11-s, dp[i][1]+9-s)
        # dp[i+1][2] = min(dp[i][1], dp[i][2])+9-s
        # 11-sは、追加で支払うものと、お釣り
        # 9-sは、追加はすでに支払われていて、繰り下がりが起こっている
    print( min(dp[N][0], dp[N][1]))
if __name__ == '__main__':
    main()