def main():
    s = input()
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print(min(dp))

if __name__ == "__main__":
    main() def main():
    s = input()
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print(min(dp)) main s = input() s input() input dp = [0, 1] dp [0, 1] 0 1 for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
     c s x = int(c) x int(c) int c a = dp[0] + x a dp[0] + x dp[0] dp 0 x if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
         a > dp[1] + 10 - x a dp[1] + 10 - x dp[1] + 10 dp[1] dp 1 10 x a = dp[1] + 10 - x a dp[1] + 10 - x dp[1] + 10 dp[1] dp 1 10 x b = dp[0] + x + 1 b dp[0] + x + 1 dp[0] + x dp[0] dp 0 x 1 if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
         b > dp[1] + 10 - x - 1 b dp[1] + 10 - x - 1 dp[1] + 10 - x dp[1] + 10 dp[1] dp 1 10 x 1 b = dp[1] + 10 - x - 1 b dp[1] + 10 - x - 1 dp[1] + 10 - x dp[1] + 10 dp[1] dp 1 10 x 1 dp[0] = a dp[0] dp 0 a dp[1] = b dp[1] dp 1 b dp[1] += 1 dp[1] dp 1 1 print(min(dp)) print min(dp) min dp if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    s = input()
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print(min(dp)) def main():
    s = input()
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print(min(dp)) main
def main():
    s = input()
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print(min(dp))

if __name__ == "__main__":
    main()
