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 CompLib.Util;
using System;
using System.Linq;
public class Program
{
    int[] N;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.Next().Select(c => c - '0').ToArray();
        Array.Reverse(N);

        // i桁目 超えてるか? おつり + 払う
        var dp = new long[N.Length + 1, 2];
        for (int i = 0; i < N.Length; i++)
        {
            for (int j = 0; j <= 9; j++)
            {
                dp[i, 0] = long.MaxValue / 2;
                dp[i, 1] = long.MaxValue / 2;
            }
        }


        for (int i = 0; i < N.Length; i++)
        {
            for (int j = 0; j <= 9; j++)
            {
                if (i == 0)
                {
                    if (j < N[i])
                    {
                        dp[i, 0] = Math.Min(dp[i, 0], 10 + j - N[i] + j);
                    }
                    else
                    {
                        dp[i, 1] = Math.Min(dp[i, 1], j - N[i] + j);
                    }
                }
                else
                {
                    if (j < N[i])
                    {
                        dp[i, 0] = Math.Min(dp[i, 0], dp[i - 1, 1] + 10 + j - N[i] + j);
                        dp[i, 0] = Math.Min(dp[i, 0], dp[i - 1, 0] + 9 + j - N[i] + j);
                    }

                    if (j == N[i])
                    {
                        dp[i, 0] = Math.Min(dp[i, 0], dp[i - 1, 0] + 9 + j - N[i] + j);
                        dp[i, 1] = Math.Min(dp[i, 1], dp[i - 1, 1] + j);
                    }

                    if (j > N[i])
                    {
                        dp[i, 1] = Math.Min(dp[i, 1], dp[i - 1, 0] + j + j - (N[i] + 1));
                        dp[i, 1] = Math.Min(dp[i, 1], dp[i - 1, 1] + j + j - N[i]);
                    }
                }
            }
        }

        dp[N.Length, 1] = Math.Min(dp[N.Length - 1, 0] + 1, Math.Min(dp[N.Length - 1, 1] + 2, dp[N.Length - 1, 1]));
        Console.WriteLine(dp[N.Length, 1]);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;
    class Scanner
    {
        private int _index;
        private string[] _line;
        const char separator = ' ';

        public Scanner()
        {
            _index = 0;
            _line = new string[0];
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(separator);
                _index = 0;
            }
            return _line[_index++];
        }
        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(separator);
            _index = _line.Length;
            return _line;
        }
        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
    }
}