s = map(int,list(raw_input()))
dp = [[0,0] for _ in range(len(s))]
for i in range(len(dp)):
	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i]
	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i]
	dp[i][0] = min(a,b)

	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1
	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1
	dp[i][1] = min(a,b)
print dp[-1][0]


 s = map(int,list(raw_input())) s map(int,list(raw_input())) map int list(raw_input()) list raw_input() raw_input [0,0] for _ in range(len(s)) for _ in range(len(s)) _ range(len(s)) range len(s) len s for _ in range(len(s)) [0,0] 0 0 dp = [[0,0] for _ in range(len(s))] dp [[0,0] for _ in range(len(s))] for i in range(len(dp)):
	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i]
	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i]
	dp[i][0] = min(a,b)

	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1
	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1
	dp[i][1] = min(a,b) i range(len(dp)) range len(dp) len dp a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] a (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] dp[i - 1][0] if i - 1 >= 0 else 0 i - 1 >= 0 i - 1 i 1 0 dp[i - 1][0] [i - 1] dp i - 1 i 1 0 0 s[i] s i b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] b (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 dp[i - 1][1] if i - 1 >= 0 else 1 i - 1 >= 0 i - 1 i 1 0 dp[i - 1][1] [i - 1] dp i - 1 i 1 1 1 10 s[i] s i dp[i][0] = min(a,b) dp[i][0] [i] dp i 0 min(a,b) min a b a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1 a (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1 (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] dp[i - 1][0] if i - 1 >= 0 else 0 i - 1 >= 0 i - 1 i 1 0 dp[i - 1][0] [i - 1] dp i - 1 i 1 0 0 s[i] s i 1 b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1 b (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1 (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 dp[i - 1][1] if i - 1 >= 0 else 1 i - 1 >= 0 i - 1 i 1 0 dp[i - 1][1] [i - 1] dp i - 1 i 1 1 1 10 s[i] s i 1 dp[i][1] = min(a,b) dp[i][1] [i] dp i 1 min(a,b) min a b print dp[-1][0] [-1] dp -1 0 a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1 (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1 a a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] a dp = [[0,0] for _ in range(len(s))] [[0,0] for _ in range(len(s))] dp b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] b b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1 (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1 b s = map(int,list(raw_input())) map(int,list(raw_input())) s
s = map(int,list(raw_input()))
dp = [[0,0] for _ in range(len(s))]
for i in range(len(dp)):
	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i]
	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i]
	dp[i][0] = min(a,b)

	a = (dp[i - 1][0] if i - 1 >= 0 else 0) + s[i] + 1
	b = (dp[i - 1][1] if i - 1 >= 0 else 1) + 10 - s[i] - 1
	dp[i][1] = min(a,b)
print dp[-1][0]



