def read():
    N = input().strip()
    return N,


def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
    return min(dp0[-1], dp1[-1])


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs))) def read():
    N = input().strip()
    return N, read N = input().strip() N input().strip() ().strip () input strip return N, N def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
    return min(dp0[-1], dp1[-1]) solve L = len(N) L len(N) len N N = list(reversed("0" + N)) N list(reversed("0" + N)) list reversed("0" + N) reversed "0" + N "0" N 0 for i in range(L + 1) for i in range(L + 1) i range(L + 1) range L + 1 L 1 for i in range(L + 1) 0 dp0 = [0 for i in range(L + 1)] dp0 [0 for i in range(L + 1)] 0 for i in range(L + 1) for i in range(L + 1) i range(L + 1) range L + 1 L 1 for i in range(L + 1) 0 dp1 = [0 for i in range(L + 1)] dp1 [0 for i in range(L + 1)] dp1[0] = INF dp1[0] dp1 0 INF for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
     i range(L) range L x = int(N[i]) x int(N[i]) int N[i] N i dp0[i + 1] = min(dp0[i] + x, dp1[i] + x) dp0[i + 1] dp0 i + 1 i 1 min(dp0[i] + x, dp1[i] + x) min dp0[i] + x dp0[i] dp0 i x dp1[i] + x dp1[i] dp1 i x dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2) dp1[i + 1] dp1 i + 1 i 1 min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2) min dp0[i] + 1 + (10 - x) dp0[i] + 1 dp0[i] dp0 i 1 10 - x 10 x dp1[i] + 1 + (10 - x) - 2 dp1[i] + 1 + (10 - x) dp1[i] + 1 dp1[i] dp1 i 1 10 - x 10 x 2 return min(dp0[-1], dp1[-1]) min(dp0[-1], dp1[-1]) min dp0[-1] dp0 -1 dp1[-1] dp1 -1 N N INF=1000000 INF 1000000 if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs))) __name__ == '__main__' __name__ '__main__' inputs = read() inputs read() read print("{}".format(solve(*inputs))) print "{}".format(solve(*inputs)) "{}".format "{}" format solve(*inputs) solve *inputs inputs inputs = read() read() inputs def read():
    N = input().strip()
    return N, def read():
    N = input().strip()
    return N, read def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
    return min(dp0[-1], dp1[-1]) def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
    return min(dp0[-1], dp1[-1]) solve
def read():
    N = input().strip()
    return N,


def solve(N, INF=1000000):
    L = len(N)
    N = list(reversed("0" + N))
    dp0 = [0 for i in range(L + 1)]
    dp1 = [0 for i in range(L + 1)]
    dp1[0] = INF
    for i in range(L):
        x = int(N[i])
        dp0[i + 1] = min(dp0[i] + x, dp1[i] + x)
        dp1[i + 1] = min(dp0[i] + 1 + (10 - x), dp1[i] + 1 + (10 - x) - 2)
    return min(dp0[-1], dp1[-1])


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))
