#include <bits/stdc++.h>
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define int long long
#define ll long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
 
using namespace std;
 
const int N = 3e5 + 123;
const int inf = 2e18;
const int mod = 1e9 + 7;
 
int a[N], b[N], c[N][2], n, m, q, k, z, w, x, y, ans;
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
 
int rand( int l, int r )
{
    uniform_int_distribution <int> uid( l, r );
    return uid( rng );
}
vector <int> v;
int count( int bl, int x, int y )
{
	if( x == 0 || y == 0 ) return 0;
	int bx = x / bl, by = y / bl; 
	int res = bx * by / 2 * bl * bl;
	res += ((bx + by % 2) / 2) * (y - by * bl) * bl;
    res += ((by + bx % 2) / 2) * (x - bx * bl) * bl;
    return res + ((bx + by) % 2) * (x - bx * bl) * (y - by * bl);
}
void solve()
{
	cin >> n >> k;
	
	FOR( i, 1, sqrt(n), 1 )
	{
		if( n % i == 0 )
		{
			if( i > 1 && i != n / i ) v.pb(n / i);
			v.pb(i);
		}
	}
	FOR( i, 0, sz(v) - 1, 1 )
	{
		if( n / v[i] % 2 )
		{
			c[i][0] = ( n * n - v[i] * v[i] ) / 2 + v[i] * v[i];
			c[i][1] = ( n * n - v[i] * v[i] ) / 2;
		}
		else c[i][0] = c[i][1] = n * n / 2;
	}
	FOR( pldf, 1, k, 1 )
	{
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		
		FOR( i, 0, sz(v) - 1, 1 )
		{
			int black = count( v[i], x2, y2 ) - count( v[i], x1 - 1, y2 ) - count( v[i], x2, y1 - 1 ) + count( v[i], x1 - 1, y1 - 1 );
			int obshi = ( x2 - x1 + 1 ) * ( y2 - y1 + 1 );
			
			c[i][1] -= black, c[i][0] += black;
			c[i][0] -= obshi - black, c[i][1] += obshi - black;
		}
	}
	ans = inf;
	FOR( i, 0, sz(v) - 1, 1 ) ans = min( ans, min( c[i][0], c[i][1] ));
	cout << ans;
}
signed main() 
{
//	freopen("connect.in", "r", stdin);
//	freopen("connect.out", "w", stdout);
	    
    respagold
    
    int test = 1;
    
    if( !test ) cin >> test;
	
	while( test -- )
	{
		solve();
	}
}
// solved by KluydQ
/*
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
*/
| # | 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... |