using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("36");
//8
}
else if (InputPattern == "Input2") {
WillReturn.Add("91");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170");
//243
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
string StrN = InputList[0];
// 最小コスト[繰り上がり有無] なDP表
var PrevDP = new Dictionary<bool, long>();
PrevDP[false] = 0;
foreach (char EachChar in StrN.ToCharArray().Reverse()) {
var CurrDP = new Dictionary<bool, long>();
foreach (var EachPair in PrevDP) {
int CurrNum = EachChar - '0';
if (EachPair.Key) {
CurrNum++;
}
Action<bool, long> UpdateAct = (pNewKey, pNewVal) =>
{
if (CurrDP.ContainsKey(pNewKey)) {
if (CurrDP[pNewKey] <= pNewVal) {
return;
}
}
CurrDP[pNewKey] = pNewVal;
};
// 今の桁で払う場合
UpdateAct(false, EachPair.Value + CurrNum);
// 次の桁で支払い、今の桁でおつりを貰う場合
UpdateAct(true, EachPair.Value + 10 - CurrNum);
}
PrevDP = CurrDP;
}
long Answer = long.MaxValue;
foreach (var EachPair in PrevDP) {
if (EachPair.Key) {
Answer = Math.Min(Answer, EachPair.Value + 1);
}
else {
Answer = Math.Min(Answer, EachPair.Value);
}
}
Console.WriteLine(Answer);
}
}