n = input()
dp = [0,1]
ans = 0
for i in range(len(n)):
    cur1 = dp[0]
    cur2 = dp[1]
    if i != len(n)-1:
        dp[0] = min(cur1+int(n[i]), cur2+10-int(n[i]))
        dp[1] = min(cur1+int(n[i])+1, cur2+9-int(n[i]))
    else:
        ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i]))
print(ans) n = input() n input() input dp = [0,1] dp [0,1] 0 1 ans = 0 ans 0 for i in range(len(n)):
    cur1 = dp[0]
    cur2 = dp[1]
    if i != len(n)-1:
        dp[0] = min(cur1+int(n[i]), cur2+10-int(n[i]))
        dp[1] = min(cur1+int(n[i])+1, cur2+9-int(n[i]))
    else:
        ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i])) i range(len(n)) range len(n) len n cur1 = dp[0] cur1 dp[0] dp 0 cur2 = dp[1] cur2 dp[1] dp 1 if i != len(n)-1:
        dp[0] = min(cur1+int(n[i]), cur2+10-int(n[i]))
        dp[1] = min(cur1+int(n[i])+1, cur2+9-int(n[i]))
    else:
        ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i])) i != len(n)-1 i len(n)-1 len(n) len n 1 dp[0] = min(cur1+int(n[i]), cur2+10-int(n[i])) dp[0] dp 0 min(cur1+int(n[i]), cur2+10-int(n[i])) min cur1+int(n[i]) cur1 int(n[i]) int n[i] n i cur2+10-int(n[i]) cur2+10 cur2 10 int(n[i]) int n[i] n i dp[1] = min(cur1+int(n[i])+1, cur2+9-int(n[i])) dp[1] dp 1 min(cur1+int(n[i])+1, cur2+9-int(n[i])) min cur1+int(n[i])+1 cur1+int(n[i]) cur1 int(n[i]) int n[i] n i 1 cur2+9-int(n[i]) cur2+9 cur2 9 int(n[i]) int n[i] n i ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i])) ans min(dp[0]+int(n[i]), dp[1]+10-int(n[i])) min dp[0]+int(n[i]) dp[0] dp 0 int(n[i]) int n[i] n i dp[1]+10-int(n[i]) dp[1]+10 dp[1] dp 1 10 int(n[i]) int n[i] n i print(ans) print ans cur2 = dp[1] dp[1] cur2 dp = [0,1] [0,1] dp ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i])) min(dp[0]+int(n[i]), dp[1]+10-int(n[i])) ans cur1 = dp[0] dp[0] cur1 ans = 0 0 ans n = input() input() n
n = input()
dp = [0,1]
ans = 0
for i in range(len(n)):
    cur1 = dp[0]
    cur2 = dp[1]
    if i != len(n)-1:
        dp[0] = min(cur1+int(n[i]), cur2+10-int(n[i]))
        dp[1] = min(cur1+int(n[i])+1, cur2+9-int(n[i]))
    else:
        ans = min(dp[0]+int(n[i]), dp[1]+10-int(n[i]))
print(ans)
