unittest
{
	assert( [ "5 2", "1 4", "2 5" ].parse.expand.solve == 1 );
	assert( [ "9 5", "1 8", "2 7", "3 5", "4 6", "7 9" ].parse.expand.solve == 2 );
	assert( [ "5 10", "1 2", "1 3", "1 4", "1 5", "2 3", "2 4", "2 5", "3 4", "3 5", "4 5" ].parse.expand.solve == 4 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nm = input.front.split.to!( long[] );
	auto abs = new long[ 2 ][ nm[ 1 ] ];
	foreach( ref ab; abs )
	{
		input.popFront;
		ab = input.front.split.to!( long[] );
		ab[] -= 1;
	}
	return tuple( nm[ 0 ], abs );
}

auto solve( long n, long[ 2 ][] abs )
{
	// 各橋の端を配列にまとめる
	auto ls = new long[ n ];
	auto rs = new long[ n ];
	foreach( ab; abs )
	{
		ls[ ab[ 0 ]     ]++;
		rs[ ab[ 1 ] - 1 ]++;
	}
	
	// 一番要望が多い橋を撤去
	auto ans = 0L;
	auto b = false;
	foreach( l, r; lockstep( ls, rs ) )
	{
		if( !b && 0 < l ) b = true;
		if(  b && 0 < r ) b = false, ans++;
	}
	return ans;
}
