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

template<typename To, typename From>
	To lexical_cast( const From& f )
	{
		stringstream ss;
		ss << f;
		To t;
		ss >> t;
		return t;
	}


class RailwayTickets { public:
	int minRejects(vector <string> travel, int seats) 
	{
		typedef pair<bool,int> event; // [true,A  --- false,B)
		vector<event> evs;

		for(int i=0; i<travel.size(); ++i)
		{
			stringstream ss(travel[i]);
			for(string s; ss>>s; )
			{
				int k = s.find('-');
				int A = lexical_cast<int>( s.substr(0, k) );
				int B = lexical_cast<int>( s.substr(k+1) );
				evs.push_back( event(true,A) );
				evs.push_back( event(false,B) );
			}
		}

		sort( evs.begin(), evs.end() );

		vector< vector<int> > minDeny(evs.size()+1, vector<int>(seats+1));
		for(int ti=evs.size()-1; ti>=0; --ti)
			for(int si=0; si<=seats; ++si)
				if( evs[ti].first )
				{
					if( si==0 )
						minDeny[ti][si] = minDeny[ti+1][si]+1;
					else
						minDeny[ti][si] = min( minDeny[ti+1][si]+1, minDeny[ti+1][si-1] );
				}
				else
				{
					minDeny[ti][si] = minDeny[ti+1][si+1];
				}
		return minDeny[0][seats];
	}
};

// 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(_, RailwayTickets().minRejects(travel, seats));}
int main(){

CASE(0)
	string travel_[] = {"1-3 3-5",
 "2-4 4-6",
 "1-2 2-3 3-4 4-5"};
	  vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int seats = 2; 
	int _ = 2; 
END
CASE(1)
	string travel_[] = {"1-10000","2-10000","1-9999","2-9999","5000-5001"};
	  vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int seats = 3; 
	int _ = 2; 
END
CASE(2)
	string travel_[] = {"1-2 2-5 2-8 234-236 56-878 6-34",
 "234-776 3242-8000 999-1000 3-14",
 "121-122 435-3455 123-987 77-999"};
	  vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int seats = 1000; 
	int _ = 0; 
END
CASE(3)
	string travel_[] = {"1-2 2-3 3-4 4-5 5-6 6-7 1-3 2-7 1-2 1-4"};
	  vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int seats = 1; 
	int _ = 4; 
END
CASE(4)
	string travel_[] = {"1-2","1-2"};
	  vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int seats = 1; 
	int _ = 1; 
END
CASE(5)
	string travel_[] = {"1-2"};
	  vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int seats = 1; 
	int _ = 0; 
END

}
// END CUT HERE
