def solve(n):
    n = list(map(int, n))
    a, b = 0, 9
    for d in n:
        a, b = min(d+a, d+b), min(11-d+a, 9-d+b)
    return min(a, b)

assert solve("36") == 8
assert solve("91") == 3
assert solve("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170") == 243

n = input()
print(solve(n)) def solve(n):
    n = list(map(int, n))
    a, b = 0, 9
    for d in n:
        a, b = min(d+a, d+b), min(11-d+a, 9-d+b)
    return min(a, b) solve n = list(map(int, n)) n list(map(int, n)) list map(int, n) map int n a, b = 0, 9 a 0 b 9 for d in n:
        a, b = min(d+a, d+b), min(11-d+a, 9-d+b)
     d n a, b = min(d+a, d+b), min(11-d+a, 9-d+b) a min(d+a, d+b) min d+a d a d+b d b b min(11-d+a, 9-d+b) min 11-d+a 11-d 11 d a 9-d+b 9-d 9 d b return min(a, b) min(a, b) min a b n n assert solve("36") == 8 solve("36") == 8 solve("36") solve "36" 8 assert solve("91") == 3 solve("91") == 3 solve("91") solve "91" 3 assert solve("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170") == 243 solve("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170") == 243 solve("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170") solve "314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170" 243 n = input() n input() input print(solve(n)) print solve(n) solve n def solve(n):
    n = list(map(int, n))
    a, b = 0, 9
    for d in n:
        a, b = min(d+a, d+b), min(11-d+a, 9-d+b)
    return min(a, b) def solve(n):
    n = list(map(int, n))
    a, b = 0, 9
    for d in n:
        a, b = min(d+a, d+b), min(11-d+a, 9-d+b)
    return min(a, b) solve n = input() input() n
def solve(n):
    n = list(map(int, n))
    a, b = 0, 9
    for d in n:
        a, b = min(d+a, d+b), min(11-d+a, 9-d+b)
    return min(a, b)

assert solve("36") == 8
assert solve("91") == 3
assert solve("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170") == 243

n = input()
print(solve(n))