#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;

class ShoutterDiv1 { public:
	int count(vector <string> s1000, vector <string> s100, vector <string> s10, vector <string> s1, vector <string> t1000, vector <string> t100, vector <string> t10, vector <string> t1)
	{
		vector<int> s;
		{
			string S1000 = accumulate(s1000.begin(), s1000.end(), string());
			string S100  = accumulate(s100.begin() , s100.end() , string());
			string S10   = accumulate(s10.begin()  , s10.end()  , string());
			string S1    = accumulate(s1.begin()   , s1.end()   , string());
			for(int i=0; i<S1.size(); ++i)
				s.push_back(atoi((string(1, S1000[i])+S100[i]+S10[i]+S1[i]).c_str()));
		}
		vector<int> t;
		{
			string T1000 = accumulate(t1000.begin(), t1000.end(), string());
			string T100 = accumulate(t100.begin(), t100.end(), string());
			string T10 = accumulate(t10.begin(), t10.end(), string());
			string T1 = accumulate(t1.begin(), t1.end(), string());
			for(int i=0; i<T1.size(); ++i)
				t.push_back(atoi((string(1, T1000[i])+T100[i]+T10[i]+T1[i]).c_str()));
		}
		vector<pair<int,int> > st;
		for(int i=0; i<s.size(); ++i)
			st.push_back(make_pair(s[i], t[i]));
		sort(st.begin(), st.end());
		return solve(st);
	}

	int solve(const vector<pair<int,int> >& st)
	{
		const int N = st.size();

		int L = 99999; // the time first rabbit leaves.
		int R = 0; // the time last rabbit comes.
		for(int i=0; i<N; ++i) {
			int l = st[i].first;
			int r = st[i].second;
			L = min(L, r);
			R = max(R, l);
		}

		// fill [L,R] with minimum number of intervals.
		// must include st[i] for each i.

		int total = 0;
		for(int i=0; i<N; ++i) {
			int c = cost(st, N, i, L, R);
			if(c==-1)
				return -1;
			total += c - 1;
		}
		return total;
	}

	int cost(const vector<pair<int,int> >& st, int N, int I, int L, int R)
	{
		bool I_used = false;
		int covered = L;
		int used = 0;
		for(int ks=0; ks<N; )
		{
			int ke=ks;
			while(ke<N && st[ke].first<=covered)
				++ke;
			if(ke==ks)
				break;

			if(ks<=I && I<ke && !I_used)
			{
				++used;
				I_used = true;
				covered = max(covered, st[I].second);
			}
			else
			{
				if(covered < R) {
					int bestRight = 0;
					for(int i=ks; i<ke; ++i) if(i!=I)
						bestRight = max(bestRight, st[i].second);
					if(covered < bestRight) {
						++used;
						covered = max(covered, bestRight);
					}
				}
				ks = ke;
			}
		}
		return (R<=covered ? used : -1);
	}
};

// 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(_, ShoutterDiv1().count(s1000, s100, s10, s1, t1000, t100, t10, t1));}
int main(){

CASE(0)
	string s1000_[] = {"22", "2"};
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = {"00", "0"};
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = {"11", "1"};
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = {"21", "4"};
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = {"22", "2"};
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = {"00", "0"};
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = {"11", "1"};
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = {"43", "6"};
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = 2; 
END
CASE(1)
	string s1000_[] = {"00"};
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = {"00"};
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = {"00"};
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = {"13"};
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = {"00"};
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = {"00"};
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = {"00"};
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = {"24"};
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = -1; 
END
CASE(2)
	string s1000_[] = {"0000"};
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = {"0000"};
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = {"0000"};
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = {"1234"};
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = {"0000"};
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = {"0000"};
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = {"0000"};
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = {"2345"};
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = 6; 
END
CASE(3)
	string s1000_[] = {"0000000000"};
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = {"0000000000"};
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = {"0000000000"};
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = {"7626463146"};
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = {"0000000000"};
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = {"0000000000"};
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = {"0000000000"};
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = {"9927686479"};
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = 18; 
END
CASE(4)
	string s1000_[] = {"00000000000000000000000000000000000000000000000000"};
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = {"00000000000000000000000000000000000000000000000000"};
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = {"50353624751857130208544645495168271486083954769538"};
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = {"85748487990028258641117783760944852941545064635928"};
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = {"00000000000000000000000000000000000000000000000000"};
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = {"00000000000000000000000000000000000000000000000000"};
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = {"61465744851859252308555855596388482696094965779649"};
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = {"37620749792666153778227385275518278477865684777411"};
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = 333; 
END
/*
CASE(5)
	string s1000_[] = ;
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = ;
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = ;
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = ;
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = ;
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = ;
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = ;
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = ;
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = ; 
END
CASE(6)
	string s1000_[] = ;
	  vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 
	string s100_[] = ;
	  vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 
	string s10_[] = ;
	  vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 
	string s1_[] = ;
	  vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 
	string t1000_[] = ;
	  vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 
	string t100_[] = ;
	  vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 
	string t10_[] = ;
	  vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 
	string t1_[] = ;
	  vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
