import java.io.*;
import java.util.*;

// The "Template" class.
public class Problem3a
{
    public static void main (String[] args) throws IOException
    {

	// Open up the input and output file for IO purpose
	FileReader inFile = new FileReader ("DATA3.txt");
	FileWriter outFile = new FileWriter ("OUT3.txt");

	// Link the input and output file for
	BufferedReader in = new BufferedReader (inFile);
	BufferedWriter out = new BufferedWriter (outFile);

	String line;
	int count = 0;

	// Keep reading as long as not end of file (eof)
	while ((line = in.readLine ()) != null)
	{
	    int memory = Integer.parseInt (line);
	    line = in.readLine ();
	    int filecount = Integer.parseInt (line);
	    String[] songs = new String [filecount];

	    for (int i = 0 ; i < filecount ; i++)
	    {
		songs [i] = in.readLine ();
	    }

	    int[] rate = new int [filecount];
	    int[] mem = new int [filecount];

	    for (int i = 0 ; i < filecount ; i++)
	    {
		StringTokenizer token = new StringTokenizer (songs [i]);
		token.nextToken ();
		rate [i] = Integer.parseInt (token.nextToken ());
		mem [i] = Integer.parseInt (token.nextToken ());
	    }

	    int smallest, largest;
	    int rating;
	    int[] rateSorted = new int [filecount];
	    int[] memSorted = new int [filecount];

	    // Find the largest

	    // Set largest to the first number and repeatedly compare
	    // with rest of the numbers.  If largest is smaller than
	    // the compared number, update largest to compared number.
	    largest = mem [0];
	    for (int i = 1 ; i < memSorted.length ; i++)
	    {
		if (largest < mem [i])
		{
		    largest = mem [i];
		}
	    }

	    // Scan the list 10 times to pick out the smallest number each time
	    for (int scan = 0 ; scan < mem.length ; scan++)
	    {

		// Find the smallest

		// Set smallest to the first number and repeatedly compare
		// with rest of the numbers.  If smallest is bigger than
		// the compared number, update smallest to compared number.

		smallest = mem [0];
		rating = rate [0];
		
		mem [0] = largest;
		


		for (int i = 1 ; i < mem.length ; i++)
		{

		    if (smallest > mem [i])
		    {
			int temp = smallest;
			int tempR = rating;
			
			smallest = mem [i];
			rating = rate [i];
			
			mem [i] = temp;
			rate [i] = tempR;
		    }
		}

		memSorted [scan] = smallest;
		rateSorted [scan] = rating;

	    }
	    
	    int w = 0;
	    int ranking = 0;
	    
	    while (memory > 0)
	    {
		memory -= memSorted[w];
		ranking += rateSorted[w];
		
		if (memory < 0)
		{
		    ranking -= rateSorted[w];
		}
		w++;
	    
	    }
	    out.write(""+ ranking);
	    out.newLine();

	}
	
	
	// Close input and output file
	in.close ();
	out.close ();
    } // main method
} // End of Class



