863_F. Almost Permutation
Recently Ivan noticed an array a while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.

Ivan clearly remembers that there were n elements in the array, and each element was not less than 1 and not greater than n. Also he remembers q facts about the array. There are two types of facts that Ivan remembers:

  * 1 li ri vi — for each x such that li ≤ x ≤ ri ax ≥ vi; 
  * 2 li ri vi — for each x such that li ≤ x ≤ ri ax ≤ vi. 



Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the q facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the cost of array as follows:

<image>, where cnt(i) is the number of occurences of i in the array.

Help Ivan to determine minimum possible cost of the array that corresponds to the facts!

Input

The first line contains two integer numbers n and q (1 ≤ n ≤ 50, 0 ≤ q ≤ 100).

Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ vi ≤ n, ti denotes the type of the fact).

Output

If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.

Examples

Input

3 0


Output

3


Input

3 1
1 1 3 2


Output

5


Input

3 2
1 1 3 2
2 1 3 2


Output

9


Input

3 2
1 1 3 2
2 1 3 1


Output

-1
```python
import sys

def is_feasible(cnt,L,R):
	n = len(R)
	inter = [(L[i],R[i]) for i in range(n)]
	AUX = []
	feasible = True
	for x in range(n):
		for p in inter:
			if p[0] == x:
				AUX.append(p[1])
				
		while AUX and min(AUX) < x:
			AUX.remove(min(AUX))
		for quantity in range(cnt[x]):
			if AUX:
				AUX.remove(min(AUX))
			else:
				feasible = False
	return feasible

n,q = map(int,sys.stdin.readline().split())
L = [0 for i in range(n)]
R = [n-1 for i in range(n)]
for restriction in range(q):
	t,l,r,v = map(int,sys.stdin.readline().split())
	if t == 1:
		for k in range(l-1,r):
			L[k] = max(L[k],v-1)
	else:
		for k in range(l-1,r):
			R[k] = min(R[k],v-1)
			
is_possible = all(map(lambda x,y : x <= y,L,R))
if not is_possible:
	print(-1)
else:
	cnt = {x:L.count(x) for x in range(n)}
	had_pair = True
	while had_pair:
		had_pair = False
		for x in range(n-1,-1,-1):
			for y in range(n-1,-1,-1):
				if cnt[x] > cnt[y]+1:
					cnt[x] -= 1
					cnt[y] += 1
					has_pair = True
					if not is_feasible(cnt,L,R):
						cnt[x] += 1
						cnt[y] -= 1
						has_pair = False
					had_pair = had_pair or has_pair 
				
	
	ans = sum([cnt[x]*cnt[x] for x in range(n)])
	print(ans)
	
					
			
			
		
		
		
			
			
			
		

```