#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
template<typename T>
struct DP3
{
int N1, N2, N3;
vector<T> data;
DP3(int N1, int N2, int N3, const T& t = T())
: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<28)); }
T& operator()(int i1, int i2, int i3)
{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
void swap(DP3& rhs)
{ data.swap(rhs.data); }
};
class CombinationLockDiv1 { public:
int minimumMoves(vector <string> P, vector <string> Q)
{
string S = accumulate(P.begin(), P.end(), string());
string T = accumulate(Q.begin(), Q.end(), string());
vector<int> U;
for(int i=0; i<S.size(); ++i)
U.push_back((T[i]-S[i]+10) % 10);
return solve(U);
}
int solve(const vector<int>& U)
{
const int INF = 50*50*20;
const int Q = 10;
DP3<int> dp(U.size()+1, Q, Q, INF);
dp(0,0,0) = 0;
for(int i=0; i<U.size(); ++i)
{
for(int m=0; m<Q; ++m)
for(int p=0; p<Q; ++p)
{
for(int mm=0; mm<=m; ++mm)
for(int pp=0; pp<=p; ++pp) {
int v = (pp-mm+10)%10;
int dip = (U[i]-v+10)%10;
int dim = (10-dip)%10;
int mmm = min(dim+mm,Q-1);
int ppp = min(dip+pp,Q-1);
dp(i+1,mmm,pp) = min(dp(i+1,mmm,pp), dp(i,m,p) + dim);
dp(i+1,mm,ppp) = min(dp(i+1,mm,ppp), dp(i,m,p) + dip);
}
}
}
int best = INF;
for(int m=0; m<Q; ++m)
for(int p=0; p<Q; ++p)
best = min(best, dp(U.size(), m, p));
return best;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, CombinationLockDiv1().minimumMoves(P, Q));}
int main(){
CASE(0)
string P_[] = {"123"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"112"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 1;
END
CASE(1)
string P_[] = {"1"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"7"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 4;
END
CASE(2)
string P_[] = {"6","07"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"","60","7"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 0;
END
CASE(3)
string P_[] = {"1234"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"4567"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 3;
END
CASE(4)
string P_[] = {"020"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"909"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 2;
END
CASE(5)
string P_[] = {"4423232218340"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"6290421476245"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 18;
END
CASE(6)
string P_[] = {"13582011523174667280008414541351904422031012238076","41048335045579967968275673754522871011068601257039","04855357969852282416624125536962426385082554121889","78074757536331862021838625347095667990535948893888","05579320225639822730235008835013806387280549085691","80899386988536534438059318016416835580192334813917","95341464766162715473052668803298477731786141378744","69816239602790838593717476180717906897243175749123","24738439366743704835924900681247874798806208147751","45337850860527204796077538037584119110987495462956","19109623542297344993947313914964412516346367095244","96248289645824455435790136083954599263965549130880","58401114021732043302864235558929780726578985430340","14418544383945031017494746702943514243376501190225","66249685629466035559385559250659296957755747196013","04785111927450904580203778222015365726498982699825","15269688063687053428204228967590278365731589445556","52836901795992325405969222392137556237720751803897","73917282470183746697363519879638377830810448700972","48931852957642664180697841772815067658315516508820","28656501252849429602603172562329240976023161940826","37156083646002750183572507124427592539716213493097","48063714185666861796912908924538985163177952016299","52773362719445635074535987829747253807426846754632","58904953136889913785986347937686998493070883951354","16945161728053635470076872669538849220363955440232","01316771478426257681554570729231953678202745236445","80547194249309162089697847348429235299584464070011","48909217233221310233477898131778996139508912874381","57165921870900700928516353466840515863348321007080","03199380285346276716385704233782058602186477541273","57629586195572946196250765990226852191103936041714","53446459026412049802639693633102627103026448385312","10449560544606230079321830630030549315203948785881","78865644328326729238917398515310302163206950711046","93807876100811118037712483047336372657089771808462","95924064423619365461974775778936847567532168396539","30934522552730239320804167555179733248719500179913","92449062520683014513899530376735799931693464212672","18439075607384060207916758028210747792158690034945","67550046930243544877782111951966692512319844028670","60767792508462968711880673532076795668800379421487","78121092917010361170954018211894626946083703760055","49667352485242602855643266038304470530983154846508","15168929880962349141948246852672003537639087309111","29037544243698149476895123319828479381872954148263","93686919265019496323833587775066697795417518561622","54995834325316496397379891082659236727599918638569","04499269120952438849149871831661730991764176812547","79371457213323740197817329649628939319568091637705"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"26585546315429895673160149821124323624385723648950","93145975424958492531929386719584878797847740043845","95701479323952655390238230967385653534124002661507","23983653758767357093823783993402052207751233494459","98711583090998839780395749138443275449931653742603","61293167602041603340392289107485524474795134870395","74512418862972537528310814463762318220469210316020","03358080324524140549537033852110007943188267480443","31646977753094077695930166382158316905727440462903","11002846107507123033068113706085961508369204903779","99741245890202804099741453386497577761969504241049","47836160882024796482460880683993868569526410623646","96107764672367326319737218357764159082400331128350","36961091275709970680500985451120241946922775875678","88397008935577205128749102059410926508484539966798","80534906503785197224539831265662419380733454092957","09475003340691062577096422764671636498967245119340","50433401131294882868176859626470398871396863774112","13802400129313166669538277217316956804639201622263","17257081790858760560971773120609978519424690526864","18019455769163702271286521059595276090770258883368","60351517055058669406347326556613386863411274223935","31394694251586651119602376269072383189130168471397","64862905602117845471790840660568880992748206616344","03722072134979404818149642072110006125367213097061","56890392458412031324407801718803651830274463787651","02604877724543786395576787946929929240596339245669","42373529984023882276028307690922789630695888449704","58231765898533664834822889266470284277354443339763","97209446654486428990686495351862368409922596851015","89749090720796049484610991644351923011565368078007","12740957679811435790969770507372742994884543250962","61607759784379577542186922781270084740937911060159","51864665482708227453841279720353622438748574669914","37976869128043879687016809498880435159399942579534","61200345600233010939778430702655952933023559483785","52956567802930503732745175189074850180362655567605","62376327002164238083885086706807442475301676912314","16570236775930283353485612619103181529970843080790","79555154762437866313663093223224491840166126405296","09352082575345052392287259550484807145788077671444","59554166683997533509509207485780871667325264585949","98764945336111968229089144120495247293714158297736","13309148762592847300050367505355443271678493638940","16190173208191757395742636058696599858531306877468","11441600207487016269710089937177986280975584413878","46684067545737865090159099578555995856800545435052","28369570052939976082383431980352736051623861461400","28591566192943839521108624118212754637294867570363","47161056270197028192623737725828173240010898334910"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = -1;
END
CASE(7)
string P_[] = {"000000"};
vector <string> P(P_, P_+sizeof(P_)/sizeof(*P_));
string Q_[] = {"456789"};
vector <string> Q(Q_, Q_+sizeof(Q_)/sizeof(*Q_));
int _ = 6;
END
}
// END CUT HERE