import sys

from collections import *

import heapq

import math

import bisect

from itertools import permutations,accumulate,combinations,product

from fractions import gcd

def input():

    return sys.stdin.readline()[:-1]

mod=pow(10,9)+7



n=eval(input())

# lst=[int(n[i]) for i in range(len(n))]

# for i in range(len(n)):

#     lst.append(int(n[i]))

# n=int(n)



keta=len(n)

ans=0

# lst.reverse()



# dp=[[float('inf')]*2 for i in range(keta+1)]

dp=[[0,1]]

for i in range(keta):

    dp.append([float('inf')]*2)

# dp[0]=[0,1]

# print(dp)

for i in range(keta):

    s=int(n[-1-i])

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

    dp[i+1][1]=min(dp[i][0]+(10-s),dp[i][1]+(10-s-1))

# print(dp)

print((min(dp[-1][0],dp[-1][1]+1)))
import sys

from collections import *

import heapq

import math

import bisect

from itertools import permutations,accumulate,combinations,product

from fractions import gcd

def input():

    return sys.stdin.readline()[:-1]

mod=pow(10,9)+7



n=eval(input())

keta=len(n)

ans=0



# dp=[[0,1]]

# for i in range(keta):

#     dp.append([float('inf')]*2)

dp0=[float('inf')]*(keta+1)

dp1=[float('inf')]*(keta+1)

dp0[0]=0

# dp[0]=[0,1]

# print(dp)

for i in range(keta):

    s=int(n[-1-i])

    dp0[i+1]=min(dp0[i]+s,dp1[i]+s+1)

    dp1[i+1]=min(dp0[i]+(10-s),dp1[i]+(10-s-1))

# print(dp)

print((min(dp0[-1],dp1[-1]+1)))
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations, accumulate, combinations, product
from fractions import gcd


def input():
    return sys.stdin.readline()[:-1]


mod = pow(10, 9) + 7
n = eval(input())
# lst=[int(n[i]) for i in range(len(n))]
# for i in range(len(n)):
#     lst.append(int(n[i]))
# n=int(n)
keta = len(n)
ans = 0
# lst.reverse()
# dp=[[float('inf')]*2 for i in range(keta+1)]
dp = [[0, 1]]
for i in range(keta):
    dp.append([float("inf")] * 2)
# dp[0]=[0,1]
# print(dp)
for i in range(keta):
    s = int(n[-1 - i])
    dp[i + 1][0] = min(dp[i][0] + s, dp[i][1] + s + 1)
    dp[i + 1][1] = min(dp[i][0] + (10 - s), dp[i][1] + (10 - s - 1))
# print(dp)
print((min(dp[-1][0], dp[-1][1] + 1)))

import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations, accumulate, combinations, product
from fractions import gcd


def input():
    return sys.stdin.readline()[:-1]


mod = pow(10, 9) + 7
n = eval(input())
keta = len(n)
ans = 0
# dp=[[0,1]]
# for i in range(keta):
#     dp.append([float('inf')]*2)
dp0 = [float("inf")] * (keta + 1)
dp1 = [float("inf")] * (keta + 1)
dp0[0] = 0
# dp[0]=[0,1]
# print(dp)
for i in range(keta):
    s = int(n[-1 - i])
    dp0[i + 1] = min(dp0[i] + s, dp1[i] + s + 1)
    dp1[i + 1] = min(dp0[i] + (10 - s), dp1[i] + (10 - s - 1))
# print(dp)
print((min(dp0[-1], dp1[-1] + 1)))

-# lst=[int(n[i]) for i in range(len(n))] -# for i in range(len(n)): -#     lst.append(int(n[i])) -# n=int(n) -# lst.reverse() -# dp=[[float('inf')]*2 for i in range(keta+1)] -dp = [[0, 1]] -for i in range(keta): -    dp.append([float("inf")] * 2) +# dp=[[0,1]] +# for i in range(keta): +#     dp.append([float('inf')]*2) +dp0 = [float("inf")] * (keta + 1) +dp1 = [float("inf")] * (keta + 1) +dp0[0] = 0 -    dp[i + 1][0] = min(dp[i][0] + s, dp[i][1] + s + 1) -    dp[i + 1][1] = min(dp[i][0] + (10 - s), dp[i][1] + (10 - s - 1)) +    dp0[i + 1] = min(dp0[i] + s, dp1[i] + s + 1) +    dp1[i + 1] = min(dp0[i] + (10 - s), dp1[i] + (10 - s - 1)) -print((min(dp[-1][0], dp[-1][1] + 1))) +print((min(dp0[-1], dp1[-1] + 1)))