import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(System.out);
	static int nextInt() throws IOException  {
		in.nextToken();
		return (int)in.nval;
	}
	public static void main (String[] args) throws java.lang.Exception
	{
    	int x = nextInt(), n = nextInt();
    	int N[][] = new int[n][4];;
    	int tmp = 0;
    	for (int i = 0; i < n; i++)
    	    for (int j = 0; j < 4; j++)
    	        N[i][j] = nextInt();
    	for (int i = 0; i < n; i++) 
    	    for (int j = 0; j < n; j++) 
    	        for (int q = 0; q < 4; q++)
    	            if ((N[i][1] > N[j][1] || N[i][3] > N[j][3]) || ( (N[i][1] == N[j][1] || N[i][3] == N[j][3] ) && (N[i][0] > N[j][0] || N[i][2] > N[j][2]) ) ) {
    	                tmp = N[i][q];
    	                N[i][q] = N[j][q];
    	                N[j][q] = tmp;
    	            }
    	for (int i = 0; i < n; i++) {
    	    if (x >= N[i][0] && x <= N[i][2]) 
    	        x = (N[i][1] < N[i][3]) ? N[i][0] : N[i][2];
    	    for (int j = 0; j < 4; j++)
    	        N[i][j] = 0;
    	}
    	out.println(x);
    	out.flush();
	}
}