#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = input().strip()[::-1]
  dp = [[0, 0] for _ in range(len(n))]
  dp[0][0] = int(n[0])
  dp[0][1] = 10 - int(n[0])
  for i, d in enumerate(map(int, n[1:]), 1):
    dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d)
    dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1))
  print(min(dp[-1][0], dp[-1][1]+1))  

if __name__=='__main__':
  main()
 def main():
  n = input().strip()[::-1]
  dp = [[0, 0] for _ in range(len(n))]
  dp[0][0] = int(n[0])
  dp[0][1] = 10 - int(n[0])
  for i, d in enumerate(map(int, n[1:]), 1):
    dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d)
    dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1))
  print(min(dp[-1][0], dp[-1][1]+1))   main n = input().strip()[::-1] n input().strip()[::-1] ().strip() ().strip () input strip ::-1 -1 [0, 0] for _ in range(len(n)) for _ in range(len(n)) _ range(len(n)) range len(n) len n for _ in range(len(n)) [0, 0] 0 0 dp = [[0, 0] for _ in range(len(n))] dp [[0, 0] for _ in range(len(n))] dp[0][0] = int(n[0]) dp[0][0] [0] dp 0 0 int(n[0]) int n[0] n 0 dp[0][1] = 10 - int(n[0]) dp[0][1] [0] dp 0 1 10 - int(n[0]) 10 int(n[0]) int n[0] n 0 for i, d in enumerate(map(int, n[1:]), 1):
    dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d)
    dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1))
   i d enumerate(map(int, n[1:]), 1) enumerate map(int, n[1:]) map int n[1:] n 1: 1 1 dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d) dp[i][0] [i] dp i 0 min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d) min dp[i-1][0] + d dp[i-1][0] [i-1] dp i-1 i 1 0 d dp[i-1][1] + 1 + d dp[i-1][1] + 1 dp[i-1][1] [i-1] dp i-1 i 1 1 1 d dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1)) dp[i][1] [i] dp i 1 min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1)) min dp[i-1][0] + 10 - d dp[i-1][0] + 10 dp[i-1][0] [i-1] dp i-1 i 1 0 10 d dp[i-1][1] + 10 - (d + 1) dp[i-1][1] + 10 dp[i-1][1] [i-1] dp i-1 i 1 1 10 d + 1 d 1 print(min(dp[-1][0], dp[-1][1]+1)) print min(dp[-1][0], dp[-1][1]+1) min dp[-1][0] [-1] dp -1 0 dp[-1][1]+1 dp[-1][1] [-1] dp -1 1 1 if __name__=='__main__':
  main() __name__=='__main__' __name__ '__main__' main() main def main():
  n = input().strip()[::-1]
  dp = [[0, 0] for _ in range(len(n))]
  dp[0][0] = int(n[0])
  dp[0][1] = 10 - int(n[0])
  for i, d in enumerate(map(int, n[1:]), 1):
    dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d)
    dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1))
  print(min(dp[-1][0], dp[-1][1]+1))   def main():
  n = input().strip()[::-1]
  dp = [[0, 0] for _ in range(len(n))]
  dp[0][0] = int(n[0])
  dp[0][1] = 10 - int(n[0])
  for i, d in enumerate(map(int, n[1:]), 1):
    dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d)
    dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1))
  print(min(dp[-1][0], dp[-1][1]+1))   main
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = input().strip()[::-1]
  dp = [[0, 0] for _ in range(len(n))]
  dp[0][0] = int(n[0])
  dp[0][1] = 10 - int(n[0])
  for i, d in enumerate(map(int, n[1:]), 1):
    dp[i][0] = min(dp[i-1][0] + d,      dp[i-1][1] + 1 + d)
    dp[i][1] = min(dp[i-1][0] + 10 - d, dp[i-1][1] + 10 - (d + 1))
  print(min(dp[-1][0], dp[-1][1]+1))  

if __name__=='__main__':
  main()

