nn = [0] + list(map(int, list(input())))
dp0, dp1 = 0, 0
for i in range(len(nn)):
	dp0, dp1 = nn[i] + min(dp1, dp0), 9 - nn[i] + min(dp1, dp0 + 2)
print(min(dp0, dp1)) nn = [0] + list(map(int, list(input()))) nn [0] + list(map(int, list(input()))) [0] 0 list(map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input dp0, dp1 = 0, 0 dp0 0 dp1 0 for i in range(len(nn)):
	dp0, dp1 = nn[i] + min(dp1, dp0), 9 - nn[i] + min(dp1, dp0 + 2) i range(len(nn)) range len(nn) len nn dp0, dp1 = nn[i] + min(dp1, dp0), 9 - nn[i] + min(dp1, dp0 + 2) dp0 nn[i] + min(dp1, dp0) nn[i] nn i min(dp1, dp0) min dp1 dp0 dp1 9 - nn[i] + min(dp1, dp0 + 2) 9 - nn[i] 9 nn[i] nn i min(dp1, dp0 + 2) min dp1 dp0 + 2 dp0 2 print(min(dp0, dp1)) print min(dp0, dp1) min dp0 dp1 dp0, dp1 = 0, 0 0 dp0 dp1 = 0, 0 0 dp1 dp1 = nn[i] + min(dp1, dp0), 9 - nn[i] + min(dp1, dp0 + 2) 9 - nn[i] + min(dp1, dp0 + 2) dp1 nn = [0] + list(map(int, list(input()))) [0] + list(map(int, list(input()))) nn dp0, dp1 = nn[i] + min(dp1, dp0), 9 - nn[i] + min(dp1, dp0 + 2) nn[i] + min(dp1, dp0) dp0
nn = [0] + list(map(int, list(input())))
dp0, dp1 = 0, 0
for i in range(len(nn)):
	dp0, dp1 = nn[i] + min(dp1, dp0), 9 - nn[i] + min(dp1, dp0 + 2)
print(min(dp0, dp1))