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



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 lrs = new long[][]( nm[ 1 ], 2 );
	foreach( ref lr; lrs )
	{
		input.popFront;
		lr = input.front.split.to!( long[] );
	}
	return tuple( nm[ 0 ], lrs );
}

auto solve( long n, long[][] lrs )
{
	// IDカードごとに通過できるゲートの数を集計
	auto gs = new long[]( n + 1 );
	foreach( lr; lrs )
	{
		gs[ lr[ 0 ] - 1 ]++;
		gs[ lr[ 1 ]     ]--;
	}
	gs.length--;
	
	// 全ゲートを通れるIDカードを数える
	auto ans = 0L;
	auto c = 0L;
	foreach( g; gs )
	{
		c += g;
		ans += ( c == lrs.length );
	}
	return ans;
}
