using System;
using System.Collections.Generic;
using System.Linq;
using static System.Math;

namespace CSharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            var n = ReadString().Select(c => c - '0').ToArray();
            //long ans = 0;

            //bool payOtsuri = false;

            //for (int i = n.Length - 1; i >= 0; i--)
            //{
            //    int digit = payOtsuri ? n[i] + 1 : n[i];
            //    if (digit == 10)
            //    {
            //        continue;
            //    }

            //    int otsuri = 10 - digit;
            //    int pay = digit;
            //    if (pay <= otsuri)
            //    {
            //        ans += pay;
            //        payOtsuri = false;
            //    }
            //    else if (pay > otsuri)
            //    {
            //        ans += otsuri;
            //        payOtsuri = true;
            //    }
            //}

            //if (payOtsuri)
            //{
            //    ans++;
            //}

            long ans = Solve(n, 0, 0, new long?[n.Length + 10, 11]);
            Console.WriteLine(ans);
        }

        static long Solve(int[] n, int index, int carry, long?[,] memo)
        {
            if (index >= n.Length)
            {
                return carry > 0 ? 1 : 0;
            }

            if (memo[index, carry] != null)
            {
                return memo[index, carry].GetValueOrDefault();
            }

            long ans = long.MaxValue;
            int digit = n[index] + carry;
            ans = Min(ans, Solve(n, index + 1, 0, memo) + digit);
            ans = Min(ans, Solve(n, index + 1, 1, memo) + 10 - digit);

            return (memo[index, carry] = ans).GetValueOrDefault();
        }

        #region Helpers

        static int[] ReadIntArray() => Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
        static long[] ReadLongArray() => Console.ReadLine().Split().Select(s => long.Parse(s)).ToArray();

        static int[] ReadLineIntArray(int count) => Enumerable.Repeat(0, count).Select(_ => int.Parse(Console.ReadLine())).ToArray();
        static long[] ReadLineLongArray(int count) => Enumerable.Repeat(0, count).Select(_ => long.Parse(Console.ReadLine())).ToArray();

        static string ReadString() => Console.ReadLine();
        static int ReadInt() => int.Parse(Console.ReadLine());
        static long ReadLong() => long.Parse(Console.ReadLine());

        static void ReadInt(out int first)
        {
            var array = ReadIntArray();
            first = array[0];
        }

        static void ReadInt(out int first, out int second)
        {
            var array = ReadIntArray();
            first = array[0];
            second = array[1];
        }

        static void ReadInt(out int first, out int second, out int third)
        {
            var array = ReadIntArray();
            first = array[0];
            second = array[1];
            third = array[2];
        }

        static void ReadInt(out int first, out int second, out int third, out int fourth)
        {
            var array = ReadIntArray();
            first = array[0];
            second = array[1];
            third = array[2];
            fourth = array[3];
        }

        static void ReadLong(out long first)
        {
            var array = ReadLongArray();
            first = array[0];
        }

        static void ReadLong(out long first, out long second)
        {
            var array = ReadLongArray();
            first = array[0];
            second = array[1];
        }

        static void ReadLong(out long first, out long second, out long third)
        {
            var array = ReadLongArray();
            first = array[0];
            second = array[1];
            third = array[2];
        }

        static void ReadLong(out long first, out long second, out long third, out long fourth)
        {
            var array = ReadLongArray();
            first = array[0];
            second = array[1];
            third = array[2];
            fourth = array[3];
        }

        #endregion
    }
}

Measured memory_bytes: 355040