def main():

  n=list(eval(input()))

  n=[int(i) for i in n]

  l=len(n)

  

  dp=[[0]*2 for _ in range(l+1)]

  if n[0]>=5:

    dp[0][1]=1

    

  for i in range(l):

      dp[i+1][0]=min(dp[i][0]+n[i],dp[i][1]+10-n[i])

      dp[i+1][1]=min(dp[i][0]+n[i]+1,dp[i][1]+9-n[i])

      

  #print(dp)

  print((dp[l][0]))



if __name__=='__main__':

    main()

def main():

  n=list(eval(input()))

  n=[int(i) for i in reversed(n)]

  l=len(n)

  n.append(0)

  

  ans=0

  for i in range(l):

    if n[i]<=4:

      a=n[i]

    elif n[i]==5:

      if n[i+1]<=4:

        a=n[i]

      else:

        a=10-n[i]

        n[i+1]+=1

    else:

      n[i+1]+=1

      a=10-n[i]

    ans+=a

  ans+=n[l]

  print(ans)  

if __name__=='__main__':

    main()

def main():
    n = list(eval(input()))
    n = [int(i) for i in n]
    l = len(n)
    dp = [[0] * 2 for _ in range(l + 1)]
    if n[0] >= 5:
        dp[0][1] = 1
    for i in range(l):
        dp[i + 1][0] = min(dp[i][0] + n[i], dp[i][1] + 10 - n[i])
        dp[i + 1][1] = min(dp[i][0] + n[i] + 1, dp[i][1] + 9 - n[i])
    # print(dp)
    print((dp[l][0]))


if __name__ == "__main__":
    main()

def main():
    n = list(eval(input()))
    n = [int(i) for i in reversed(n)]
    l = len(n)
    n.append(0)
    ans = 0
    for i in range(l):
        if n[i] <= 4:
            a = n[i]
        elif n[i] == 5:
            if n[i + 1] <= 4:
                a = n[i]
            else:
                a = 10 - n[i]
                n[i + 1] += 1
        else:
            n[i + 1] += 1
            a = 10 - n[i]
        ans += a
    ans += n[l]
    print(ans)


if __name__ == "__main__":
    main()

-    n = [int(i) for i in n] +    n = [int(i) for i in reversed(n)] -    dp = [[0] * 2 for _ in range(l + 1)] -    if n[0] >= 5: -        dp[0][1] = 1 +    n.append(0) +    ans = 0 -        dp[i + 1][0] = min(dp[i][0] + n[i], dp[i][1] + 10 - n[i]) -        dp[i + 1][1] = min(dp[i][0] + n[i] + 1, dp[i][1] + 9 - n[i]) -    # print(dp) -    print((dp[l][0])) +        if n[i] <= 4: +            a = n[i] +        elif n[i] == 5: +            if n[i + 1] <= 4: +                a = n[i] +            else: +                a = 10 - n[i] +                n[i + 1] += 1 +        else: +            n[i + 1] += 1 +            a = 10 - n[i] +        ans += a +    ans += n[l] +    print(ans)