
N = input()
N = N[::-1]

c = 0
otsuri = 0
mai = 0
for i in range(len(N)):
    if int(N[i]) + c == 5 and i < len(N) - 1:
        if int(N[i+1]) >= 5:
            otsuri += 5
            c = 1
        else:
            mai += int(N[i]) + c
            c = 0
    elif int(N[i]) + c > 5:
        otsuri += 10 - int(N[i]) - c
        c = 1
    else:
        mai += int(N[i]) + c
        c = 0

print(otsuri + mai + c) N = input() N input() input N = N[::-1] N N[::-1] N ::-1 -1 c = 0 c 0 otsuri = 0 otsuri 0 mai = 0 mai 0 for i in range(len(N)):
    if int(N[i]) + c == 5 and i < len(N) - 1:
        if int(N[i+1]) >= 5:
            otsuri += 5
            c = 1
        else:
            mai += int(N[i]) + c
            c = 0
    elif int(N[i]) + c > 5:
        otsuri += 10 - int(N[i]) - c
        c = 1
    else:
        mai += int(N[i]) + c
        c = 0 i range(len(N)) range len(N) len N if int(N[i]) + c == 5 and i < len(N) - 1:
        if int(N[i+1]) >= 5:
            otsuri += 5
            c = 1
        else:
            mai += int(N[i]) + c
            c = 0
    elif int(N[i]) + c > 5:
        otsuri += 10 - int(N[i]) - c
        c = 1
    else:
        mai += int(N[i]) + c
        c = 0 int(N[i]) + c == 5 and i < len(N) - 1 int(N[i]) + c == 5 int(N[i]) + c int(N[i]) int N[i] N i c 5 i < len(N) - 1 i len(N) - 1 len(N) len N 1 if int(N[i+1]) >= 5:
            otsuri += 5
            c = 1
        else:
            mai += int(N[i]) + c
            c = 0
     int(N[i+1]) >= 5 int(N[i+1]) int N[i+1] N i+1 i 1 5 otsuri += 5 otsuri 5 c = 1 c 1 mai += int(N[i]) + c mai int(N[i]) + c int(N[i]) int N[i] N i c c = 0 c 0 elif int(N[i]) + c > 5:
        otsuri += 10 - int(N[i]) - c
        c = 1
     int(N[i]) + c > 5 int(N[i]) + c int(N[i]) int N[i] N i c 5 otsuri += 10 - int(N[i]) - c otsuri 10 - int(N[i]) - c 10 - int(N[i]) 10 int(N[i]) int N[i] N i c c = 1 c 1 mai += int(N[i]) + c mai int(N[i]) + c int(N[i]) int N[i] N i c c = 0 c 0 print(otsuri + mai + c) print otsuri + mai + c otsuri + mai otsuri mai c c = 1 1 c N = N[::-1] N[::-1] N c = 1 1 c mai = 0 0 mai mai += int(N[i]) + c int(N[i]) + c mai otsuri = 0 0 otsuri c = 0 0 c N = input() input() N otsuri += 5 5 otsuri c = 0 0 c mai += int(N[i]) + c int(N[i]) + c mai c = 0 0 c otsuri += 10 - int(N[i]) - c 10 - int(N[i]) - c otsuri

N = input()
N = N[::-1]

c = 0
otsuri = 0
mai = 0
for i in range(len(N)):
    if int(N[i]) + c == 5 and i < len(N) - 1:
        if int(N[i+1]) >= 5:
            otsuri += 5
            c = 1
        else:
            mai += int(N[i]) + c
            c = 0
    elif int(N[i]) + c > 5:
        otsuri += 10 - int(N[i]) - c
        c = 1
    else:
        mai += int(N[i]) + c
        c = 0

print(otsuri + mai + c)