
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = [int(x)-48 for x in read().rstrip()]

ans = 0

dp = [[0]*(len(N)+1) for i in range(2)]


dp[1][0] = 1
dp[0][0] = 0


for i in range(1, len(N)+1):
    dp[0][i] = min(dp[0][i-1] + N[i-1],
                   dp[1][i-1] + (10 - N[i-1]))

    dp[1][i] = min(dp[0][i-1] + N[i-1] + 1,
                   dp[1][i-1] + (10 - (N[i-1] + 1)))


print(dp[0][len(N)])
 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 int(x)-48 for x in read().rstrip() for x in read().rstrip() x read().rstrip() ().rstrip () read rstrip for x in read().rstrip() int(x)-48 int(x) int x 48 N = [int(x)-48 for x in read().rstrip()] N [int(x)-48 for x in read().rstrip()] ans = 0 ans 0 [0]*(len(N)+1) for i in range(2) for i in range(2) i range(2) range 2 for i in range(2) [0]*(len(N)+1) [0] 0 len(N)+1 len(N) len N 1 dp = [[0]*(len(N)+1) for i in range(2)] dp [[0]*(len(N)+1) for i in range(2)] dp[1][0] = 1 dp[1][0] [1] dp 1 0 1 dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(1, len(N)+1):
    dp[0][i] = min(dp[0][i-1] + N[i-1],
                   dp[1][i-1] + (10 - N[i-1]))

    dp[1][i] = min(dp[0][i-1] + N[i-1] + 1,
                   dp[1][i-1] + (10 - (N[i-1] + 1))) i range(1, len(N)+1) range 1 len(N)+1 len(N) len N 1 dp[0][i] = min(dp[0][i-1] + N[i-1],
                   dp[1][i-1] + (10 - N[i-1])) dp[0][i] [0] dp 0 i min(dp[0][i-1] + N[i-1],
                   dp[1][i-1] + (10 - N[i-1])) min dp[0][i-1] + N[i-1] dp[0][i-1] [0] dp 0 i-1 i 1 N[i-1] N i-1 i 1 dp[1][i-1] + (10 - N[i-1]) dp[1][i-1] [1] dp 1 i-1 i 1 10 - N[i-1] 10 N[i-1] N i-1 i 1 dp[1][i] = min(dp[0][i-1] + N[i-1] + 1,
                   dp[1][i-1] + (10 - (N[i-1] + 1))) dp[1][i] [1] dp 1 i min(dp[0][i-1] + N[i-1] + 1,
                   dp[1][i-1] + (10 - (N[i-1] + 1))) min dp[0][i-1] + N[i-1] + 1 dp[0][i-1] + N[i-1] dp[0][i-1] [0] dp 0 i-1 i 1 N[i-1] N i-1 i 1 1 dp[1][i-1] + (10 - (N[i-1] + 1)) dp[1][i-1] [1] dp 1 i-1 i 1 10 - (N[i-1] + 1) 10 N[i-1] + 1 N[i-1] N i-1 i 1 1 print(dp[0][len(N)]) print dp[0][len(N)] [0] dp 0 len(N) len N read = sys.stdin.buffer.read sys.stdin.buffer.read read ans = 0 0 ans N = [int(x)-48 for x in read().rstrip()] [int(x)-48 for x in read().rstrip()] N readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline dp = [[0]*(len(N)+1) for i in range(2)] [[0]*(len(N)+1) for i in range(2)] dp

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = [int(x)-48 for x in read().rstrip()]

ans = 0

dp = [[0]*(len(N)+1) for i in range(2)]


dp[1][0] = 1
dp[0][0] = 0


for i in range(1, len(N)+1):
    dp[0][i] = min(dp[0][i-1] + N[i-1],
                   dp[1][i-1] + (10 - N[i-1]))

    dp[1][i] = min(dp[0][i-1] + N[i-1] + 1,
                   dp[1][i-1] + (10 - (N[i-1] + 1)))


print(dp[0][len(N)])

