s = eval(input())

n = len(s)

dp = [ [0,1e18] for i in range(n+1) ]

for i in range(n):

  d = int(s[i])

  dp[i+1][0] = min(dp[i][0], dp[i][1])+d

  dp[i+1][1] = min(dp[i][0], dp[i][1]-2)+11-d

print((min(dp[n][0],dp[n][1])))
s = eval(input()); n = len(s); INF=1<<32

dp = [ [0,INF] for i in range(n+1) ]

for i in range(n):

  d = int(s[i])

  dp[i+1][0] = min(dp[i][0], dp[i][1])+d

  dp[i+1][1] = min(dp[i][0], dp[i][1]-2)+11-d

print((min(dp[n][0],dp[n][1])))
s = eval(input())
n = len(s)
dp = [[0, 1e18] for i in range(n + 1)]
for i in range(n):
    d = int(s[i])
    dp[i + 1][0] = min(dp[i][0], dp[i][1]) + d
    dp[i + 1][1] = min(dp[i][0], dp[i][1] - 2) + 11 - d
print((min(dp[n][0], dp[n][1])))

s = eval(input())
n = len(s)
INF = 1 << 32
dp = [[0, INF] for i in range(n + 1)]
for i in range(n):
    d = int(s[i])
    dp[i + 1][0] = min(dp[i][0], dp[i][1]) + d
    dp[i + 1][1] = min(dp[i][0], dp[i][1] - 2) + 11 - d
print((min(dp[n][0], dp[n][1])))

-dp = [[0, 1e18] for i in range(n + 1)] +INF = 1 << 32 +dp = [[0, INF] for i in range(n + 1)]