INF=10**30
N=list(map(int,list(input())))[::-1]
l=len(N)
dp=[[INF,INF] for i in range(l+1)]
dp[0][0]=0
for i in range(l):
    dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+N[i]+1)
    dp[i+1][1]=min(dp[i][0]+(10-N[i]),dp[i][1]+(9-N[i]))
print(min(dp[l][0],dp[l][1]+1)) INF=10**30 INF 10**30 10 30 N=list(map(int,list(input())))[::-1] N list(map(int,list(input())))[::-1] (map(int,list(input()))) list map(int,list(input())) map int list(input()) list input() input ::-1 -1 l=len(N) l len(N) len N [INF,INF] 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) [INF,INF] INF INF dp=[[INF,INF] for i in range(l+1)] dp [[INF,INF] for i in range(l+1)] dp[0][0]=0 dp[0][0] [0] dp 0 0 0 for i in range(l):
    dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+N[i]+1)
    dp[i+1][1]=min(dp[i][0]+(10-N[i]),dp[i][1]+(9-N[i])) i range(l) range l dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+N[i]+1) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0]+N[i],dp[i][1]+N[i]+1) min dp[i][0]+N[i] dp[i][0] [i] dp i 0 N[i] N i dp[i][1]+N[i]+1 dp[i][1]+N[i] dp[i][1] [i] dp i 1 N[i] N i 1 dp[i+1][1]=min(dp[i][0]+(10-N[i]),dp[i][1]+(9-N[i])) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0]+(10-N[i]),dp[i][1]+(9-N[i])) min dp[i][0]+(10-N[i]) dp[i][0] [i] dp i 0 10-N[i] 10 N[i] N i dp[i][1]+(9-N[i]) dp[i][1] [i] dp i 1 9-N[i] 9 N[i] N i print(min(dp[l][0],dp[l][1]+1)) print 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 l=len(N) len(N) l N=list(map(int,list(input())))[::-1] list(map(int,list(input())))[::-1] N INF=10**30 10**30 INF dp=[[INF,INF] for i in range(l+1)] [[INF,INF] for i in range(l+1)] dp
INF=10**30
N=list(map(int,list(input())))[::-1]
l=len(N)
dp=[[INF,INF] for i in range(l+1)]
dp[0][0]=0
for i in range(l):
    dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+N[i]+1)
    dp[i+1][1]=min(dp[i][0]+(10-N[i]),dp[i][1]+(9-N[i]))
print(min(dp[l][0],dp[l][1]+1))