from sys import stdin

N = stdin.readline().rstrip()
ans = 0

N = N[::-1]
N = N + '0'
kuriagari = 0

for i in range(len(N)):
    r = int(N[i])
    if kuriagari == 1:
        r += 1
    if 10 - r < r:
        kuriagari = 1
    else:
        kuriagari = 0
    if r == 5 and int(N[i + 1]) > 4:
        kuriagari = 1
    if r == 10:
        r = 0
    ans += min(r, 10 - r)

print(ans) from sys import stdin N = stdin.readline().rstrip() N stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip ans = 0 ans 0 N = N[::-1] N N[::-1] N ::-1 -1 N = N + '0' N N + '0' N '0' kuriagari = 0 kuriagari 0 for i in range(len(N)):
    r = int(N[i])
    if kuriagari == 1:
        r += 1
    if 10 - r < r:
        kuriagari = 1
    else:
        kuriagari = 0
    if r == 5 and int(N[i + 1]) > 4:
        kuriagari = 1
    if r == 10:
        r = 0
    ans += min(r, 10 - r) i range(len(N)) range len(N) len N r = int(N[i]) r int(N[i]) int N[i] N i if kuriagari == 1:
        r += 1
     kuriagari == 1 kuriagari 1 r += 1 r 1 if 10 - r < r:
        kuriagari = 1
    else:
        kuriagari = 0
     10 - r < r 10 - r 10 r r kuriagari = 1 kuriagari 1 kuriagari = 0 kuriagari 0 if r == 5 and int(N[i + 1]) > 4:
        kuriagari = 1
     r == 5 and int(N[i + 1]) > 4 r == 5 r 5 int(N[i + 1]) > 4 int(N[i + 1]) int N[i + 1] N i + 1 i 1 4 kuriagari = 1 kuriagari 1 if r == 10:
        r = 0
     r == 10 r 10 r = 0 r 0 ans += min(r, 10 - r) ans min(r, 10 - r) min r 10 - r 10 r print(ans) print ans kuriagari = 0 0 kuriagari ans = 0 0 ans N = N + '0' N + '0' N r += 1 1 r r = 0 0 r N = stdin.readline().rstrip() stdin.readline().rstrip() N kuriagari = 0 0 kuriagari kuriagari = 1 1 kuriagari kuriagari = 1 1 kuriagari N = N[::-1] N[::-1] N ans += min(r, 10 - r) min(r, 10 - r) ans r = int(N[i]) int(N[i]) r
from sys import stdin

N = stdin.readline().rstrip()
ans = 0

N = N[::-1]
N = N + '0'
kuriagari = 0

for i in range(len(N)):
    r = int(N[i])
    if kuriagari == 1:
        r += 1
    if 10 - r < r:
        kuriagari = 1
    else:
        kuriagari = 0
    if r == 5 and int(N[i + 1]) > 4:
        kuriagari = 1
    if r == 10:
        r = 0
    ans += min(r, 10 - r)

print(ans)
