#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;

class StoryFromTCO { public:
	int minimumChanges(vector <int> places, vector <int> cutoff)
	{
		const int N = places.size();
		vector<pair<int,int>> cp;
		for(int i=0; i<N; ++i)
			cp.emplace_back(cutoff[i], places[i]);
		sort(cp.begin(), cp.end());

		vector<int> lm(N);  // i-th place candidate can be put at >=lm[i]
		for(int i=0; i<N; ++i) {
			int p=cp[i].second, k=0;
			for(; k<N; ++k)
				if(p <= cp[k].first)
					break;
			if(k == N)
				return -1;
			lm[i] = k;
		}
		return solve(lm);
	}

	// reorder v so that v[i]<=i for all i.
	int solve(vector<int> v)
	{
		const int N = v.size();
		const int HOLE = 0x7fffffff;
		int ans = 0;
		multiset<int> stock;
		for(int i=0; i<N; ++i)
			if(!(v[i]<=i)) {
				if(stock.empty() || !(*stock.begin()<=i)) {
					// find right most fitting elem.
					for(int k=N-1; k>i; --k)
						if(v[k]<=i) {
							stock.insert(v[k]);
							v[k] = HOLE;
							break;
						}
				}

				if(stock.empty() || !(*stock.begin()<=i))
					return -1;

				++ans;
				if(v[i] != HOLE) stock.insert(v[i]);
				v[i] = *stock.begin();
				stock.erase(stock.begin());
			}
		return ans;
	}
};

// 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(_, StoryFromTCO().minimumChanges(places, cutoff));}
int main(){

CASE(0)
	int places_[] = {20,100,500,50};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {7500,2250,150,24};
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = 3; 
END
CASE(1)
	int places_[] = {5,4,3,2,1};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {5,4,3,2,1};
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = 0; 
END
CASE(2)
	int places_[] = {1,5,5,5};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {8,6,4,2};
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = -1; 
END
CASE(3)
	int places_[] = {3,1,5};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {6,4,2};
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = 3; 
END
CASE(4)
	int places_[] = {14,11,42,9,19};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {11,16,37,41,47}
;
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = 4; 
END
CASE(5)
	int places_[] = {4,1,3,3,2,4,5,5,4,4};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {3,3,5,2,4,4,5,4,3,5};
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = 6; 
END
CASE(6)
	int places_[] = {213,177,237,444,497,315,294,104,522,635,13,26,22,276,88,249,374,17,42,245,80,553,121,625,62,105,
53,132,178,250,18,210,492,181,2,99,29,21,62,218,188,584,702,63,41,79,28,452,2};
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = {33,40,41,48,74,84,117,125,126,164,197,197,201,218,222,231,244,277,290,309,321,321,360,376,440,
442,465,477,491,513,639,645,647,675,706,710,726,736,736,765,801,838,845,854,863,865,887,902,908};
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = 23; 
END
/*
CASE(7)
	int places_[] = ;
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = ;
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = ; 
END
CASE(8)
	int places_[] = ;
	  vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 
	int cutoff_[] = ;
	  vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
