import sys
input =sys.stdin.readline
def solve():
  s = input().rstrip()
  n = len(s)
  dp = [[float("Inf")]*2 for _ in range(n+1)]
  dp[0][0] = 0
  dp[0][1] = 1

  for i in range(0,n):
    number = int(s[i])

    dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]])
    if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
    dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0])
    dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1])
        
  #print(dp)
  print(min(dp[n][0],dp[n][1]+1))
solve() import sys sys input =sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def solve():
  s = input().rstrip()
  n = len(s)
  dp = [[float("Inf")]*2 for _ in range(n+1)]
  dp[0][0] = 0
  dp[0][1] = 1

  for i in range(0,n):
    number = int(s[i])

    dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]])
    if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
    dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0])
    dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1])
        
  #print(dp)
  print(min(dp[n][0],dp[n][1]+1)) solve s = input().rstrip() s input().rstrip() ().rstrip () input rstrip n = len(s) n len(s) len s [float("Inf")]*2 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [float("Inf")]*2 [float("Inf")] float("Inf") float "Inf" 2 dp = [[float("Inf")]*2 for _ in range(n+1)] dp [[float("Inf")]*2 for _ in range(n+1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(0,n):
    number = int(s[i])

    dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]])
    if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
    dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0])
    dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1])
        
  #print(dp)
   i range(0,n) range 0 n number = int(s[i]) number int(s[i]) int s[i] s i dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]]) dp[i+1][0] [i+1] dp i+1 i 1 0 min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]]) min [dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]] dp[i][0]+number dp[i][0] [i] dp i 0 number dp[i][0]+(10-number)+1 dp[i][0]+(10-number) dp[i][0] [i] dp i 0 10-number 10 number 1 dp[i+1][0] [i+1] dp i+1 i 1 0 if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
     number != 9 number 9 dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]]) dp[i+1][1] [i+1] dp i+1 i 1 1 min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]]) min [dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]] dp[i][0]+number+1 dp[i][0]+number dp[i][0] [i] dp i 0 number 1 dp[i][0]+(10-number) dp[i][0] [i] dp i 0 10-number 10 number dp[i+1][1] [i+1] dp i+1 i 1 1 dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0]) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][1] +(10-number),dp[i+1][0]) min dp[i][1] +(10-number) dp[i][1] [i] dp i 1 10-number 10 number dp[i+1][0] [i+1] dp i+1 i 1 0 dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1]) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][1] + (10-number-1),dp[i+1][1]) min dp[i][1] + (10-number-1) dp[i][1] [i] dp i 1 10-number-1 10-number 10 number 1 dp[i+1][1] [i+1] dp i+1 i 1 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 solve() solve input =sys.stdin.readline sys.stdin.readline input def solve():
  s = input().rstrip()
  n = len(s)
  dp = [[float("Inf")]*2 for _ in range(n+1)]
  dp[0][0] = 0
  dp[0][1] = 1

  for i in range(0,n):
    number = int(s[i])

    dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]])
    if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
    dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0])
    dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1])
        
  #print(dp)
  print(min(dp[n][0],dp[n][1]+1)) def solve():
  s = input().rstrip()
  n = len(s)
  dp = [[float("Inf")]*2 for _ in range(n+1)]
  dp[0][0] = 0
  dp[0][1] = 1

  for i in range(0,n):
    number = int(s[i])

    dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]])
    if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
    dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0])
    dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1])
        
  #print(dp)
  print(min(dp[n][0],dp[n][1]+1)) solve
import sys
input =sys.stdin.readline
def solve():
  s = input().rstrip()
  n = len(s)
  dp = [[float("Inf")]*2 for _ in range(n+1)]
  dp[0][0] = 0
  dp[0][1] = 1

  for i in range(0,n):
    number = int(s[i])

    dp[i+1][0] = min([dp[i][0]+number,dp[i][0]+(10-number)+1,dp[i+1][0]])
    if number != 9:
      dp[i+1][1] = min([dp[i][0]+number+1,dp[i][0]+(10-number),dp[i+1][1]])
     
    dp[i+1][0] = min(dp[i][1] +(10-number),dp[i+1][0])
    dp[i+1][1] = min(dp[i][1] + (10-number-1),dp[i+1][1])
        
  #print(dp)
  print(min(dp[n][0],dp[n][1]+1))
solve()