Problem:
Score: 500 points

Problem Statement

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints

N is an integer between 1 and 10^{1,000,000} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible number of total banknotes used by you and the clerk.

Sample Input 1

36

Sample Output 1

8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Sample Input 2

91

Sample Output 2

3

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    partial class Program
    {
        static void Main(string[] args)
        {
            ABC155_E();
        }

        static void ABC155_A()
        {
            var abc = ReadIntN();
            var A = abc[0];
            var B = abc[1];
            var C = abc[2];
            if (A == B && B == C) { Console.WriteLine("No");return; }
            if (A == B || A == C || B == C) { Console.WriteLine("Yes");return; }
            Console.WriteLine("No");
        }
        static void ABC155_B()
        {
            var N = ReadInt();
            var A = ReadIntN();
            for(int i = 0; i < N; ++i)
            { var a = A[i];
                if (a % 2 == 0 && (a % 3 > 0 && a % 5 > 0)) { Console.WriteLine("DENIED");return; }
            }
            Console.WriteLine("APPROVED");
        }
        static void ABC155_C()
        {
            var N = ReadInt();
            Dictionary<string, int> dict = new Dictionary<string, int>();
            for (int i = 0; i < N; ++i)
            {
                var s = Console.ReadLine();
                if (!dict.ContainsKey(s)) { dict.Add(s, 0); }
                dict[s] += 1;
            }
            var max = dict.Values.Max();
            var keys = dict.Where(x => x.Value == max).Select(x => x.Key).ToList();
            keys.Sort(StringComparer.OrdinalIgnoreCase);
            foreach (var s in keys)
            {
                Console.WriteLine(s);
            }
        }
        static void ABC155_D()
        {
            var nk = ReadLongN();
            var A = ReadLongN();
            var N = nk[0];
            var K = nk[1];
            A = A.OrderBy(x => x).ToArray();
            var A2 = A.Select(x => x * x).ToArray();

            var nbNeg = A.Count(x => x < 0);
            var nbZero = A.Count(x => x == 0);

            // xより小さいペアの個数を返す
            Func<long, long> lessThan = x => {
                long count = 0;

                //Negativeに対して尺取り法
                if (x >= 0) //反転させたやつを使えばループの場合わけいらないよね
                {
                    long right = N;
                    for (int left = 0; left < nbNeg; ++left)
                    {
                        while (right > 0 && A[left] * A[right - 1] < x)
                        {
                            right--;
                        }
                        count += N - right;
                    }
                }
                else
                {
                    long right = 0;
                    for (int left = 0; left < nbNeg; ++left)
                    {
                        while (right < N && A[left] * A[right] >= x)
                        {
                            right++;
                        }
                        count += N - right;
                    }
                }

                //Zero
                count += x > 0 ? nbZero * N : 0;

                //Positiveに対して尺取り法
                if (x >= 0)
                {
                    long right = N;
                    for (int left = nbNeg + nbZero; left < N; ++left)
                    {
                        while (right > 0 && A[left] * A[right - 1] >= x)
                        {
                            right--;
                        }
                        count += right;
                    }
                }
                else
                {
                    long right = 0;
                    for (int left = nbNeg + nbZero; left < N; ++left)
                    {
                        while (right < N && A[left] * A[right] < x)
                        {
                            right++;
                        }
                        count += right;
                    }
                }

                //(i,j)に制約をつけないN^2に対する個数が求まったので
                //対角成分の個数を引いて半分にし、i<jの個数にする
                return (count - A2.Count(v => v < x)) / 2;
            };

            //二分探索
            long max = 1000000000000000001;
            long min = -max;
            while (min + 1 < max)
            {
                long mid = (min + max) / 2;
                var count = lessThan(mid);
                if (count < K)
                {
                    min = mid;
                }
                else
                {
                    max = mid;
                }
            }
            Console.WriteLine(min);

        }
        static void ABC155_E()
        {
            var str = Console.ReadLine();
            int useMajor = 99, useMinor = 0;
            foreach(var c in str)
            {
                var d = ((int)c) - 48;
                var nextMajor = Math.Min(useMajor + 9, useMinor + 11) - d;
                var nextMinor = Math.Min(useMajor, useMinor) + d;
                useMajor = nextMajor;
                useMinor = nextMinor;
            }
            Console.WriteLine(Math.Min(useMajor, useMinor));
        }
        static void ABC155_F()
        {

        }

        static string ReadStr()
        {
            return Console.ReadLine();
        }
        static string[] ReadStrN()
        {
            return Console.ReadLine().Split(' ');
        }
        static int ReadInt()
        {
            return Convert.ToInt32(Console.ReadLine());
        }
        static int[] ReadIntN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
        }
        static long ReadLong()
        {
            return Convert.ToInt64(Console.ReadLine());
        }
        static long[] ReadLongN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
        }
        static Tuple<long, long>[]  ReadTupleLong(int n)
        {
            List<Tuple<long, long>> list = new List<Tuple<long, long>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadLongN();
                list.Add(new Tuple<long, long>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
    }
}