S = input()
kuri = False
ans = 0
for i in range(len(S)-1, -1, -1):
    K = int(S[i])
    if kuri:
        K += 1
    kuri = False
    if K < 5:
        ans += K
    elif K > 5:
        ans += (10-K)
        kuri = True
    elif K == 5:
        if i > 0:
            M = int(S[i-1])
            if i != 0 and M >= 5:
                kuri = True
        ans += 5
    if i == 0 and kuri:
        ans += 1
print(ans) S = input() S input() input kuri = False kuri False ans = 0 ans 0 for i in range(len(S)-1, -1, -1):
    K = int(S[i])
    if kuri:
        K += 1
    kuri = False
    if K < 5:
        ans += K
    elif K > 5:
        ans += (10-K)
        kuri = True
    elif K == 5:
        if i > 0:
            M = int(S[i-1])
            if i != 0 and M >= 5:
                kuri = True
        ans += 5
    if i == 0 and kuri:
        ans += 1 i range(len(S)-1, -1, -1) range len(S)-1 len(S) len S 1 -1 -1 K = int(S[i]) K int(S[i]) int S[i] S i if kuri:
        K += 1
     kuri K += 1 K 1 kuri = False kuri False if K < 5:
        ans += K
    elif K > 5:
        ans += (10-K)
        kuri = True
    elif K == 5:
        if i > 0:
            M = int(S[i-1])
            if i != 0 and M >= 5:
                kuri = True
        ans += 5
     K < 5 K 5 ans += K ans K elif K > 5:
        ans += (10-K)
        kuri = True
     K > 5 K 5 ans += (10-K) ans 10-K 10 K kuri = True kuri True elif K == 5:
        if i > 0:
            M = int(S[i-1])
            if i != 0 and M >= 5:
                kuri = True
        ans += 5
     K == 5 K 5 if i > 0:
            M = int(S[i-1])
            if i != 0 and M >= 5:
                kuri = True
         i > 0 i 0 M = int(S[i-1]) M int(S[i-1]) int S[i-1] S i-1 i 1 if i != 0 and M >= 5:
                kuri = True
         i != 0 and M >= 5 i != 0 i 0 M >= 5 M 5 kuri = True kuri True ans += 5 ans 5 if i == 0 and kuri:
        ans += 1 i == 0 and kuri i == 0 i 0 kuri ans += 1 ans 1 print(ans) print ans ans += (10-K) 10-K ans kuri = True True kuri ans += 5 5 ans M = int(S[i-1]) int(S[i-1]) M kuri = False False kuri kuri = False False kuri ans = 0 0 ans K = int(S[i]) int(S[i]) K K += 1 1 K S = input() input() S kuri = True True kuri ans += K K ans ans += 1 1 ans
S = input()
kuri = False
ans = 0
for i in range(len(S)-1, -1, -1):
    K = int(S[i])
    if kuri:
        K += 1
    kuri = False
    if K < 5:
        ans += K
    elif K > 5:
        ans += (10-K)
        kuri = True
    elif K == 5:
        if i > 0:
            M = int(S[i-1])
            if i != 0 and M >= 5:
                kuri = True
        ans += 5
    if i == 0 and kuri:
        ans += 1
print(ans)
