This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long
using namespace std;
const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);
int ans = 1e18;
int n, k, x[2][N], y[2][N];
void calc(int div){
	int bl = n / div, cnt = 0, x1, y1, cnt1 = 0;
	if(bl & 1){
		cnt += div * div * ((bl + 1) / 2) * ((bl + 1) / 2);
		cnt += div * div * (bl / 2) * (bl / 2);
		cnt1 = n * n - cnt;
		for(int i = 1;i <= k; i++){
			x1 = x[0][i];
			y1 = y[0][i];
			x1 = (x1 + div - 1) / div;
			y1 = (y1 + div - 1) / div;
			//cout << x1 << " " << y1 << endl;
			if((x1 + y1) % 2 == 0){
				cnt--;
				cnt1++;
			}
			else{
				cnt++;
				cnt1--;
			}
		}
		ans = min(ans, cnt);
		ans = min(ans, cnt1);
	}
	else{
		cnt += div * div * (bl / 2) * bl;
		cnt1 = n * n - cnt;
		for(int i = 1;i <= k; i++){
			x1 = x[0][i];
			y1 = y[0][i];
			x1 = (x1 + div - 1) / div;
			y1 = (y1 + div - 1) / div;
			if((x1 + y1) % 2 == 0){
				cnt--;
				cnt1++;
			}
			else{
				cnt++;
				cnt1--;
			}
		}
		ans = min(ans, cnt);
		ans = min(ans, cnt1);
	}
}
main()
{
	cin >> n >> k;
	for(int i = 1;i <= k; i++){
		scanf("%lld%lld%lld%lld", &x[0][i], &y[0][i], &x[1][i], &y[1][i]);
	}
	if(n % 2 == 1){
		int x1, y1;
		int cnt = n * n / 2ll + 1ll, cnt1 = n * n / 2ll;
		for(int i = 1;i <= k; i++){
			x1 = x[0][i];
			y1 = y[0][i];
			if((x1 + y1) % 2 == 0){
				cnt--;
				cnt1++;
			}
			else{
				cnt++;
				cnt1--;
			}
		}
		ans = min(ans, cnt);
		ans = min(ans, cnt1);
	}
	else{
		int x1, y1;
		int cnt = n * n / 2ll, cnt1 = n * n / 2ll;
		for(int i = 1;i <= k; i++){
			x1 = x[0][i];
			y1 = y[0][i];
			if((x1 + y1) % 2 == 0){
				cnt--;
				cnt1++;
			}
			else{
				cnt++;
				cnt1--;
			}
		}
		ans = min(ans, cnt);
		ans = min(ans, cnt1);
	}
	for(int i = 2;i * i <= n; i++){
		if(n % i == 0){
			calc(i);
			calc(n / i);
		}
	}
	cout << ans;
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
*/
Compilation message (stderr)
chessboard.cpp:64:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
chessboard.cpp: In function 'int main()':
chessboard.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld%lld", &x[0][i], &y[0][i], &x[1][i], &y[1][i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |