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.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Extensions;
using static System.Math;
using static Extensions.MathExtension;
using static Extensions.ConsoleInputExtension;
using static Extensions.ConsoleOutputExtension;

class Solver
{
    public void Solve()
    {
        // int n = Cin;
        // var dictionary = new Dictionary<string, int>();
        // int max = int.MinValue;
        // for (int i = 0; i < n; i++)
        // {
        //     string s = Cin;
        //     int current = dictionary.ContainsKey(s) ? dictionary[s] : 0;
        //     dictionary[s] = current + 1;
        // }
        //
        // int? buf = null;
        // foreach (var pair in dictionary.OrderByDescending(val => val.Value).ThenBy(val => val.Key))
        // {
        //     if (buf == null || buf == pair.Value)
        //     {
        //         Coutln(pair.Key);
        //         buf = pair.Value;
        //     }
        //     else break;
        // }
        string n = Cin;
        var dp = new long[n.Length + 1, 2];
        dp[0, 0] = 1;
        for (int i = 0; i < n.Length; i++)
        {
            int x = n[i] - '0';
            dp[i + 1, 0] = Min(dp[i, 0] + (9 - x), x != 9 ? dp[i, 1] + (x + 1) : long.MaxValue);
            dp[i + 1, 1] = Min(dp[i, 0] + (10 - x), dp[i, 1] + x);
        }

        Coutln(dp[n.Length, 1]);
    }

    //Other Functions Here
}

//Other Classes Here

#if !DEBUG
class EntryPoint
{
    static void Main(string[] args)
    {
        new Solver().Solve();
        Flush();
    }
}
#endif

class PriorityQueue<T>
{
    private readonly List<Tuple<int, T>> _list = new List<Tuple<int, T>>();

    public int Count => _list.Count;

    public PriorityQueue()
    {
    }

    public void Push(T item, int priority)
    {
        _list.Add(Tuple.Create(priority, item));

        int itemIndex = Count - 1, parentIndex;
        while ((parentIndex = GetParentIndex(itemIndex)) != -1 &&
               priority > _list[parentIndex].Item1)
        {
            Swap(itemIndex, parentIndex);
            itemIndex = parentIndex;
        }
    }

    private int GetParentIndex(int index)
    {
        if (index == 0) return -1;
        return ((index + 1) >> 1) - 1;
    }

    private Tuple<int, int> GetChildrenIndex(int index)
    {
        var item2 = (index + 1) << 1;
        var item1 = item2 - 1;
        return Tuple.Create(item1, item2);
    }

    public T Pop()
    {
        if (Count <= 0) throw new IndexOutOfRangeException();
        var item = _list[0].Item2;
        _list[0] = _list[Count - 1];
        _list.RemoveAt(Count - 1);

        int index = 0;
        Tuple<int, int> childrenIndex = GetChildrenIndex(index);
        while (childrenIndex.Item1 < Count || childrenIndex.Item2 < Count)
        {
            if (childrenIndex.Item2 >= Count || _list[childrenIndex.Item1].Item1 > _list[childrenIndex.Item2].Item1)
            {
                if (_list[childrenIndex.Item1].Item1 <= _list[index].Item1) return item;

                Swap(index, childrenIndex.Item1);
                index = childrenIndex.Item1;
            }
            else
            {
                if (_list[childrenIndex.Item2].Item1 <= _list[index].Item1) return item;
                Swap(index, childrenIndex.Item2);
                index = childrenIndex.Item2;
            }

            childrenIndex = GetChildrenIndex(index);
        }

        return item;
    }

    public T Peek()
    {
        return _list[0].Item2;
    }

    private void Swap(int index1, int index2)
    {
        var tmp = _list[index1];
        _list[index1] = _list[index2];
        _list[index2] = tmp;
    }
}

struct Rational
{
    private long _numerator;
    private long _denominator;
    public long Numerator => _numerator;
    public long Denominator => _denominator;

    public Rational(long numerator, long denominator)
    {
        var gcd = GCD(numerator, denominator);
        _numerator = numerator / gcd;
        _denominator = denominator / gcd;
    }

    public static Rational operator +(Rational a, Rational b)
    {
        return new Rational(a._numerator * b._denominator + b._numerator * a._denominator,
            a._denominator * b._denominator);
    }

    public static Rational operator -(Rational a, Rational b)
    {
        return new Rational(a._numerator * b._denominator - b._numerator * a._denominator,
            a._denominator * b._denominator);
    }

    public static Rational operator *(Rational a, Rational b)
    {
        return new Rational(a._numerator * b._numerator, a._denominator * b._denominator);
    }

    public static Rational operator /(Rational a, Rational b)
    {
        return new Rational(a._numerator * b._denominator, a._denominator * b._numerator);
    }

    public static explicit operator Rational(int i)
    {
        return new Rational(i, 1);
    }

    public static explicit operator Rational(long l)
    {
        return new Rational(l, 1);
    }

    public static explicit operator Rational(double d)
    {
        long denominator = 1;
        while (d % 1 != 0)
        {
            denominator *= 2;
            d *= 2;
        }

        return new Rational((long) d, denominator);
    }

    public static explicit operator Rational(float f)
    {
        long denominator = 1;
        while (f % 1 != 0)
        {
            denominator *= 2;
            f *= 2;
        }

        return new Rational((long) f, denominator);
    }
}

struct ModInt
{
    public long Value { get; }
    public static long Mod { get; set; } = 1000000000L + 7;

    public ModInt(long l)
    {
        long value = l % Mod;
        while (value < 0) value += Mod;
        Value = value % Mod;
    }

    public static implicit operator long(ModInt m)
    {
        return m.Value;
    }

    public static implicit operator ModInt(long l)
    {
        return new ModInt(l);
    }

    public static ModInt operator +(ModInt a, ModInt b)
    {
        return new ModInt(a.Value + b.Value);
    }

    public static ModInt operator -(ModInt a, ModInt b)
    {
        return new ModInt(a.Value - b.Value);
    }

    public static ModInt operator *(ModInt a, ModInt b)
    {
        return new ModInt(a.Value * b.Value);
    }

    public static ModInt operator /(ModInt a, ModInt b)
    {
        return new ModInt(a * b.Inverse());
    }

    public override string ToString()
    {
        return Value.ToString();
    }
}

static class ModIntMath
{
    public static ModInt Pow(this ModInt a, ModInt b)
    {
        var pow = b.Value;
        var m = a;
        ModInt ans = new ModInt(1);
        while (pow != 0)
        {
            if ((pow & 1) == 1)
            {
                ans *= m;
            }

            pow >>= 1;
            m *= m;
        }

        return ans;
    }

    public static ModInt Inverse(this ModInt m)
    {
        return m.Pow((ModInt) (ModInt.Mod - 2));
    }
}

class UnionFind
{
    private readonly int[] _array;

    public UnionFind(int N)
    {
        _array = new int[N];
        for (int i = 0; i < N; i++)
        {
            _array[i] = i;
        }
    }

    public int GetRootNode(int n)
    {
        if (_array[n] == n) return n;
        return _array[n] = GetRootNode(_array[n]);
    }

    public void UnionGroup(int a, int b)
    {
        var rootA = GetRootNode(a);
        var rootB = GetRootNode(b);
        if (rootA == rootB) return;
        _array[rootA] = rootB;
    }

    public bool IsSameGroup(int a, int b) => GetRootNode(a) == GetRootNode(b);

    public bool IsRoot(int n) => _array[n] == n;
}


namespace Extensions
{
    public class ConsoleInputExtension
    {
        private static readonly ConsoleInputExtension _cin = new ConsoleInputExtension();

        public static ConsoleInputExtension Cin => _cin;

        private static readonly Queue<string> _inputQueue = new Queue<string>();

        private ConsoleInputExtension()
        {
        }

        public static implicit operator string(ConsoleInputExtension _)
        {
            if (_inputQueue.Count == 0)
                Console.ReadLine().Split(' ')
                    .ForEach(val => _inputQueue.Enqueue(val));
            return _inputQueue.Dequeue();
        }

        public static implicit operator int(ConsoleInputExtension _)
        {
            if (_inputQueue.Count == 0)
                Console.ReadLine().Split(' ')
                    .ForEach(val => _inputQueue.Enqueue(val));
            return int.Parse(_inputQueue.Dequeue());
        }

        public static implicit operator long(ConsoleInputExtension _)
        {
            if (_inputQueue.Count == 0)
                Console.ReadLine().Split(' ')
                    .ForEach(val => _inputQueue.Enqueue(val));
            return long.Parse(_inputQueue.Dequeue());
        }

        public static implicit operator float(ConsoleInputExtension _)
        {
            if (_inputQueue.Count == 0)
                Console.ReadLine().Split(' ')
                    .ForEach(val => _inputQueue.Enqueue(val));
            return float.Parse(_inputQueue.Dequeue());
        }

        public static implicit operator double(ConsoleInputExtension _)
        {
            if (_inputQueue.Count == 0)
                Console.ReadLine().Split(' ')
                    .ForEach(val => _inputQueue.Enqueue(val));
            return double.Parse(_inputQueue.Dequeue());
        }
    }

    public static class ConsoleOutputExtension
    {
        private static StringBuilder _builder = new StringBuilder();

        public static void Cout(object o)
        {
            _builder.Append(o);
        }


        public static void CoutF(object o)
        {
            _builder.Append(o);
            Flush();
        }

        public static void Coutln(object o)
        {
            _builder.Append(o);
            _builder.Append('\n');
        }

        public static void Coutln()
        {
            _builder.Append('\n');
        }

        public static void CoutlnF(object o)
        {
            _builder.Append(o);
            _builder.Append('\n');
            Flush();
        }

        public static void Flush()
        {
            Console.Write(_builder.ToString());
            _builder.Clear();
        }
    }

    public static class MathExtension
    {
        //最小公倍数
        public static int LCM(int num1, int num2)
        {
            var gcd = GCD(num1, num2);
            return num1 * (num2 / gcd);
        }

        public static long LCM(long num1, long num2)
        {
            var gcd = GCD(num1, num2);
            return num1 * (num2 / gcd);
        }

        //最大公約数
        public static int GCD(int num1, int num2)
        {
            int a = Math.Max(num1, num2);
            int b = Math.Min(num1, num2);
            if (b == 0) return a;
            int mod;
            while ((mod = a % b) != 0)
            {
                a = b;
                b = mod;
            }

            return b;
        }

        public static long GCD(long num1, long num2)
        {
            long a = Math.Max(num1, num2);
            long b = Math.Min(num1, num2);
            if (b == 0) return a;
            long mod;
            while ((mod = a % b) != 0)
            {
                a = b;
                b = mod;
            }

            return b;
        }
    }

    public static class EnumerableExtension
    {
        public delegate void Function<in T>(T val);

        public static void ForEach<T>(this IEnumerable<T> enumerable, Function<T> function)
        {
            foreach (var x in enumerable)
            {
                function(x);
            }
        }
    }
}