Z = [0,1,2,3,4,5,5,4,3,2,1,1]
X=[0,1,2,3,4,5,6,7,8,9]
def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

    return dp[n-1][0]

if __name__ == '__main__':
    s = input()
    print(solve_dp(s)) Z = [0,1,2,3,4,5,5,4,3,2,1,1] Z [0,1,2,3,4,5,5,4,3,2,1,1] 0 1 2 3 4 5 5 4 3 2 1 1 X=[0,1,2,3,4,5,6,7,8,9] X [0,1,2,3,4,5,6,7,8,9] 0 1 2 3 4 5 6 7 8 9 def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

    return dp[n-1][0] solve_dp n = len(s) n len(s) len s int(c) for c in s for c in s c s for c in s int(c) int c s = [int(c) for c in s] s [int(c) for c in s] inf = 10**9 inf 10**9 10 9 [inf] * 2 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [inf] * 2 [inf] inf 2 dp = [[inf] * 2 for _ in range(n)] dp [[inf] * 2 for _ in range(n)] d0 = s[0] d0 s[0] s 0 dp[0][0] = Z[d0] dp[0][0] [0] dp 0 0 Z[d0] Z d0 dp[0][1] = Z[d0+1] dp[0][1] [0] dp 0 1 Z[d0+1] Z d0+1 d0 1 for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

     i range(1, n) range 1 n for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

     j range(2) range 2 d = s[i] + j d s[i] + j s[i] s i j for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

     k range(10) range 10 if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

     d + X[k] >= 10 d + X[k] d X[k] X k 10 dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10]) dp[i][j] [i] dp i j min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10]) min dp[i][j] [i] dp i j dp[i-1][1] + X[k] + Z[d + X[k] - 10] dp[i-1][1] + X[k] dp[i-1][1] [i-1] dp i-1 i 1 1 X[k] X k Z[d + X[k] - 10] Z d + X[k] - 10 d + X[k] d X[k] X k 10 dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]]) dp[i][j] [i] dp i j min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]]) min dp[i][j] [i] dp i j dp[i-1][0] + X[k] + Z[d + X[k]] dp[i-1][0] + X[k] dp[i-1][0] [i-1] dp i-1 i 1 0 X[k] X k Z[d + X[k]] Z d + X[k] d X[k] X k return dp[n-1][0] dp[n-1][0] [n-1] dp n-1 n 1 0 s s if __name__ == '__main__':
    s = input()
    print(solve_dp(s)) __name__ == '__main__' __name__ '__main__' s = input() s input() input print(solve_dp(s)) print solve_dp(s) solve_dp s def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

    return dp[n-1][0] def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

    return dp[n-1][0] solve_dp Z = [0,1,2,3,4,5,5,4,3,2,1,1] [0,1,2,3,4,5,5,4,3,2,1,1] Z s = input() input() s X=[0,1,2,3,4,5,6,7,8,9] [0,1,2,3,4,5,6,7,8,9] X
Z = [0,1,2,3,4,5,5,4,3,2,1,1]
X=[0,1,2,3,4,5,6,7,8,9]
def solve_dp(s):
    n = len(s)
    s = [int(c) for c in s]
    inf = 10**9

    dp = [[inf] * 2 for _ in range(n)]

    d0 = s[0]
    dp[0][0] = Z[d0]
    dp[0][1] = Z[d0+1]

    for i in range(1, n):
        for j in range(2):
            d = s[i] + j
            for k in range(10):
                if d + X[k] >= 10:
                    dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + X[k] - 10])
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + X[k]])

    return dp[n-1][0]

if __name__ == '__main__':
    s = input()
    print(solve_dp(s))
