# 解説放送

# 写経



def main():

    inf = 18 * (10 ** 6 + 1)



    s = eval(input())

    s = '0' + s

    *s, = list(map(int, s))  # 先に一括でintにした



    n = len(s)



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

    dp[0][0] = 0



    for i, c in enumerate(s[::-1]):

        for j in range(2):

            x = c + j

            if x < 10:

                dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)  # a=x,b=0

            if x > 0:

                dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x))  # a=0,bに繰り下がり



    print((dp[n][0]))





if __name__ == '__main__':

    main()

# 解説放送

# 写経



def main():

    from itertools import islice



    inf = 18 * (10 ** 6 + 1)



    s = '0' + eval(input())

    n = len(s)



    s = list(map(int, reversed(s)))  # 先に一括でintにした



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

    dp[0][0] = 0



    for i, c in enumerate(islice(s, 0, n, 1)):

        for j in range(2):

            x = c + j

            if x < 10:

                dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)  # a=x,b=0

            if x > 0:

                dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x))  # a=0,bに繰り下がり



    print((dp[n][0]))





if __name__ == '__main__':

    main()

# 解説放送
# 写経
def main():
    inf = 18 * (10**6 + 1)
    s = eval(input())
    s = "0" + s
    (*s,) = list(map(int, s))  # 先に一括でintにした
    n = len(s)
    dp = [[inf] * 2 for _ in range(n + 1)]
    dp[0][0] = 0
    for i, c in enumerate(s[::-1]):
        for j in range(2):
            x = c + j
            if x < 10:
                dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)  # a=x,b=0
            if x > 0:
                dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x))  # a=0,bに繰り下がり
    print((dp[n][0]))


if __name__ == "__main__":
    main()

# 解説放送
# 写経
def main():
    from itertools import islice

    inf = 18 * (10**6 + 1)
    s = "0" + eval(input())
    n = len(s)
    s = list(map(int, reversed(s)))  # 先に一括でintにした
    dp = [[inf] * 2 for _ in range(n + 1)]
    dp[0][0] = 0
    for i, c in enumerate(islice(s, 0, n, 1)):
        for j in range(2):
            x = c + j
            if x < 10:
                dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)  # a=x,b=0
            if x > 0:
                dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x))  # a=0,bに繰り下がり
    print((dp[n][0]))


if __name__ == "__main__":
    main()

+    from itertools import islice + -    s = eval(input()) -    s = "0" + s -    (*s,) = list(map(int, s))  # 先に一括でintにした +    s = "0" + eval(input()) +    s = list(map(int, reversed(s)))  # 先に一括でintにした -    for i, c in enumerate(s[::-1]): +    for i, c in enumerate(islice(s, 0, n, 1)):