s = "0"+input()
ans = 0
n = len(s)
f = 0
p = 0
for i in range(1,n+1):
    n = int(s[-i])
    # n += f
    if p+(n>4) > 5:
        f = 1
    else:
        # ans += f
        f = 0
    n += f
    ans += min(n,10-n)
    p = n
# ans += f
print(ans) s = "0"+input() s "0"+input() "0" input() input ans = 0 ans 0 n = len(s) n len(s) len s f = 0 f 0 p = 0 p 0 for i in range(1,n+1):
    n = int(s[-i])
    # n += f
    if p+(n>4) > 5:
        f = 1
    else:
        # ans += f
        f = 0
    n += f
    ans += min(n,10-n)
    p = n
# ans += f i range(1,n+1) range 1 n+1 n 1 n = int(s[-i]) n int(s[-i]) int s[-i] s -i i if p+(n>4) > 5:
        f = 1
    else:
        # ans += f
        f = 0
     p+(n>4) > 5 p+(n>4) p n>4 n 4 5 f = 1 f 1 f = 0 f 0 n += f n f ans += min(n,10-n) ans min(n,10-n) min n 10-n 10 n p = n p n print(ans) print ans s = "0"+input() "0"+input() s f = 0 0 f ans += min(n,10-n) min(n,10-n) ans f = 1 1 f n = len(s) len(s) n f = 0 0 f ans = 0 0 ans n = int(s[-i]) int(s[-i]) n p = 0 0 p n += f f n p = n n p
s = "0"+input()
ans = 0
n = len(s)
f = 0
p = 0
for i in range(1,n+1):
    n = int(s[-i])
    # n += f
    if p+(n>4) > 5:
        f = 1
    else:
        # ans += f
        f = 0
    n += f
    ans += min(n,10-n)
    p = n
# ans += f
print(ans)