n = input()
s = []
for c in n:
    s.append(int(c))
s.reverse()
l = len(s)
INF = 1000000000


def get_next(c):
    ret = [
        [INF, INF],
        [INF, INF],
    ]
    # for a in range(10):
    #     for b in range(10):
    #         if a - b == c:
    #             ret[0][0] = min(ret[0][0], a + b)
    #         if 10 + a - b == c:
    #             ret[0][1] = min(ret[0][1], a + b)
    #         if a - 1 - b == c:
    #             ret[1][0] = min(ret[1][0], a + b)
    #         if 10 + a - 1 - b == c:
    #             ret[1][1] = min(ret[1][1], a + b)
    # for b in range(10):
    #     a = c + b
    #     if 0 <= a < 10:
    #         ret[0][0] = min(ret[0][0], a+b)
    #     a = c + b - 10
    #     if 0 <= a < 10:
    #         ret[0][1] = min(ret[0][1], a+b)
    #     a = c + b + 1
    #     if 0 <= a < 10:
    #         ret[1][0] = min(ret[1][0], a + b)
    #     a = c + b - 10 + 1
    #     if 0 <= a < 10:
    #         ret[1][1] = min(ret[1][1], a + b)
    a = c
    ret[0][0] = min(ret[0][0], a)
    b = 10 - c
    if b < 10:
        ret[0][1] = min(ret[0][1], b)
    a = c + 1
    if a < 10:
        ret[1][0] = min(ret[1][0], a)
    b = 9-c
    ret[1][1] = min(ret[1][1], b)

    return ret


dp = [[0 for _ in range(l+1)] for _ in range(2)]

dp[1][0] = INF

for i, c in enumerate(s):
    next_step = get_next(c)
    dp[0][i+1] = min(dp[0][i]+next_step[0][0], dp[1][i]+next_step[1][0])
    dp[1][i+1] = min(dp[0][i]+next_step[0][1], dp[1][i]+next_step[1][1])

print(min(dp[0][l], dp[1][l]+1))
# print(dp) n = input() n input() input s = [] s [] for c in n:
    s.append(int(c)) c n s.append(int(c)) s.append s append int(c) int c s.reverse() s.reverse s reverse l = len(s) l len(s) len s INF = 1000000000 INF 1000000000 def get_next(c):
    ret = [
        [INF, INF],
        [INF, INF],
    ]
    # for a in range(10):
    #     for b in range(10):
    #         if a - b == c:
    #             ret[0][0] = min(ret[0][0], a + b)
    #         if 10 + a - b == c:
    #             ret[0][1] = min(ret[0][1], a + b)
    #         if a - 1 - b == c:
    #             ret[1][0] = min(ret[1][0], a + b)
    #         if 10 + a - 1 - b == c:
    #             ret[1][1] = min(ret[1][1], a + b)
    # for b in range(10):
    #     a = c + b
    #     if 0 <= a < 10:
    #         ret[0][0] = min(ret[0][0], a+b)
    #     a = c + b - 10
    #     if 0 <= a < 10:
    #         ret[0][1] = min(ret[0][1], a+b)
    #     a = c + b + 1
    #     if 0 <= a < 10:
    #         ret[1][0] = min(ret[1][0], a + b)
    #     a = c + b - 10 + 1
    #     if 0 <= a < 10:
    #         ret[1][1] = min(ret[1][1], a + b)
    a = c
    ret[0][0] = min(ret[0][0], a)
    b = 10 - c
    if b < 10:
        ret[0][1] = min(ret[0][1], b)
    a = c + 1
    if a < 10:
        ret[1][0] = min(ret[1][0], a)
    b = 9-c
    ret[1][1] = min(ret[1][1], b)

    return ret get_next ret = [
        [INF, INF],
        [INF, INF],
    ] ret [
        [INF, INF],
        [INF, INF],
    ] [INF, INF] INF INF [INF, INF] INF INF a = c a c ret[0][0] = min(ret[0][0], a) ret[0][0] [0] ret 0 0 min(ret[0][0], a) min ret[0][0] [0] ret 0 0 a b = 10 - c b 10 - c 10 c if b < 10:
        ret[0][1] = min(ret[0][1], b)
     b < 10 b 10 ret[0][1] = min(ret[0][1], b) ret[0][1] [0] ret 0 1 min(ret[0][1], b) min ret[0][1] [0] ret 0 1 b a = c + 1 a c + 1 c 1 if a < 10:
        ret[1][0] = min(ret[1][0], a)
     a < 10 a 10 ret[1][0] = min(ret[1][0], a) ret[1][0] [1] ret 1 0 min(ret[1][0], a) min ret[1][0] [1] ret 1 0 a b = 9-c b 9-c 9 c ret[1][1] = min(ret[1][1], b) ret[1][1] [1] ret 1 1 min(ret[1][1], b) min ret[1][1] [1] ret 1 1 b return ret ret c c [0 for _ in range(l+1)] for _ in range(2) for _ in range(2) _ range(2) range 2 for _ in range(2) [0 for _ in range(l+1)] dp = [[0 for _ in range(l+1)] for _ in range(2)] dp [[0 for _ in range(l+1)] for _ in range(2)] dp[1][0] = INF dp[1][0] [1] dp 1 0 INF for i, c in enumerate(s):
    next_step = get_next(c)
    dp[0][i+1] = min(dp[0][i]+next_step[0][0], dp[1][i]+next_step[1][0])
    dp[1][i+1] = min(dp[0][i]+next_step[0][1], dp[1][i]+next_step[1][1]) i c enumerate(s) enumerate s next_step = get_next(c) next_step get_next(c) get_next c dp[0][i+1] = min(dp[0][i]+next_step[0][0], dp[1][i]+next_step[1][0]) dp[0][i+1] [0] dp 0 i+1 i 1 min(dp[0][i]+next_step[0][0], dp[1][i]+next_step[1][0]) min dp[0][i]+next_step[0][0] dp[0][i] [0] dp 0 i next_step[0][0] [0] next_step 0 0 dp[1][i]+next_step[1][0] dp[1][i] [1] dp 1 i next_step[1][0] [1] next_step 1 0 dp[1][i+1] = min(dp[0][i]+next_step[0][1], dp[1][i]+next_step[1][1]) dp[1][i+1] [1] dp 1 i+1 i 1 min(dp[0][i]+next_step[0][1], dp[1][i]+next_step[1][1]) min dp[0][i]+next_step[0][1] dp[0][i] [0] dp 0 i next_step[0][1] [0] next_step 0 1 dp[1][i]+next_step[1][1] dp[1][i] [1] dp 1 i next_step[1][1] [1] next_step 1 1 print(min(dp[0][l], dp[1][l]+1)) print min(dp[0][l], dp[1][l]+1) min dp[0][l] [0] dp 0 l dp[1][l]+1 dp[1][l] [1] dp 1 l 1 s = [] [] s l = len(s) len(s) l dp = [[0 for _ in range(l+1)] for _ in range(2)] [[0 for _ in range(l+1)] for _ in range(2)] dp INF = 1000000000 1000000000 INF def get_next(c):
    ret = [
        [INF, INF],
        [INF, INF],
    ]
    # for a in range(10):
    #     for b in range(10):
    #         if a - b == c:
    #             ret[0][0] = min(ret[0][0], a + b)
    #         if 10 + a - b == c:
    #             ret[0][1] = min(ret[0][1], a + b)
    #         if a - 1 - b == c:
    #             ret[1][0] = min(ret[1][0], a + b)
    #         if 10 + a - 1 - b == c:
    #             ret[1][1] = min(ret[1][1], a + b)
    # for b in range(10):
    #     a = c + b
    #     if 0 <= a < 10:
    #         ret[0][0] = min(ret[0][0], a+b)
    #     a = c + b - 10
    #     if 0 <= a < 10:
    #         ret[0][1] = min(ret[0][1], a+b)
    #     a = c + b + 1
    #     if 0 <= a < 10:
    #         ret[1][0] = min(ret[1][0], a + b)
    #     a = c + b - 10 + 1
    #     if 0 <= a < 10:
    #         ret[1][1] = min(ret[1][1], a + b)
    a = c
    ret[0][0] = min(ret[0][0], a)
    b = 10 - c
    if b < 10:
        ret[0][1] = min(ret[0][1], b)
    a = c + 1
    if a < 10:
        ret[1][0] = min(ret[1][0], a)
    b = 9-c
    ret[1][1] = min(ret[1][1], b)

    return ret def get_next(c):
    ret = [
        [INF, INF],
        [INF, INF],
    ]
    # for a in range(10):
    #     for b in range(10):
    #         if a - b == c:
    #             ret[0][0] = min(ret[0][0], a + b)
    #         if 10 + a - b == c:
    #             ret[0][1] = min(ret[0][1], a + b)
    #         if a - 1 - b == c:
    #             ret[1][0] = min(ret[1][0], a + b)
    #         if 10 + a - 1 - b == c:
    #             ret[1][1] = min(ret[1][1], a + b)
    # for b in range(10):
    #     a = c + b
    #     if 0 <= a < 10:
    #         ret[0][0] = min(ret[0][0], a+b)
    #     a = c + b - 10
    #     if 0 <= a < 10:
    #         ret[0][1] = min(ret[0][1], a+b)
    #     a = c + b + 1
    #     if 0 <= a < 10:
    #         ret[1][0] = min(ret[1][0], a + b)
    #     a = c + b - 10 + 1
    #     if 0 <= a < 10:
    #         ret[1][1] = min(ret[1][1], a + b)
    a = c
    ret[0][0] = min(ret[0][0], a)
    b = 10 - c
    if b < 10:
        ret[0][1] = min(ret[0][1], b)
    a = c + 1
    if a < 10:
        ret[1][0] = min(ret[1][0], a)
    b = 9-c
    ret[1][1] = min(ret[1][1], b)

    return ret get_next n = input() input() n next_step = get_next(c) get_next(c) next_step
n = input()
s = []
for c in n:
    s.append(int(c))
s.reverse()
l = len(s)
INF = 1000000000


def get_next(c):
    ret = [
        [INF, INF],
        [INF, INF],
    ]
    # for a in range(10):
    #     for b in range(10):
    #         if a - b == c:
    #             ret[0][0] = min(ret[0][0], a + b)
    #         if 10 + a - b == c:
    #             ret[0][1] = min(ret[0][1], a + b)
    #         if a - 1 - b == c:
    #             ret[1][0] = min(ret[1][0], a + b)
    #         if 10 + a - 1 - b == c:
    #             ret[1][1] = min(ret[1][1], a + b)
    # for b in range(10):
    #     a = c + b
    #     if 0 <= a < 10:
    #         ret[0][0] = min(ret[0][0], a+b)
    #     a = c + b - 10
    #     if 0 <= a < 10:
    #         ret[0][1] = min(ret[0][1], a+b)
    #     a = c + b + 1
    #     if 0 <= a < 10:
    #         ret[1][0] = min(ret[1][0], a + b)
    #     a = c + b - 10 + 1
    #     if 0 <= a < 10:
    #         ret[1][1] = min(ret[1][1], a + b)
    a = c
    ret[0][0] = min(ret[0][0], a)
    b = 10 - c
    if b < 10:
        ret[0][1] = min(ret[0][1], b)
    a = c + 1
    if a < 10:
        ret[1][0] = min(ret[1][0], a)
    b = 9-c
    ret[1][1] = min(ret[1][1], b)

    return ret


dp = [[0 for _ in range(l+1)] for _ in range(2)]

dp[1][0] = INF

for i, c in enumerate(s):
    next_step = get_next(c)
    dp[0][i+1] = min(dp[0][i]+next_step[0][0], dp[1][i]+next_step[1][0])
    dp[1][i+1] = min(dp[0][i]+next_step[0][1], dp[1][i]+next_step[1][1])

print(min(dp[0][l], dp[1][l]+1))
# print(dp)