# https://atcoder.jp/contests/abc155/submissions/10157837
# 写経

def main():
    a = [0]
    a += map(int, input())
    a.reverse()

    n = len(a)
    b = a.copy()

    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt)


if __name__ == '__main__':
    main() def main():
    a = [0]
    a += map(int, input())
    a.reverse()

    n = len(a)
    b = a.copy()

    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt) main a = [0] a [0] 0 a += map(int, input()) a map(int, input()) map int input() input a.reverse() a.reverse a reverse n = len(a) n len(a) len a b = a.copy() b a.copy() a.copy a copy for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

     i range(n) range n if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

     b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5) b[i] > 5 b[i] b i 5 b[i] == 5 and b[i + 1] >= 5 b[i] == 5 b[i] b i 5 b[i + 1] >= 5 b[i + 1] b i + 1 i 1 5 b[i] = 0 b[i] b i 0 b[i + 1] += 1 b[i + 1] b i + 1 i 1 1 cnt = sum(b) cnt sum(b) sum b for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
     i range(n) range n if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
         a[i] > b[i] a[i] a i b[i] b i b[i] += 10 b[i] b i 10 b[i + 1] -= 1 b[i + 1] b i + 1 i 1 1 cnt += b[i] - a[i] cnt b[i] - a[i] b[i] b i a[i] a i print(cnt) print cnt if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    a = [0]
    a += map(int, input())
    a.reverse()

    n = len(a)
    b = a.copy()

    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt) def main():
    a = [0]
    a += map(int, input())
    a.reverse()

    n = len(a)
    b = a.copy()

    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt) main
# https://atcoder.jp/contests/abc155/submissions/10157837
# 写経

def main():
    a = [0]
    a += map(int, input())
    a.reverse()

    n = len(a)
    b = a.copy()

    for i in range(n):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1

    cnt = sum(b)
    for i in range(n):
        if a[i] > b[i]:
            b[i] += 10
            b[i + 1] -= 1
        cnt += b[i] - a[i]
    print(cnt)


if __name__ == '__main__':
    main()
