import math


def count(N, X):
    cnt = sum(map(int, str(X))) + sum(map(int, str(X - N)))
    return cnt


def solve_naive_first(N):
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive(N), 1+solve_naive(p*10-N))


def solve_naive(N):
    if N < 10:
        return min(N, 10-N+1)
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p)


def solve(N):
    n_0, n_1 = 0, 1  # 0: その桁で処理, 1: 次の桁に1タス
    for i in N[::-1]:
        i = int(i)
        if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
    return min(n_0, n_1+1)


if __name__ == '__main__':
    N = input()
    ans = solve(N)
    print(ans) import math math def count(N, X):
    cnt = sum(map(int, str(X))) + sum(map(int, str(X - N)))
    return cnt count cnt = sum(map(int, str(X))) + sum(map(int, str(X - N))) cnt sum(map(int, str(X))) + sum(map(int, str(X - N))) sum(map(int, str(X))) sum map(int, str(X)) map int str(X) str X sum(map(int, str(X - N))) sum map(int, str(X - N)) map int str(X - N) str X - N X N return cnt cnt N N X X def solve_naive_first(N):
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive(N), 1+solve_naive(p*10-N)) solve_naive_first keta = math.floor(math.log(N, 10)) keta math.floor(math.log(N, 10)) math.floor math floor math.log(N, 10) math.log math log N 10 p = pow(10, keta) p pow(10, keta) pow 10 keta return min(solve_naive(N), 1+solve_naive(p*10-N)) min(solve_naive(N), 1+solve_naive(p*10-N)) min solve_naive(N) solve_naive N 1+solve_naive(p*10-N) 1 solve_naive(p*10-N) solve_naive p*10-N p*10 p 10 N N N def solve_naive(N):
    if N < 10:
        return min(N, 10-N+1)
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p) solve_naive if N < 10:
        return min(N, 10-N+1)
     N < 10 N 10 return min(N, 10-N+1) min(N, 10-N+1) min N 10-N+1 10-N 10 N 1 keta = math.floor(math.log(N, 10)) keta math.floor(math.log(N, 10)) math.floor math floor math.log(N, 10) math.log math log N 10 p = pow(10, keta) p pow(10, keta) pow 10 keta return min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p) min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p) min solve_naive((N // p + 1) * p - N) + (N // p + 1) solve_naive((N // p + 1) * p - N) solve_naive (N // p + 1) * p - N (N // p + 1) * p N // p + 1 N // p N p 1 p N N // p + 1 N // p N p 1 solve_naive(N % p) + N // p solve_naive(N % p) solve_naive N % p N p N // p N p N N def solve(N):
    n_0, n_1 = 0, 1  # 0: その桁で処理, 1: 次の桁に1タス
    for i in N[::-1]:
        i = int(i)
        if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
    return min(n_0, n_1+1) solve n_0, n_1 = 0, 1 n_0 0 n_1 1 for i in N[::-1]:
        i = int(i)
        if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
     i N[::-1] N ::-1 -1 i = int(i) i int(i) int i if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
     i == 9 i 9 n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1) n_0 n_0 + i n_0 i n_1 min(n_0 + 10 - i, n_1 + 10 - i - 1) min n_0 + 10 - i n_0 + 10 n_0 10 i n_1 + 10 - i - 1 n_1 + 10 - i n_1 + 10 n_1 10 i 1 n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1) n_0 min(n_0 + i, n_1 + i + 1) min n_0 + i n_0 i n_1 + i + 1 n_1 + i n_1 i 1 n_1 min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1) min n_0 +
                                                      10 - i n_0 +
                                                      10 n_0 10 i n_1 + 10 - i - 1 n_1 + 10 - i n_1 + 10 n_1 10 i 1 return min(n_0, n_1+1) min(n_0, n_1+1) min n_0 n_1+1 n_1 1 N N if __name__ == '__main__':
    N = input()
    ans = solve(N)
    print(ans) __name__ == '__main__' __name__ '__main__' N = input() N input() input ans = solve(N) ans solve(N) solve N print(ans) print ans def solve_naive_first(N):
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive(N), 1+solve_naive(p*10-N)) def solve_naive_first(N):
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive(N), 1+solve_naive(p*10-N)) solve_naive_first def count(N, X):
    cnt = sum(map(int, str(X))) + sum(map(int, str(X - N)))
    return cnt def count(N, X):
    cnt = sum(map(int, str(X))) + sum(map(int, str(X - N)))
    return cnt count def solve(N):
    n_0, n_1 = 0, 1  # 0: その桁で処理, 1: 次の桁に1タス
    for i in N[::-1]:
        i = int(i)
        if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
    return min(n_0, n_1+1) def solve(N):
    n_0, n_1 = 0, 1  # 0: その桁で処理, 1: 次の桁に1タス
    for i in N[::-1]:
        i = int(i)
        if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
    return min(n_0, n_1+1) solve N = input() input() N ans = solve(N) solve(N) ans def solve_naive(N):
    if N < 10:
        return min(N, 10-N+1)
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p) def solve_naive(N):
    if N < 10:
        return min(N, 10-N+1)
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p) solve_naive
import math


def count(N, X):
    cnt = sum(map(int, str(X))) + sum(map(int, str(X - N)))
    return cnt


def solve_naive_first(N):
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive(N), 1+solve_naive(p*10-N))


def solve_naive(N):
    if N < 10:
        return min(N, 10-N+1)
    keta = math.floor(math.log(N, 10))
    p = pow(10, keta)
    return min(solve_naive((N // p + 1) * p - N) + (N // p + 1), solve_naive(N % p) + N // p)


def solve(N):
    n_0, n_1 = 0, 1  # 0: その桁で処理, 1: 次の桁に1タス
    for i in N[::-1]:
        i = int(i)
        if i == 9:
            n_0, n_1 = n_0 + i, min(n_0 + 10 - i, n_1 + 10 - i - 1)
        else:
            n_0, n_1 = min(n_0 + i, n_1 + i + 1), min(n_0 +
                                                      10 - i, n_1 + 10 - i - 1)
    return min(n_0, n_1+1)


if __name__ == '__main__':
    N = input()
    ans = solve(N)
    print(ans)
