import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
in.nextToken();
return (int)in.nval;
}
{
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();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBNYWluCnsKCXN0YXRpYyBTdHJlYW1Ub2tlbml6ZXIgaW4gPSBuZXcgU3RyZWFtVG9rZW5pemVyKG5ldyBCdWZmZXJlZFJlYWRlcihuZXcgSW5wdXRTdHJlYW1SZWFkZXIoU3lzdGVtLmluKSkpOwoJc3RhdGljIFByaW50V3JpdGVyIG91dCA9IG5ldyBQcmludFdyaXRlcihTeXN0ZW0ub3V0KTsKCXN0YXRpYyBpbnQgbmV4dEludCgpIHRocm93cyBJT0V4Y2VwdGlvbiAgewoJCWluLm5leHRUb2tlbigpOwoJCXJldHVybiAoaW50KWluLm52YWw7Cgl9CglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KCXsKICAgIAlpbnQgeCA9IG5leHRJbnQoKSwgbiA9IG5leHRJbnQoKTsKICAgIAlpbnQgTltdW10gPSBuZXcgaW50W25dWzRdOzsKICAgIAlpbnQgdG1wID0gMDsKICAgIAlmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgIAkgICAgZm9yIChpbnQgaiA9IDA7IGogPCA0OyBqKyspCiAgICAJICAgICAgICBOW2ldW2pdID0gbmV4dEludCgpOwogICAgCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSAKICAgIAkgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIAogICAgCSAgICAgICAgZm9yIChpbnQgcSA9IDA7IHEgPCA0OyBxKyspCiAgICAJICAgICAgICAgICAgaWYgKChOW2ldWzFdID4gTltqXVsxXSB8fCBOW2ldWzNdID4gTltqXVszXSkgfHwgKCAoTltpXVsxXSA9PSBOW2pdWzFdIHx8IE5baV1bM10gPT0gTltqXVszXSApICYmIChOW2ldWzBdID4gTltqXVswXSB8fCBOW2ldWzJdID4gTltqXVsyXSkgKSApIHsKICAgIAkgICAgICAgICAgICAgICAgdG1wID0gTltpXVtxXTsKICAgIAkgICAgICAgICAgICAgICAgTltpXVtxXSA9IE5bal1bcV07CiAgICAJICAgICAgICAgICAgICAgIE5bal1bcV0gPSB0bXA7CiAgICAJICAgICAgICAgICAgfQogICAgCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAJICAgIGlmICh4ID49IE5baV1bMF0gJiYgeCA8PSBOW2ldWzJdKSAKICAgIAkgICAgICAgIHggPSAoTltpXVsxXSA8IE5baV1bM10pID8gTltpXVswXSA6IE5baV1bMl07CiAgICAJICAgIGZvciAoaW50IGogPSAwOyBqIDwgNDsgaisrKQogICAgCSAgICAgICAgTltpXVtqXSA9IDA7CiAgICAJfQogICAgCW91dC5wcmludGxuKHgpOwogICAgCW91dC5mbHVzaCgpOwoJfQp9