import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc155/tasks/abc155_e

# dp?
# もしXiが6より大きかったらX(i-1)という上の桁10借りてくればお釣りで枚数が減る
# そうなると上位桁では1枚増えるけどi桁では減る
# 解けなかったね ピッタリ払うときと一枚余分に払うときというアイデアは合ってたけど、一枚余分はdpの数字に反映させるんじゃなくて、前の桁に反映させよう


N = '0' + read()[:-1]
pre_is_kuriagari = False
dp1 = int(N[0])
dp2 = dp1 + 1
for i in ra(1, len(N)):
    n = int(N[i])
    # ピッタリ払うときの最小
    oturi = dp2 + (10 - n)
    pay = dp1 + n
    dp1_new = min(oturi, pay)

    # 1多く払うときの最小
    oturi = dp2 + (10 - (n + 1))
    pay = dp1 + n + 1
    dp2_new = min(oturi, pay)
    dp1, dp2 = dp1_new, dp2_new
print(dp1) import sys sys sys.setrecursionlimit(1 << 25) sys.setrecursionlimit sys setrecursionlimit 1 << 25 1 25 read = sys.stdin.readline read sys.stdin.readline sys.stdin sys stdin readline ra = range ra range enu = enumerate enu enumerate def read_ints():
    return list(map(int, read().split())) read_ints return list(map(int, read().split())) list(map(int, read().split())) list map(int, read().split()) map int read().split() ().split () read split def read_a_int():
    return int(read()) read_a_int return int(read()) int(read()) int read() read def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret read_tuple '''
    H is number of rows
    ''' ret = [] ret [] for _ in range(H):
        ret.append(tuple(map(int, read().split())))
     _ range(H) range H ret.append(tuple(map(int, read().split()))) ret.append ret append tuple(map(int, read().split())) tuple map(int, read().split()) map int read().split() ().split () read split return ret ret H H def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret))) read_col '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    ''' ret = [] ret [] for _ in range(H):
        ret.append(list(map(int, read().split())))
     _ range(H) range H ret.append(list(map(int, read().split()))) ret.append ret append list(map(int, read().split())) list map(int, read().split()) map int read().split() ().split () read split return tuple(map(list, zip(*ret))) tuple(map(list, zip(*ret))) tuple map(list, zip(*ret)) map list zip(*ret) zip *ret ret H H def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため read_matrix '''
    H is number of rows
    ''' ret = [] ret [] for _ in range(H):
        ret.append(list(map(int, read().split())))
     _ range(H) range H ret.append(list(map(int, read().split()))) ret.append ret append list(map(int, read().split())) list map(int, read().split()) map int read().split() ().split () read split return ret ret H H MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 INF = 2**31 INF 2**31 2 31 from collections import defaultdict, Counter, deque from operator import itemgetter from itertools import product, permutations, combinations from bisect import bisect_left, bisect_right N = '0' + read()[:-1] N '0' + read()[:-1] '0' read()[:-1] () read :-1 -1 pre_is_kuriagari = False pre_is_kuriagari False dp1 = int(N[0]) dp1 int(N[0]) int N[0] N 0 dp2 = dp1 + 1 dp2 dp1 + 1 dp1 1 for i in ra(1, len(N)):
    n = int(N[i])
    # ピッタリ払うときの最小
    oturi = dp2 + (10 - n)
    pay = dp1 + n
    dp1_new = min(oturi, pay)

    # 1多く払うときの最小
    oturi = dp2 + (10 - (n + 1))
    pay = dp1 + n + 1
    dp2_new = min(oturi, pay)
    dp1, dp2 = dp1_new, dp2_new i ra(1, len(N)) ra 1 len(N) len N n = int(N[i]) n int(N[i]) int N[i] N i oturi = dp2 + (10 - n) oturi dp2 + (10 - n) dp2 10 - n 10 n pay = dp1 + n pay dp1 + n dp1 n dp1_new = min(oturi, pay) dp1_new min(oturi, pay) min oturi pay oturi = dp2 + (10 - (n + 1)) oturi dp2 + (10 - (n + 1)) dp2 10 - (n + 1) 10 n + 1 n 1 pay = dp1 + n + 1 pay dp1 + n + 1 dp1 + n dp1 n 1 dp2_new = min(oturi, pay) dp2_new min(oturi, pay) min oturi pay dp1, dp2 = dp1_new, dp2_new dp1 dp1_new dp2 dp2_new print(dp1) print dp1 def read_a_int():
    return int(read()) def read_a_int():
    return int(read()) read_a_int pre_is_kuriagari = False False pre_is_kuriagari INF = 2**31 2**31 INF oturi = dp2 + (10 - n) dp2 + (10 - n) oturi def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため read_matrix dp2_new = min(oturi, pay) min(oturi, pay) dp2_new read = sys.stdin.readline sys.stdin.readline read oturi = dp2 + (10 - (n + 1)) dp2 + (10 - (n + 1)) oturi dp2 = dp1_new, dp2_new dp2_new dp2 pay = dp1 + n + 1 dp1 + n + 1 pay dp1_new = min(oturi, pay) min(oturi, pay) dp1_new def read_ints():
    return list(map(int, read().split())) def read_ints():
    return list(map(int, read().split())) read_ints n = int(N[i]) int(N[i]) n pay = dp1 + n dp1 + n pay N = '0' + read()[:-1] '0' + read()[:-1] N dp1 = int(N[0]) int(N[0]) dp1 MOD = 10**9 + 7 10**9 + 7 MOD dp2 = dp1 + 1 dp1 + 1 dp2 def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret read_tuple def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret))) def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret))) read_col enu = enumerate enumerate enu dp1, dp2 = dp1_new, dp2_new dp1_new dp1 ra = range range ra
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc155/tasks/abc155_e

# dp?
# もしXiが6より大きかったらX(i-1)という上の桁10借りてくればお釣りで枚数が減る
# そうなると上位桁では1枚増えるけどi桁では減る
# 解けなかったね ピッタリ払うときと一枚余分に払うときというアイデアは合ってたけど、一枚余分はdpの数字に反映させるんじゃなくて、前の桁に反映させよう


N = '0' + read()[:-1]
pre_is_kuriagari = False
dp1 = int(N[0])
dp2 = dp1 + 1
for i in ra(1, len(N)):
    n = int(N[i])
    # ピッタリ払うときの最小
    oturi = dp2 + (10 - n)
    pay = dp1 + n
    dp1_new = min(oturi, pay)

    # 1多く払うときの最小
    oturi = dp2 + (10 - (n + 1))
    pay = dp1 + n + 1
    dp2_new = min(oturi, pay)
    dp1, dp2 = dp1_new, dp2_new
print(dp1)
