abef=0
bbef=1
s=input()
for i in range(len(s)):
  curr=int(s[len(s)-1-i])
  a=abef+curr
  b=bbef+9-curr
  a=min(a,1+bbef+curr)
  b=min(b,abef+10-curr)
  abef=a
  bbef=b
curr=0
a=abef+curr
b=bbef+9-curr
a=min(a,1+bbef+curr)
b=min(b,abef+10-curr)
print(min(a,b)) abef=0 abef 0 bbef=1 bbef 1 s=input() s input() input for i in range(len(s)):
  curr=int(s[len(s)-1-i])
  a=abef+curr
  b=bbef+9-curr
  a=min(a,1+bbef+curr)
  b=min(b,abef+10-curr)
  abef=a
  bbef=b i range(len(s)) range len(s) len s curr=int(s[len(s)-1-i]) curr int(s[len(s)-1-i]) int s[len(s)-1-i] s len(s)-1-i len(s)-1 len(s) len s 1 i a=abef+curr a abef+curr abef curr b=bbef+9-curr b bbef+9-curr bbef+9 bbef 9 curr a=min(a,1+bbef+curr) a min(a,1+bbef+curr) min a 1+bbef+curr 1+bbef 1 bbef curr b=min(b,abef+10-curr) b min(b,abef+10-curr) min b abef+10-curr abef+10 abef 10 curr abef=a abef a bbef=b bbef b curr=0 curr 0 a=abef+curr a abef+curr abef curr b=bbef+9-curr b bbef+9-curr bbef+9 bbef 9 curr a=min(a,1+bbef+curr) a min(a,1+bbef+curr) min a 1+bbef+curr 1+bbef 1 bbef curr b=min(b,abef+10-curr) b min(b,abef+10-curr) min b abef+10-curr abef+10 abef 10 curr print(min(a,b)) print min(a,b) min a b curr=int(s[len(s)-1-i]) int(s[len(s)-1-i]) curr b=bbef+9-curr bbef+9-curr b a=min(a,1+bbef+curr) min(a,1+bbef+curr) a curr=0 0 curr abef=0 0 abef a=min(a,1+bbef+curr) min(a,1+bbef+curr) a abef=a a abef b=bbef+9-curr bbef+9-curr b a=abef+curr abef+curr a bbef=b b bbef a=abef+curr abef+curr a b=min(b,abef+10-curr) min(b,abef+10-curr) b b=min(b,abef+10-curr) min(b,abef+10-curr) b bbef=1 1 bbef s=input() input() s
abef=0
bbef=1
s=input()
for i in range(len(s)):
  curr=int(s[len(s)-1-i])
  a=abef+curr
  b=bbef+9-curr
  a=min(a,1+bbef+curr)
  b=min(b,abef+10-curr)
  abef=a
  bbef=b
curr=0
a=abef+curr
b=bbef+9-curr
a=min(a,1+bbef+curr)
b=min(b,abef+10-curr)
print(min(a,b))