def main():
    *e, = map(int, 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 def main():
    *e, = map(int, 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)) main *e, = map(int, input()) *e e map(int, input()) map int input() input e.reverse() e.reverse e reverse inf = 20 * len(e) inf 20 * len(e) 20 len(e) len e dp = [0, inf] dp [0, inf] 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

     x e ndp = [-1, -1] ndp [-1, -1] -1 -1 ndp[0] = min(
            dp[0] + x,
            dp[1] + ((x + 1) if x < 9 else inf)  # ちょうど支払えないのでinf
        ) ndp[0] ndp 0 min(
            dp[0] + x,
            dp[1] + ((x + 1) if x < 9 else inf)  # ちょうど支払えないのでinf
        ) min dp[0] + x dp[0] dp 0 x dp[1] + ((x + 1) if x < 9 else inf) dp[1] dp 1 (x + 1) if x < 9 else inf x < 9 x 9 x + 1 x 1 inf ndp[1] = min(
            dp[0] + ((10 - x) if x > 0 else inf),  # 繰り下がりにならないのでinf
            dp[1] + (10 - (x + 1))
        ) ndp[1] ndp 1 min(
            dp[0] + ((10 - x) if x > 0 else inf),  # 繰り下がりにならないのでinf
            dp[1] + (10 - (x + 1))
        ) min dp[0] + ((10 - x) if x > 0 else inf) dp[0] dp 0 (10 - x) if x > 0 else inf x > 0 x 0 10 - x 10 x inf dp[1] + (10 - (x + 1)) dp[1] dp 1 10 - (x + 1) 10 x + 1 x 1 dp = ndp dp ndp print(min(dp[0], dp[1] + 1)) print min(dp[0], dp[1] + 1) min dp[0] dp 0 dp[1] + 1 dp[1] 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 __name__ == '__main__' __name__ '__main__' main() main def main():
    *e, = map(int, 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)) def main():
    *e, = map(int, 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)) main
def main():
    *e, = map(int, 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
