def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        
    print(dp0)
 
 
if __name__ == '__main__':
    main() def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        
    print(dp0)
 
  main int(x) for x in input() for x in input() x input() input for x in input() int(x) int x N = [int(x) for x in input()] N [int(x) for x in input()] dp0 = 0 dp0 0 dp1 = 1 dp1 1 for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        
     n N a = min(dp0 + n, dp1 + 10 - n) a min(dp0 + n, dp1 + 10 - n) min dp0 + n dp0 n dp1 + 10 - n dp1 + 10 dp1 10 n b = min(dp0 + n + 1, dp1 + 10 - (n+1)) b min(dp0 + n + 1, dp1 + 10 - (n+1)) min dp0 + n + 1 dp0 + n dp0 n 1 dp1 + 10 - (n+1) dp1 + 10 dp1 10 n+1 n 1 dp0, dp1 = a, b dp0 a dp1 b print(dp0) print dp0 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        
    print(dp0)
 
  def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        
    print(dp0)
 
  main
def main():
    N = [int(x) for x in input()]
    dp0 = 0
    dp1 = 1
    for n in N:
        # そのまま払う
        a = min(dp0 + n, dp1 + 10 - n)
        # 1多めに払う
        b = min(dp0 + n + 1, dp1 + 10 - (n+1))
        dp0, dp1 = a, b
        
    print(dp0)
 
 
if __name__ == '__main__':
    main()