import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = list(map(int, list(readline().rstrip().decode())[::-1]))
a = 0
b = 10000
for x in n:
    memo = min(a + x, b + x)
    b = min(a + 11 - x, b + 9 - x)
    a = memo
print(min(a, b)) import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 n = list(map(int, list(readline().rstrip().decode())[::-1])) n list(map(int, list(readline().rstrip().decode())[::-1])) list map(int, list(readline().rstrip().decode())[::-1]) map int list(readline().rstrip().decode())[::-1] (readline().rstrip().decode()) list readline().rstrip().decode() ().rstrip().decode ().rstrip() ().rstrip () readline rstrip decode ::-1 -1 a = 0 a 0 b = 10000 b 10000 for x in n:
    memo = min(a + x, b + x)
    b = min(a + 11 - x, b + 9 - x)
    a = memo x n memo = min(a + x, b + x) memo min(a + x, b + x) min a + x a x b + x b x b = min(a + 11 - x, b + 9 - x) b min(a + 11 - x, b + 9 - x) min a + 11 - x a + 11 a 11 x b + 9 - x b + 9 b 9 x a = memo a memo print(min(a, b)) print min(a, b) min a b readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline b = min(a + 11 - x, b + 9 - x) min(a + 11 - x, b + 9 - x) b n = list(map(int, list(readline().rstrip().decode())[::-1])) list(map(int, list(readline().rstrip().decode())[::-1])) n a = memo memo a read = sys.stdin.buffer.read sys.stdin.buffer.read read b = 10000 10000 b readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines memo = min(a + x, b + x) min(a + x, b + x) memo a = 0 0 a
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = list(map(int, list(readline().rstrip().decode())[::-1]))
a = 0
b = 10000
for x in n:
    memo = min(a + x, b + x)
    b = min(a + 11 - x, b + 9 - x)
    a = memo
print(min(a, b))
