N = input().replace("\n", "")
L = len(N)
dp = [[0 for _ in range(2)] for i in range(L+1)]
dp[0][1] = 1
for i in range(L):
    n = int(N[i])
    dp[i+1][0] = min(dp[i][0]+n, dp[i][1]+(10-n))
    dp[i+1][1] = min(dp[i][0]+n+1, dp[i][1]+(9-n))
print(dp[L][0]) N = input().replace("\n", "") N input().replace("\n", "") ().replace () input replace "\n" "" L = len(N) L len(N) len N [0 for _ in range(2)] 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 for _ in range(2)] dp = [[0 for _ in range(2)] for i in range(L+1)] dp [[0 for _ in range(2)] for i in range(L+1)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(L):
    n = int(N[i])
    dp[i+1][0] = min(dp[i][0]+n, dp[i][1]+(10-n))
    dp[i+1][1] = min(dp[i][0]+n+1, dp[i][1]+(9-n)) i range(L) range L n = int(N[i]) n int(N[i]) int N[i] N i dp[i+1][0] = min(dp[i][0]+n, dp[i][1]+(10-n)) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0]+n, dp[i][1]+(10-n)) min dp[i][0]+n dp[i][0] [i] dp i 0 n dp[i][1]+(10-n) dp[i][1] [i] dp i 1 10-n 10 n dp[i+1][1] = min(dp[i][0]+n+1, dp[i][1]+(9-n)) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0]+n+1, dp[i][1]+(9-n)) min dp[i][0]+n+1 dp[i][0]+n dp[i][0] [i] dp i 0 n 1 dp[i][1]+(9-n) dp[i][1] [i] dp i 1 9-n 9 n print(dp[L][0]) print dp[L][0] [L] dp L 0 n = int(N[i]) int(N[i]) n N = input().replace("\n", "") input().replace("\n", "") N L = len(N) len(N) L dp = [[0 for _ in range(2)] for i in range(L+1)] [[0 for _ in range(2)] for i in range(L+1)] dp
N = input().replace("\n", "")
L = len(N)
dp = [[0 for _ in range(2)] for i in range(L+1)]
dp[0][1] = 1
for i in range(L):
    n = int(N[i])
    dp[i+1][0] = min(dp[i][0]+n, dp[i][1]+(10-n))
    dp[i+1][1] = min(dp[i][0]+n+1, dp[i][1]+(9-n))
print(dp[L][0])
