s = input().rstrip()
s = s[::-1]
n = len(s)
dp = [[0]*2 for _ in range(n+1)]
for i in range(1,n+1):
  if i == 1:
    dp[1][0] = int(s[0])
    dp[1][1] = 10-int(s[0])
  else:
    dp[i][0] = min(dp[i-1][0]+int(s[i-1]),dp[i-1][1]+int(s[i-1])+1)
    dp[i][1] = min(dp[i-1][0]+10-int(s[i-1]),dp[i-1][1]+9-int(s[i-1]))
print(min(dp[n][0],dp[n][1]+1)) s = input().rstrip() s input().rstrip() ().rstrip () input rstrip s = s[::-1] s s[::-1] s ::-1 -1 n = len(s) n len(s) len s [0]*2 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [0]*2 [0] 0 2 dp = [[0]*2 for _ in range(n+1)] dp [[0]*2 for _ in range(n+1)] for i in range(1,n+1):
  if i == 1:
    dp[1][0] = int(s[0])
    dp[1][1] = 10-int(s[0])
  else:
    dp[i][0] = min(dp[i-1][0]+int(s[i-1]),dp[i-1][1]+int(s[i-1])+1)
    dp[i][1] = min(dp[i-1][0]+10-int(s[i-1]),dp[i-1][1]+9-int(s[i-1])) i range(1,n+1) range 1 n+1 n 1 if i == 1:
    dp[1][0] = int(s[0])
    dp[1][1] = 10-int(s[0])
  else:
    dp[i][0] = min(dp[i-1][0]+int(s[i-1]),dp[i-1][1]+int(s[i-1])+1)
    dp[i][1] = min(dp[i-1][0]+10-int(s[i-1]),dp[i-1][1]+9-int(s[i-1])) i == 1 i 1 dp[1][0] = int(s[0]) dp[1][0] [1] dp 1 0 int(s[0]) int s[0] s 0 dp[1][1] = 10-int(s[0]) dp[1][1] [1] dp 1 1 10-int(s[0]) 10 int(s[0]) int s[0] s 0 dp[i][0] = min(dp[i-1][0]+int(s[i-1]),dp[i-1][1]+int(s[i-1])+1) dp[i][0] [i] dp i 0 min(dp[i-1][0]+int(s[i-1]),dp[i-1][1]+int(s[i-1])+1) min dp[i-1][0]+int(s[i-1]) dp[i-1][0] [i-1] dp i-1 i 1 0 int(s[i-1]) int s[i-1] s i-1 i 1 dp[i-1][1]+int(s[i-1])+1 dp[i-1][1]+int(s[i-1]) dp[i-1][1] [i-1] dp i-1 i 1 1 int(s[i-1]) int s[i-1] s i-1 i 1 1 dp[i][1] = min(dp[i-1][0]+10-int(s[i-1]),dp[i-1][1]+9-int(s[i-1])) dp[i][1] [i] dp i 1 min(dp[i-1][0]+10-int(s[i-1]),dp[i-1][1]+9-int(s[i-1])) min dp[i-1][0]+10-int(s[i-1]) dp[i-1][0]+10 dp[i-1][0] [i-1] dp i-1 i 1 0 10 int(s[i-1]) int s[i-1] s i-1 i 1 dp[i-1][1]+9-int(s[i-1]) dp[i-1][1]+9 dp[i-1][1] [i-1] dp i-1 i 1 1 9 int(s[i-1]) int s[i-1] s i-1 i 1 print(min(dp[n][0],dp[n][1]+1)) print min(dp[n][0],dp[n][1]+1) min dp[n][0] [n] dp n 0 dp[n][1]+1 dp[n][1] [n] dp n 1 1 s = s[::-1] s[::-1] s n = len(s) len(s) n dp = [[0]*2 for _ in range(n+1)] [[0]*2 for _ in range(n+1)] dp s = input().rstrip() input().rstrip() s
s = input().rstrip()
s = s[::-1]
n = len(s)
dp = [[0]*2 for _ in range(n+1)]
for i in range(1,n+1):
  if i == 1:
    dp[1][0] = int(s[0])
    dp[1][1] = 10-int(s[0])
  else:
    dp[i][0] = min(dp[i-1][0]+int(s[i-1]),dp[i-1][1]+int(s[i-1])+1)
    dp[i][1] = min(dp[i-1][0]+10-int(s[i-1]),dp[i-1][1]+9-int(s[i-1]))
print(min(dp[n][0],dp[n][1]+1))