S = ['0'] + list(input()) + ['0']
N = len(S)
flag = False

ans = 0

for i in range(N - 1, 0, -1):
    j = int(S[i])
    if flag:
        j += 1
        S[i] = j

    if j <= 5:
        if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False

    else:
        ans += 10 - j
        flag = True

if flag:
    ans += 1


# print (ans)
count = 0
for s in S:
    if s == 5:
        count += 1
    else:
        ans -= max(0, count - 2)
        count = 0

print (ans)
            


        
     S = ['0'] + list(input()) + ['0'] S ['0'] + list(input()) + ['0'] ['0'] + list(input()) ['0'] '0' list(input()) list input() input ['0'] '0' N = len(S) N len(S) len S flag = False flag False ans = 0 ans 0 for i in range(N - 1, 0, -1):
    j = int(S[i])
    if flag:
        j += 1
        S[i] = j

    if j <= 5:
        if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False

    else:
        ans += 10 - j
        flag = True i range(N - 1, 0, -1) range N - 1 N 1 0 -1 j = int(S[i]) j int(S[i]) int S[i] S i if flag:
        j += 1
        S[i] = j

     flag j += 1 j 1 S[i] = j S[i] S i j if j <= 5:
        if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False

    else:
        ans += 10 - j
        flag = True j <= 5 j 5 if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False

     j == 5 and int(S[i - 1]) >= 5 j == 5 j 5 int(S[i - 1]) >= 5 int(S[i - 1]) int S[i - 1] S i - 1 i 1 5 ans += j ans j flag = True flag True ans += j ans j flag = False flag False ans += 10 - j ans 10 - j 10 j flag = True flag True if flag:
    ans += 1


# print (ans) flag ans += 1 ans 1 count = 0 count 0 for s in S:
    if s == 5:
        count += 1
    else:
        ans -= max(0, count - 2)
        count = 0 s S if s == 5:
        count += 1
    else:
        ans -= max(0, count - 2)
        count = 0 s == 5 s 5 count += 1 count 1 ans -= max(0, count - 2) ans max(0, count - 2) max 0 count - 2 count 2 count = 0 count 0 print (ans) print ans flag = True True flag ans += j j ans flag = False False flag count = 0 0 count flag = False False flag count += 1 1 count ans -= max(0, count - 2) max(0, count - 2) ans flag = True True flag count = 0 0 count S = ['0'] + list(input()) + ['0'] ['0'] + list(input()) + ['0'] S ans = 0 0 ans ans += j j ans N = len(S) len(S) N ans += 10 - j 10 - j ans j = int(S[i]) int(S[i]) j j += 1 1 j ans += 1 1 ans
S = ['0'] + list(input()) + ['0']
N = len(S)
flag = False

ans = 0

for i in range(N - 1, 0, -1):
    j = int(S[i])
    if flag:
        j += 1
        S[i] = j

    if j <= 5:
        if j == 5 and int(S[i - 1]) >= 5:
            ans += j
            flag = True
        else:
            ans += j
            flag = False

    else:
        ans += 10 - j
        flag = True

if flag:
    ans += 1


# print (ans)
count = 0
for s in S:
    if s == 5:
        count += 1
    else:
        ans -= max(0, count - 2)
        count = 0

print (ans)
            


        
    
