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



import std.algorithm;
import std.container;
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[ 0 ] ];
	foreach( ref ab; abs )
	{
		input.popFront;
		ab = input.front.split.to!( long[] );
	}
	return tuple( nm[ 1 ], abs );
}

auto solve( long m, long[ 2 ][] abs )
{
	abs.sort();
	
	auto ans = 0L;
	auto bs = heapify( new long[ abs.length ], 0 );
	foreach( d; 1L .. m + 1 )
	{
		while( !abs.empty && abs.front[ 0 ] <= d )
		{
			bs.insert( abs.front[ 1 ] );
			abs.popFront;
		}
		if( bs.empty ) continue;
		ans += bs.front;
		bs.popFront;
	}
	return ans;
}
