N=input()
L=[int(N[i]) for i in range(len(N))]

P=0
OT=0

for i in range(len(L)-1):
    if 0 <= L[i] <= 3:
        P += L[i] + OT
        OT = 0

    elif L[i] == 4:
        if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1

Pone = 0
if OT==0:
    Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1]
else:
    Pone = 10-L[-1]

print(P+Pone) N=input() N input() input int(N[i]) for i in range(len(N)) for i in range(len(N)) i range(len(N)) range len(N) len N for i in range(len(N)) int(N[i]) int N[i] N i L=[int(N[i]) for i in range(len(N))] L [int(N[i]) for i in range(len(N))] P=0 P 0 OT=0 OT 0 for i in range(len(L)-1):
    if 0 <= L[i] <= 3:
        P += L[i] + OT
        OT = 0

    elif L[i] == 4:
        if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1 i range(len(L)-1) range len(L)-1 len(L) len L 1 if 0 <= L[i] <= 3:
        P += L[i] + OT
        OT = 0

    elif L[i] == 4:
        if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1 0 <= L[i] <= 3 0 <= L[i] 0 L[i] L i 3 P += L[i] + OT P L[i] + OT L[i] L i OT OT = 0 OT 0 elif L[i] == 4:
        if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

     L[i] == 4 L[i] L i 4 if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

     OT == 1 and L[i+1]>=5 OT == 1 OT 1 L[i+1]>=5 L[i+1] L i+1 i 1 5 P += 5 P 5 P += OT+L[i] P OT+L[i] OT L[i] L i OT = 0 OT 0 elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

     L[i] == 5 L[i] L i 5 if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

     OT == 1 OT 1 P += 4 P 4 if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

     L[i+1] >= 5 L[i+1] L i+1 i 1 5 P += 1 + 4 P 1 + 4 1 4 OT = 1 OT 1 P += 5 P 5 if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1 OT == 1 OT 1 P += 9-L[i] P 9-L[i] 9 L[i] L i OT = 1 OT 1 P += 10 - L[i] P 10 - L[i] 10 L[i] L i OT = 1 OT 1 Pone = 0 Pone 0 if OT==0:
    Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1]
else:
    Pone = 10-L[-1] OT==0 OT 0 Pone = min(L[-1],11-L[-1]) Pone min(L[-1],11-L[-1]) min L[-1] L -1 11-L[-1] 11 L[-1] L -1 elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1] OT==1 and L[-1]<=4 OT==1 OT 1 L[-1]<=4 L[-1] L -1 4 Pone = 1+L[-1] Pone 1+L[-1] 1 L[-1] L -1 Pone = 10-L[-1] Pone 10-L[-1] 10 L[-1] L -1 print(P+Pone) print P+Pone P Pone L=[int(N[i]) for i in range(len(N))] [int(N[i]) for i in range(len(N))] L P += 10 - L[i] 10 - L[i] P P=0 0 P P += 5 5 P Pone = 0 0 Pone Pone = 10-L[-1] 10-L[-1] Pone OT = 1 1 OT OT = 1 1 OT OT = 0 0 OT P += 4 4 P P += 1 + 4 1 + 4 P P += OT+L[i] OT+L[i] P Pone = 1+L[-1] 1+L[-1] Pone N=input() input() N OT = 0 0 OT P += 9-L[i] 9-L[i] P OT=0 0 OT P += L[i] + OT L[i] + OT P OT = 1 1 OT P += 5 5 P Pone = min(L[-1],11-L[-1]) min(L[-1],11-L[-1]) Pone
N=input()
L=[int(N[i]) for i in range(len(N))]

P=0
OT=0

for i in range(len(L)-1):
    if 0 <= L[i] <= 3:
        P += L[i] + OT
        OT = 0

    elif L[i] == 4:
        if OT == 1 and L[i+1]>=5:
            P += 5
        else:
            P += OT+L[i]
            OT = 0

    elif L[i] == 5:
        if OT == 1:
            P += 4

        else:
            if L[i+1] >= 5:
                P += 1 + 4
                OT = 1
            else:
                P += 5

    else:
        if OT == 1:
            P += 9-L[i]
            OT = 1
        else:
            P += 10 - L[i]
            OT = 1

Pone = 0
if OT==0:
    Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
    Pone = 1+L[-1]
else:
    Pone = 10-L[-1]

print(P+Pone)