CF863F.Almost Permutation

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Recently Ivan noticed an array aa 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 nn elements in the array, and each element was not less than 11 and not greater than nn . Also he remembers qq facts about the array. There are two types of facts that Ivan remembers:

  • 11 lil_{i} rir_{i} viv_{i} — for each xx such that li<=x<=ril_{i}<=x<=r_{i} ax>=via_{x}>=v_{i} ;
  • 22 lil_{i} rir_{i} viv_{i} — for each xx such that li<=x<=ril_{i}<=x<=r_{i} ax<=via_{x}<=v_{i} .

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 qq facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the costcost of array as follows:

, where cnt(i)cnt(i) is the number of occurences of ii in the array.

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

输入格式

The first line contains two integer numbers nn and qq ( 1<=n<=501<=n<=50 , 0<=q<=1000<=q<=100 ).

Then qq lines follow, each representing a fact about the array. ii -th line contains the numbers tit_{i} , lil_{i} , rir_{i} and viv_{i} for ii -th fact ( 1<=ti<=21<=t_{i}<=2 , 1<=li<=ri<=n1<=l_{i}<=r_{i}<=n , 1<=vi<=n1<=v_{i}<=n , tit_{i} denotes the type of the fact).

输出格式

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

输入输出样例

  • 输入#1

    3 0
    

    输出#1

    3
    
  • 输入#2

    3 1
    1 1 3 2
    

    输出#2

    5
    
  • 输入#3

    3 2
    1 1 3 2
    2 1 3 2
    

    输出#3

    9
    
  • 输入#4

    3 2
    1 1 3 2
    2 1 3 1
    

    输出#4

    -1
    
首页