/*
`..~-(+ztrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtrrtO&-.```
``(xrOI<<_``````_~~~~~~_``````_~~~~~~_``````_~~~~~~~``````_~~~~~~~_`````_~~~~~~~_``````~~~~~~~_``````~~~~~~~_``````_~~~<?1Orz.``
`.rO>_.~.._ ```````.~.~._.`````` .~.....`````` _~.....`````` _~.~..-`````` _~.~.._ ````` _.~.~._ ```````~.~.~_ ```````..~._?Oti`
.rO!` _.~~.~_ ```````~.~.._.`````` ~_-((JJ&gmQkWqHHHHHHHHHkQmggJJ--_..`````` _~.~..- ````` _~.~.._ ````` _.~..~_ ```````.~.~(rt{
zt{```` _.~..~_ ````` `.~~.._  ..JgHMHHHWyyyyyyyyyyyyyyyyyyyyyyVWHHMMHmJ..```` _~.~..-`````` _~~.~._ ````` _~..~._ ````` `.~.(rO
rr} ````` _~..~._ ````` _._(+dMHHyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyWHHMmJ. ` _~~....`````` _~.~.._ ````` _~.~.._ ````` _~(rt
rr{._ ````` _~.~.._ ```..gMHWyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyWHNa,  _~~....`````` _~.~.._ ````` _~~.~._ ````` (rr
tr{.~.- ````` _~.~~._.gMHyyyyyyyyyyyyyyyyyyyyyWyyyyyyyyyVWWyyyyyyyyyyyyyyyyyyyyWHMm, _~.~...`````` _~~.~.- ````` _~.~.~_ ````.tr
rt{~.~..- ````` _~(+HMyyyyyyyyyyyyyyyyyyyyyyWWWQHH@@@@@@@@@@@@@@@MHkkkWyyyyyyyyyyyWMN&._.~....`````` _~.~..- ````` _~..~._ ``.rt
rr} _~.~..-`````.+MMyyyyyyyyyyyWWyyyyyyyyyWH@@@@@@@@H@@H@@@H@@H@@@@@@@@@HkkyyyyyyyyyyHNx..~~....`````` _.~~..-`````` _~..~.- .rr
tr}`` _~~.~..` (MMyyyyyyyyyyyWWHyyyyyyyyyW@@@@H@@H@@H@@H@@H@@H@@H@@@H@@@@@@@HkyyyyyWyyyHN, .~.~._.`````` _.~..~.`````` _~..~.(rt
rt}```` .~..~(dMHyyyyyyyyyyyWHWyyyyyyyyyW@@H@HH@MMMHHHHHWWWHHHHHHMMMMHH@H@@@@@@HkyyyHHkyWMk. .~..._.`````` .~.....`````` _~.~(tr
rr}`````` .~(MMyyyyyyyyyyyyWMWyyyyyyyyyWH@MMHWyyyyyyyyyyyyyyyyyyyyyyyyyWHMMH@@@@@NkyyWHHkWWN,`_~.~.._ `````` ~.~....`````` ..(rr
tr{_ ``````(MHyyyyyyyyyyyyWMWyyyyyyyyyWMHVyyyyyyyyyyyyyyyyyyyyyyyyyyyyyWyyyyWHM@@@@NWyyWM@HXML ``~.~.~_ `````` .~~....`````` (rt
rt{~._ ```.MWyyyyyyyyyyyyWMWyyyyyyyyyWMWyyyyyyyyyyyyyyWyyyyyyyyyyyyyyyyWKyyyyyyVWM@@@kyyWM@@HHh.```.~.~._ ``````_.~~.._.`````.rr
rr{.~~._ .MHyyyyyyyyWKyyWMHyyyyyyyyyX@HyyyyWKyyyyyyWkdb&&+><<?+177TUUUWWNyyyyyyyyyWHMHHyyyW@@MWN.`` `.~.~._ ```````.~.~._.```.tr
tr}_.~.~(MHyyyyyyyyyWyyWMHyyyyyyyyyWMMyyyyWHyyyyyyyyyWHyyyyyykA&+j<;:;;(@<vZTUUyyyyyWWMNyyyWM@@@N.``` _~.~.._ ```````~..~._ `.rr
rt}` _~.dMyyyyyyyyyWHyyHMyyyyyyyyyyWMWyyyWMWyyyyyyyyWHWyyyyyyyyyyHWyXA&j@(:dc:;;<?7TUWHWNyyyyM@@@N.```` _~~.~._ ````` _~.~~._,rt
rr}``` (MWyyyyyyyyWMHyWHHyyyyyyyyyW@HyyWWMWyyyyyyyyWHMyyyyyyyyyyWHkyyyyWNyXAN+<:;:;;:;?NTUWyyyWH@@b ````` _~.~.._ ````` _.~~.(rr
rt}````dNyyyyyyyyyW@WW@MyyyyyyyyyWM@kQW@MkWyyyyyyyWHMHyyyyyyyyyWM@HyyyyWHyyyWHMkA++:;;:dR:j27UWWM@@p-`````` _~.~.._ ````` _.~(tr
rr{.``.MHyyyyyyyyyM@yWHHyyyyyyyyyW@NW@@MWHHHMHWkyWM8dWyyyyyyyyWHMWWyyyyMNyyyyM@MkyyHm&+;Wc;?m::?7H@@c..`````` _~~.~.-`````` _(rt
tr{....MWyyyyyyyyX@@W@MyyyyyyyyyWM@@@HMyyyyyyWHWWM3jNyyyyyyyyWHHEdWyyyX@WHyyyWKWMkyW@kyyXN-;JR:;;;dMN/...`````` _~....-``````.rr
rt{.~.JNyyyyyyyyyW@@WHHyyyyyyyyyXH@@MHyyyyWWMMWH8><dHyyyyyyyW@@#+HyyyyH3?MkWmHNgHMkyHMWyyHHykdp;:;;?Mb~....`````` _~~....````.tr
rr{.~.d#yyyyyyyyyW@@@MyyyyyyyyyyW@HMyyWWWHHMHHB1<!.MyyyyyyWW@H@<jNyyyW@>>?NyyyMI>WHMH@HyyWNyyWMkA+<:?Hr~~....`````` _~~....``.rt
tr}` .WHyyyyyyyyyH@@@NyyyyyyyyyWHMWQHHMBWHHHB1<!. -#yyyyWW@@H8><(NyyW#1><_JNyyMI>?MNX@@WyXMWyyWNyyyk&<Ue.~.~...`````` _~~..~..rr
rt}```WHyyyyyyyyyM@@HHyyyyyyyyWW@@@H9CuHMY6><~...-HHyyyWH@@M3<!_JHyWM3><_..MkyMI>>JMW@@HyyHHyyWMyyyyyykdh-.~.~._.`````` .~...(tr
rr}```dHyyyyyyZyy@@@HHyyyyyyyyWMHHH@MHHNmg&-----..MyWWH@@#6<!..-HWWM3><....JNyMI><(WN@@HyW@MyyyHkyyyyyyyWN,_.~~.._.`````` .~.(rt
rt} ``dHyyyyyyWHBYHH@WyyyyyyyyZH{d@HH@H@@HH@HHH@MMWW@@MBC!_.....NW#C<!......MXM1?: JM@@NyWH@yyyWHyyyyyyyyyH&``.~~.~_ `````` .(rr
rr{._ JNyyyyyW@!...WMyyyyyyyyyyH<H@H@H@H#M#WMM@#wMH9=<~......`-dH8><_...... dH@><_.(M@@HyW@@yyyWHyyyyyyyyyyWh  `.~.~._ ``````(rt
tr{~.~(NyyyyW@..gHm,MyyyyyyyyyZH(MH@H@H@  _~~~~_............` (@<<_..,M@@@@HMHM@Ht.(W@@HXH@MyyyWHyyyWyyyyyyyWh.` `.~~.~_ ````.rr
rt{.~.(MyyyyH\ W1>?NHyyyyyyyyyym.M@H@H@Hh._~~~~.....`..`..`..........(HH@HHMHYYYB~..W@@WMM@MyyyWHHyyWMkyyyyyyWN,`` _.~..~_ ``.tr
rr} _~.dHyyWM_.1>>>?Myyyyyyyyyyg.H@@H@H@HF     .`..`....`........... d@HH@# (<~~_...W@MMFd@HyyyH@@NyyW@kyyyyyWXN,``` _.~..._ .rt
tr}`` _(NyyyM;.(>>>>WWyyyyyyyyZH(HH@H@H@M:     ..`....`....`.`..`....M@@H@Ha.  `...-H@~M:JMWyyWM@@@NkyH@HyyyyWMkH/```` _~~.~.(rr
rt}```` qkyyWb.._<>>dHyyyyyyyyyHJNMH@HMM@     ......`....`....`...`..MH@H@HM\   ...._..#`JMyyyW%?M@@MkW@HkyyyyWMKHx````` _~.~(tr
rr}`````.MWyyWN,.._~jNyyyyyyyyyWdMkWHHWM%    ..`..`....`....`...... (H@H@H@M    ..... Jt dWyyWF.~_U@@@k@@MWyyyyWMKM;`````` _.(rt
rt{..````,NyyyWMNa--.MyyyyyyyWyWVMHMMMHM!    ........`....`....`... dMH@HMHF   ...`.. d<(MyyW@ ` .~Z@@H@@@NyyyyyWMWN-.`````` (rr
rr{....```/NyyyyyWM@@HHk+7Wk7WkXt-_<<<_.......`..`..`...`....`..`.. dNWHHHH\  .....`..#(HyyW@````` .ZH@H@@HkyyyyyMMWb...`````.rt
tr{~.~._.``?HyyyyyW@@@Nyyk+?W&JTr:~~~~~__......`.......`...`......` XH@MHH@   ....-.-JNMyWW9_ ``````_?@@H@@NyyyyyWMNH/~...```.rr
rt}`~.~.._.`?HyyyyXH@@@kyyyk+dMn-:~::~~~:_..`.....`..`....`...`...... ?7TY'  __~~~~:_J7WW9!~.._ ``````?@@H@MyyyyyXNdW].~....`.tr
rr}```.~.~._ ?HyyyWH@@HNyyyyyX@HUN.____.......`..`......`....`..`............_~~~:~~(J$(_ _~~.~._ `````Z@@@HkyyyyX@v@] .~.~..,rt
tr}``` _~.~.._dHyyyM@@MWHyyyyyH@kWH@NaJ-.....`.....`..`....`.....`..`..`......._._~JJF____` _~.~~._ ``` W@HMHyyyyWH}Wt(,-~..~(rr
rt}````` _~~.~_HWyyM@HHyMkyyyyW@NyM@@@HMMMHaJ--.`..,TT>..`....`....`....`..--((JgWB3~._ ` ``` _.~.~._ ``,H@HHyyyyHH!JH=J] _..(rt
rr{_ ````` _~.~dHyWH@MyyWMkyyyW@@NUMHMMMMMM#vMMHNmgJ---((-(((((((JJJ+ggkWYY"=!``` _~.~..- ````` _~.~.~_ `d@HHyyyW@NW=_.HmYM-`(rr
tr{.._ ````` _.(NyX@MHyyyH@HyyyH@@HWMMMMMMMMdMMR?MMMNMNMM8dMHHH@MM@@@@@@p.~_........JJJJgggggQQNNNP.~_---,@MWWyWH#=.-(B=_.H>`.rt
rt{~.~.- ````` (NyW@HyyyW@@@MkyWH@@HWMM@HfffWHMMMHHHHMMMNdMWfWMNW8TYYY""""""7!~`` /WMMMBTMMMMMMMHHNNHMHHH#B9UMHY!..(M6&+M#~``.rr
rt{_~.~..-`````.NyMMyyyyW@@@@HNkWM@@MWMH@WWQkfWfWkWffffffpMNWMWM3>><`                !      ?"MNMHMMHHHH#l==dt.....HdH@MY!_ `.tr
rr}` _~.~...```.NWMyyyyWH@@@@HMHNkM@HHHM#4HMfWMWW@MWHHkkpWMHBd@<><`          .               .MMHHHH###M6=ldF ....uMB"!~.~..-,rt
tr}``` _~.~...`JHMyyyyyW@@@@HMmdNWM@@@NWMNWHfMHHWMWNJ=dHHMMSd8><~.-`    ..-<`               .MMMMMMMMMM#AAQHgg&-...(He` _~~.~(tr
rr}````` ..~..-dHyyyyyW@@@@@@mZHHWHVWMMHMWMHWMHNfWNWM#Ilq#OdSjgV=  ..JV1>>><-...           .MMHMMMMMMMMkkkM@#=._?YWJ.d%`` _~.(rt
rt}.`````` .~(HHyyyyyyW@@@@@M@TWsXHHHszzXMHWMWHMfWMHRlldBuH3qP..+H@@@@@@@@@HHmggag&+-...  .MM#~__~..~~_(HMY'` .....(Y=````` _(rr
rr{._.````` (MyyyyyyyW@@@@@@@<>>?BmsTHHedNfWM3UWWfMHHHHNd#! .M@@@@@@@@@@@H@@@@@@Me_?77TY9WMM@`````.~.(g9^ ````` ~~.~...``````.tr
tr{~.._ ``.dHyyyyyyyWH@@@@@#`  _s(k7HmdMMffM3:,dHfW#WWKd@!   dM@@@@@@@@@@@@@H@@@@@Ne._.~..~_ ```````.~~.~_ ``````_.~~.~_.````.rt
rt{~.~~.-(MyyyyyyyyWH@@@@@M'    N;d[!?dMHfW@>  WNfWN<._=      M@@@@@HHMMHHHWWWHM@@@@Ma,_~.~.._ ````` `.~..._ ```````.~..._.``.rr
rr} _.~(WWyyyyyyyyyW@@@@@Mt     WjMM, dNffMz:  .NffM2_        (M@@@@@kVWMHkyyyyyyWHM@@@HJ._.~~._ ````` _~~.~~_ ````` `.~~.._ .rr
rt}``.dHyyyyyyyyyyW@@@@@MF      ddWUN.MffW@>`   NffWb<         W@@@@@@MkyWHMkWyyyyyyyWWHMM@maJ-_._ ````` _~..~._ ````` `~.~~.(rt
rr}.+HyyyyyyyyyyyW@@@@@MD     ..JHyyWMHfWMC:    HffWN>         ,M@@@@@@@MkyyWHHkyyyyyyyyyyyyyWHHHma+.. ``` _~.~.~_ ````` _.~~(rr
trdHyyyyyyyyyyyyW@@@@@H#`     <_,NyyWNffW@>~    WWfWMz.         X@@H@@@@@@@HWyyWHHkyyyyyyyyyyyyyyyyyWWHHmJ-. _.~..._ ````` _.(tr
rtdyyyyyyyyyyyyW@@@@@HM!     .><.HyWMffWMC<     HHffMI_          M@@@@@@@@@@@HkyyyWHHkyyyyWWkWyyyyyyyyyyyyyWHHmJ.-~.._ ````` (rt
rtdyyyWyyyyyyyW@@@@H@M^      >+dHkyWHMMME<!     HHffHP:          ,M@@H@H@@@@@@@MkkyyyWHHkkyyyWHHkkWyyyyyyyyyyyyyWHHmgJ-_ ````.rr
rrdWWHyyyyyyyW@@@@H@@M[     (>jNBNyyyWMP!<      HNHHH@<           ?M@@@@@HH@@@@@@@HkkyyyyWHHkWyyyWHHHHkkyyyyyyyyyyyyyWHHmJ.``.rt
rtdWyyyyyyyyW@@@@H@@MMF     >>dD(NyyW@(N          <<?N!            WM@H@@@@@HH@@@@@@@HHkyyyyWHHkWyyyWHH@@HHkkWyyyyyyyyyyyVHHa,tr
wrdyyWyyyyyW@@@H@@@MM[     (>+K .WQQMQWN             ,L             WH@@HHM@@@@H@@@@@@@@@HkyyyyWHHkyyyyyHM@@@@@HkkWyyyyyyWMHkSrw
krrXHyyyyyW@@@H@@@HMM\     >>jMMHHfffffN             .P  .....      .MH@NfffpHM@@@HH@@@@@@@@HkyyyyHMkWyyyyWHM@@@@@@HkkyyyyyWKrtd
ykrrXWyyyW@@@M@@@HHM#{    ->>d@HffffffW#         _~<<<?4<!!`         ?MH@HffffpWM@@@@H@@@@@@@@HkWyyyHMHyyyyyyWH@@@@@@@HkWyX0rrs?
HMkwrrwXWMHM8JMHMMMMM>....+zwHH8UUUUWHWE...............,>.............JMWMUUUUUWMHHHHHMMMHHHHHHHMHUUUUHMUUHWUUUUHHHHHHHH9ZrrwXMh
MyyWHkwAwrrrrrrrrttrrrrrrrrrtrrtrrtrrtrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrtrrtrrrrrrrrrrrrrrrrrrrrtrrrrrrrtrrtrrrrrrrwwQgkyyyH
*/

#include<iostream>
#include<vector>
#include <algorithm>
#include <string>
#include <utility>
#include <set>
#include <map>
#include<cmath>
#include <queue>
#include <stack>
#include <numeric>
#include <cassert>
#include<map>
#include<bitset>

using lli = long long int;
lli inf = 1000000007;
using namespace std;

void solve() {
	string S;
	cin >> S;

	int N = S.length();
	// [ 桁 ][ 超したか超してないか ]
	int dp[1000010][2];
	dp[0][1] = 1;
	for (int i = 0; i < N; i++) {
		int x = S[i] - '0';
		// ピッタリ払う場合
		// 前の桁で多く払ってた場合はおつりが出る
		dp[i + 1][0] = min(dp[i][0] + x, dp[i][1] + (10 - x));
		// 多く払う場合
		// ここで超すのでさっき超した分減らしておく
		dp[i + 1][1] = min(dp[i][0] + x + 1, dp[i][1] + (10 - x - 1));
	}

	cout << dp[N][0] << endl;
}

int main()
{
	solve();
	return 0;
}

Measured memory_bytes: 9220