n=list(map(int,list(input())))

#右からi桁目、繰り上げあり1、繰り上げ無し0
dp=[[0]*2 for _ in range(len(n)+1)]

nrev=n[::-1]
nrev.append(0)
dp[0][0]=nrev[0]
dp[0][1]=10-nrev[0]
#print(0,dp)
for i in range(1,len(n)+1):
    dp[i][0]=min(dp[i-1][0]+nrev[i],dp[i-1][1]+(nrev[i]+1))
    dp[i][1]=min(dp[i-1][0]+(10-nrev[i]),dp[i-1][1]+(10-(nrev[i]+1)))
    #print(i,nrev[i],dp)

print(min(dp[-1]))


        
 n=list(map(int,list(input()))) n list(map(int,list(input()))) list map(int,list(input())) map int list(input()) list input() input [0]*2 for _ in range(len(n)+1) for _ in range(len(n)+1) _ range(len(n)+1) range len(n)+1 len(n) len n 1 for _ in range(len(n)+1) [0]*2 [0] 0 2 dp=[[0]*2 for _ in range(len(n)+1)] dp [[0]*2 for _ in range(len(n)+1)] nrev=n[::-1] nrev n[::-1] n ::-1 -1 nrev.append(0) nrev.append nrev append 0 dp[0][0]=nrev[0] dp[0][0] [0] dp 0 0 nrev[0] nrev 0 dp[0][1]=10-nrev[0] dp[0][1] [0] dp 0 1 10-nrev[0] 10 nrev[0] nrev 0 for i in range(1,len(n)+1):
    dp[i][0]=min(dp[i-1][0]+nrev[i],dp[i-1][1]+(nrev[i]+1))
    dp[i][1]=min(dp[i-1][0]+(10-nrev[i]),dp[i-1][1]+(10-(nrev[i]+1)))
    #print(i,nrev[i],dp) i range(1,len(n)+1) range 1 len(n)+1 len(n) len n 1 dp[i][0]=min(dp[i-1][0]+nrev[i],dp[i-1][1]+(nrev[i]+1)) dp[i][0] [i] dp i 0 min(dp[i-1][0]+nrev[i],dp[i-1][1]+(nrev[i]+1)) min dp[i-1][0]+nrev[i] dp[i-1][0] [i-1] dp i-1 i 1 0 nrev[i] nrev i dp[i-1][1]+(nrev[i]+1) dp[i-1][1] [i-1] dp i-1 i 1 1 nrev[i]+1 nrev[i] nrev i 1 dp[i][1]=min(dp[i-1][0]+(10-nrev[i]),dp[i-1][1]+(10-(nrev[i]+1))) dp[i][1] [i] dp i 1 min(dp[i-1][0]+(10-nrev[i]),dp[i-1][1]+(10-(nrev[i]+1))) min dp[i-1][0]+(10-nrev[i]) dp[i-1][0] [i-1] dp i-1 i 1 0 10-nrev[i] 10 nrev[i] nrev i dp[i-1][1]+(10-(nrev[i]+1)) dp[i-1][1] [i-1] dp i-1 i 1 1 10-(nrev[i]+1) 10 nrev[i]+1 nrev[i] nrev i 1 print(min(dp[-1])) print min(dp[-1]) min dp[-1] dp -1 nrev=n[::-1] n[::-1] nrev n=list(map(int,list(input()))) list(map(int,list(input()))) n dp=[[0]*2 for _ in range(len(n)+1)] [[0]*2 for _ in range(len(n)+1)] dp
n=list(map(int,list(input())))

#右からi桁目、繰り上げあり1、繰り上げ無し0
dp=[[0]*2 for _ in range(len(n)+1)]

nrev=n[::-1]
nrev.append(0)
dp[0][0]=nrev[0]
dp[0][1]=10-nrev[0]
#print(0,dp)
for i in range(1,len(n)+1):
    dp[i][0]=min(dp[i-1][0]+nrev[i],dp[i-1][1]+(nrev[i]+1))
    dp[i][1]=min(dp[i-1][0]+(10-nrev[i]),dp[i-1][1]+(10-(nrev[i]+1)))
    #print(i,nrev[i],dp)

print(min(dp[-1]))


        

