import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = input()
    l = len(n)

    dp = [[0] * 2 for _ in range(l + 1)]

    dp[0][1] = 1

    for i, d in enumerate(n, 1):
        di = int(d)
        dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di)
        dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1))

    ans = min(dp[l][0], dp[l][1] + 1)

    print(ans)


if __name__ == '__main__':
    main() import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = float('INF') INF float('INF') float 'INF' sys.setrecursionlimit(10 ** 5) sys.setrecursionlimit sys setrecursionlimit 10 ** 5 10 5 def main():
    n = input()
    l = len(n)

    dp = [[0] * 2 for _ in range(l + 1)]

    dp[0][1] = 1

    for i, d in enumerate(n, 1):
        di = int(d)
        dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di)
        dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1))

    ans = min(dp[l][0], dp[l][1] + 1)

    print(ans) main n = input() n input() input l = len(n) l len(n) len n [0] * 2 for _ in range(l + 1) for _ in range(l + 1) _ range(l + 1) range l + 1 l 1 for _ in range(l + 1) [0] * 2 [0] 0 2 dp = [[0] * 2 for _ in range(l + 1)] dp [[0] * 2 for _ in range(l + 1)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i, d in enumerate(n, 1):
        di = int(d)
        dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di)
        dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1))

     i d enumerate(n, 1) enumerate n 1 di = int(d) di int(d) int d dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di) dp[i][0] [i] dp i 0 min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di) min dp[i - 1][1] + (10 - di) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - di 10 di dp[i - 1][0] + di dp[i - 1][0] [i - 1] dp i - 1 i 1 0 di dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1)) dp[i][1] [i] dp i 1 min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1)) min dp[i - 1][1] + (10 - di - 1) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - di - 1 10 - di 10 di 1 dp[i - 1][0] + (di + 1) dp[i - 1][0] [i - 1] dp i - 1 i 1 0 di + 1 di 1 ans = min(dp[l][0], dp[l][1] + 1) ans min(dp[l][0], dp[l][1] + 1) min dp[l][0] [l] dp l 0 dp[l][1] + 1 dp[l][1] [l] dp l 1 1 print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD def main():
    n = input()
    l = len(n)

    dp = [[0] * 2 for _ in range(l + 1)]

    dp[0][1] = 1

    for i, d in enumerate(n, 1):
        di = int(d)
        dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di)
        dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1))

    ans = min(dp[l][0], dp[l][1] + 1)

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

    dp = [[0] * 2 for _ in range(l + 1)]

    dp[0][1] = 1

    for i, d in enumerate(n, 1):
        di = int(d)
        dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di)
        dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1))

    ans = min(dp[l][0], dp[l][1] + 1)

    print(ans) main readline = sys.stdin.readline sys.stdin.readline readline INF = float('INF') float('INF') INF
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = input()
    l = len(n)

    dp = [[0] * 2 for _ in range(l + 1)]

    dp[0][1] = 1

    for i, d in enumerate(n, 1):
        di = int(d)
        dp[i][0] = min(dp[i - 1][1] + (10 - di), dp[i - 1][0] + di)
        dp[i][1] = min(dp[i - 1][1] + (10 - di - 1), dp[i - 1][0] + (di + 1))

    ans = min(dp[l][0], dp[l][1] + 1)

    print(ans)


if __name__ == '__main__':
    main()
