def main():
    n = input()
    l = len(n)
    ans = 0
    p = 0

    for i in range(l-1, -1, -1):
        v = int(n[i])
        if p == 1:
            v += 1
        if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
    if p == 1:
        ans += 1

    print(ans)
main() def main():
    n = input()
    l = len(n)
    ans = 0
    p = 0

    for i in range(l-1, -1, -1):
        v = int(n[i])
        if p == 1:
            v += 1
        if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
    if p == 1:
        ans += 1

    print(ans) main n = input() n input() input l = len(n) l len(n) len n ans = 0 ans 0 p = 0 p 0 for i in range(l-1, -1, -1):
        v = int(n[i])
        if p == 1:
            v += 1
        if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
     i range(l-1, -1, -1) range l-1 l 1 -1 -1 v = int(n[i]) v int(n[i]) int n[i] n i if p == 1:
            v += 1
         p == 1 p 1 v += 1 v 1 if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
     v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5) v > 5 v 5 i != 0 and v == 5 and int(n[i-1]) >= 5 i != 0 and v == 5 i != 0 i 0 v == 5 v 5 int(n[i-1]) >= 5 int(n[i-1]) int n[i-1] n i-1 i 1 5 ans += (10-v) ans 10-v 10 v p = 1 p 1 ans += v ans v p = 0 p 0 if p == 1:
        ans += 1

     p == 1 p 1 ans += 1 ans 1 print(ans) print ans main() main def main():
    n = input()
    l = len(n)
    ans = 0
    p = 0

    for i in range(l-1, -1, -1):
        v = int(n[i])
        if p == 1:
            v += 1
        if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
    if p == 1:
        ans += 1

    print(ans) def main():
    n = input()
    l = len(n)
    ans = 0
    p = 0

    for i in range(l-1, -1, -1):
        v = int(n[i])
        if p == 1:
            v += 1
        if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
    if p == 1:
        ans += 1

    print(ans) main
def main():
    n = input()
    l = len(n)
    ans = 0
    p = 0

    for i in range(l-1, -1, -1):
        v = int(n[i])
        if p == 1:
            v += 1
        if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
            ans += (10-v)
            p = 1
        else:
            ans += v
            p = 0
    if p == 1:
        ans += 1

    print(ans)
main()