#include <iostream>
using namespace std;
int main ()
{
int x, n;
cin >> x >> n;
int N[n][4];
for (int i = 0; i < n; i++)
for (int j = 0; j < 4; j++)
cin >> N[i][j];
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]) ) )
swap(N[i][q], N[j][q]);
for (int i = 0; i < n; i++) {
if (x >= N[i][0] && x <= N[i][2])
N[i][1] < N[i][3] ? x = N[i][0] : x = N[i][2];
for (int j = 0; j < 4; j++)
N[i][j] = 0;
}
cout << x;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCmludCBtYWluICgpIAp7CglpbnQgeCwgbjsKCWNpbiA+PiB4ID4+IG47CglpbnQgTltuXVs0XTsKCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQoJCWZvciAoaW50IGogPSAwOyBqIDwgNDsgaisrKQoJCQljaW4gPj4gTltpXVtqXTsKCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSAKCQlmb3IgKGludCBqID0gMDsgaiA8IG47IGorKykgCgkJCWZvciAoaW50IHEgPSAwOyBxIDwgNDsgcSsrKQoJCQkJaWYgKChOW2ldWzFdID4gTltqXVsxXSB8fCBOW2ldWzNdID4gTltqXVszXSkgfHwgKCAoTltpXVsxXSA9PSBOW2pdWzFdIHx8IE5baV1bM10gPT0gTltqXVszXSApICYmIChOW2ldWzBdID4gTltqXVswXSB8fCBOW2ldWzJdID4gTltqXVsyXSkgKSApCgkJCQkJc3dhcChOW2ldW3FdLCBOW2pdW3FdKTsKCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CgkJaWYgKHggPj0gTltpXVswXSAmJiB4IDw9IE5baV1bMl0pIAoJCQlOW2ldWzFdIDwgTltpXVszXSA/IHggPSBOW2ldWzBdIDogeCA9IE5baV1bMl07CgkJZm9yIChpbnQgaiA9IDA7IGogPCA0OyBqKyspCgkJCU5baV1bal0gPSAwOwoJfQoJY291dCA8PCB4OwogCglyZXR1cm4gMDsKfQ==