def main():

    *e, = list(map(int, eval(input())))

    e.reverse()



    inf = 20 * len(e)



    dp = [0, inf]  # 繰り下がり無,有



    for x in e:

        ndp = [-1, -1]  # 繰り下がり無,有



        ndp[0] = min(

            dp[0] + x,

            dp[1] + ((x + 1) if x < 9 else inf)  # ちょうど支払えないのでinf

        )  # ちょうど支払う



        ndp[1] = min(

            dp[0] + ((10 - x) if x > 0 else inf),  # 繰り下がりにならないのでinf

            dp[1] + (10 - (x + 1))

        )  # 0枚支払って繰り下がり



        dp = ndp



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





if __name__ == '__main__':

    main()



# import sys

# input = sys.stdin.readline

# 

# sys.setrecursionlimit(10 ** 7)

# 

# (int(x)-1 for x in input().split())

# rstrip()

#

# def binary_search(*, ok, ng, func):

#     while abs(ok - ng) > 1:

#         mid = (ok + ng) // 2

#         if func(mid):

#             ok = mid

#         else:

#             ng = mid

#     return ok

# https://atcoder.jp/contests/abc155/submissions/10157837

# 写経



def main():

    a = [0]

    a += list(map(int, eval(input())))

    a.reverse()



    n = len(a)

    b = a.copy()



    for i in range(n):

        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):

            b[i] = 0

            b[i + 1] += 1



    cnt = sum(b)

    for i in range(n):

        if a[i] > b[i]:

            b[i] += 10

            b[i + 1] -= 1

        cnt += b[i] - a[i]

    print(cnt)





if __name__ == '__main__':

    main()

def main():
    (*e,) = list(map(int, eval(input())))
    e.reverse()
    inf = 20 * len(e)
    dp = [0, inf]  # 繰り下がり無,有
    for x in e:
        ndp = [-1, -1]  # 繰り下がり無,有
        ndp[0] = min(
            dp[0] + x, dp[1] + ((x + 1) if x < 9 else inf)  # ちょうど支払えないのでinf
        )  # ちょうど支払う
        ndp[1] = min(
            dp[0] + ((10 - x) if x > 0 else inf),  # 繰り下がりにならないのでinf
            dp[1] + (10 - (x + 1)),
        )  # 0枚支払って繰り下がり
        dp = ndp
    print((min(dp[0], dp[1] + 1)))


if __name__ == "__main__":
    main()
# import sys
# input = sys.stdin.readline
#
# sys.setrecursionlimit(10 ** 7)
#
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

# https://atcoder.jp/contests/abc155/submissions/10157837
# 写経
def main():
    a = [0]
    a += list(map(int, eval(input())))
    a.reverse()
    n = len(a)
    b = a.copy()
    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1
    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt)


if __name__ == "__main__":
    main()

+# https://atcoder.jp/contests/abc155/submissions/10157837 +# 写経 -    (*e,) = list(map(int, eval(input()))) -    e.reverse() -    inf = 20 * len(e) -    dp = [0, inf]  # 繰り下がり無,有 -    for x in e: -        ndp = [-1, -1]  # 繰り下がり無,有 -        ndp[0] = min( -            dp[0] + x, dp[1] + ((x + 1) if x < 9 else inf)  # ちょうど支払えないのでinf -        )  # ちょうど支払う -        ndp[1] = min( -            dp[0] + ((10 - x) if x > 0 else inf),  # 繰り下がりにならないのでinf -            dp[1] + (10 - (x + 1)), -        )  # 0枚支払って繰り下がり -        dp = ndp -    print((min(dp[0], dp[1] + 1))) +    a = [0] +    a += list(map(int, eval(input()))) +    a.reverse() +    n = len(a) +    b = a.copy() +    for i in range(n): +        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5): +            b[i] = 0 +            b[i + 1] += 1 +    cnt = sum(b) +    for i in range(n): +        if a[i] > b[i]: +            b[i] += 10 +            b[i + 1] -= 1 +        cnt += b[i] - a[i] +    print(cnt) -# import sys -# input = sys.stdin.readline -# -# sys.setrecursionlimit(10 ** 7) -# -# (int(x)-1 for x in input().split()) -# rstrip() -# -# def binary_search(*, ok, ng, func): -#     while abs(ok - ng) > 1: -#         mid = (ok + ng) // 2 -#         if func(mid): -#             ok = mid -#         else: -#             ng = mid -#     return ok