#include <stdio.h>
// 计算给定直线位置下,左右两侧矩形面积之差
int calculateAreaDiff(int R, int N, int rectangles[][4], int line) {
int leftArea = 0, rightArea = 0;
for (int i = 0; i < N; i++) {
int L = rectangles[ i][0], T = rectangles[ i][1], W = rectangles[ i][2], H = rectangles[ i][3];
if (L + W <= line) {
leftArea += W * H;
} else if (L >= line) {
rightArea += W * H;
} else {
leftArea += (line - L) * H;
rightArea += (L + W - line) * H;
}
}
return leftArea - rightArea;
}
// 使用二分查找找到使得左右两侧面积之差最小的直线位置
int findOptimalLine(int R, int N, int rectangles[][4]) {
int left = 0, right = R;
while (left < right) {
int mid = left + (right - left) / 2;
int diff = calculateAreaDiff(R, N, rectangles, mid);
if (diff > 0) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
int main() {
int R, N;
scanf("%d", &R);
scanf("%d", &N);
int rectangles[N][4];
for (int i = 0; i < N; i++) {
scanf("%d %d %d %d", &rectangles[ i][0], &rectangles[ i][1], &rectangles[ i][2], &rectangles[ i][3]);
}
int optimalLine = findOptimalLine(R, N, rectangles);
printf("%d\n", optimalLine);
return 0;
}