{"raw_statement":[{"iden":"statement","content":"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.\n\nIvan 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:\n\n*   1 _l__i_ _r__i_ _v__i_ — for each _x_ such that _l__i_ ≤ _x_ ≤ _r__i_ _a__x_ ≥ _v__i_;\n*   2 _l__i_ _r__i_ _v__i_ — for each _x_ such that _l__i_ ≤ _x_ ≤ _r__i_ _a__x_ ≤ _v__i_.\n\nAlso 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:\n\n, where _cnt_(_i_) is the number of occurences of _i_ in the array.\n\nHelp Ivan to determine minimum possible _cost_ of the array that corresponds to the facts!"},{"iden":"input","content":"The first line contains two integer numbers _n_ and _q_ (1 ≤ _n_ ≤ 50, 0 ≤ _q_ ≤ 100).\n\nThen _q_ lines follow, each representing a fact about the array. _i_\\-th line contains the numbers _t__i_, _l__i_, _r__i_ and _v__i_ for _i_\\-th fact (1 ≤ _t__i_ ≤ 2, 1 ≤ _l__i_ ≤ _r__i_ ≤ _n_, 1 ≤ _v__i_ ≤ _n_, _t__i_ denotes the type of the fact)."},{"iden":"output","content":"If the facts are controversial and there is no array that corresponds to them, print _\\-1_. Otherwise, print minimum possible _cost_ of the array."},{"iden":"examples","content":"Input\n\n3 0\n\nOutput\n\n3\n\nInput\n\n3 1\n1 1 3 2\n\nOutput\n\n5\n\nInput\n\n3 2\n1 1 3 2\n2 1 3 2\n\nOutput\n\n9\n\nInput\n\n3 2\n1 1 3 2\n2 1 3 1\n\nOutput\n\n\\-1"}],"translated_statement":[{"iden":"statement","content":"最近，Ivan 在调试代码时注意到一个数组 #cf_span[a]。现在 Ivan 已经不记得这个数组了，但他试图修复的 bug 仍然存在，因此他认为这个数组中的数据可能有助于他复现该 bug。\n\nIvan 清楚地记得，该数组包含 #cf_span[n] 个元素，每个元素的值不小于 #cf_span[1] 且不大于 #cf_span[n]。此外，他还记得 #cf_span[q] 条关于该数组的事实。Ivan 记住的事实有两种类型：\n\n此外，Ivan 认为这个数组是一个排列，但他对此并不十分确定。他希望恢复一个符合他所记得的 #cf_span[q] 条事实、且与排列非常相似的数组。形式上，Ivan 将数组的 #cf_span[cost] 定义为：\n\n，其中 #cf_span[cnt(i)] 是数字 #cf_span[i] 在数组中出现的次数。\n\n请帮助 Ivan 确定符合所给事实的数组的最小可能 #cf_span[cost]！\n\n第一行包含两个整数 #cf_span[n] 和 #cf_span[q]（#cf_span[1 ≤ n ≤ 50]，#cf_span[0 ≤ q ≤ 100]）。\n\n接下来 #cf_span[q] 行，每行表示一条关于数组的事实。第 #cf_span[i] 行包含四个数：#cf_span[ti]、#cf_span[li]、#cf_span[ri] 和 #cf_span[vi]（#cf_span[1 ≤ ti ≤ 2]，#cf_span[1 ≤ li ≤ ri ≤ n]，#cf_span[1 ≤ vi ≤ n]，其中 #cf_span[ti] 表示事实的类型）。\n\n如果这些事实相互矛盾，不存在符合它们的数组，则输出 _-1_。否则，输出符合事实的数组的最小可能 #cf_span[cost]。"},{"iden":"input","content":"第一行包含两个整数 #cf_span[n] 和 #cf_span[q]（#cf_span[1 ≤ n ≤ 50]，#cf_span[0 ≤ q ≤ 100]）。接下来 #cf_span[q] 行，每行表示一条关于数组的事实。第 #cf_span[i] 行包含四个数：#cf_span[ti]、#cf_span[li]、#cf_span[ri] 和 #cf_span[vi]（#cf_span[1 ≤ ti ≤ 2]，#cf_span[1 ≤ li ≤ ri ≤ n]，#cf_span[1 ≤ vi ≤ n]，其中 #cf_span[ti] 表示事实的类型）。"},{"iden":"output","content":"如果这些事实相互矛盾，不存在符合它们的数组，则输出 _-1_。否则，输出符合事实的数组的最小可能 #cf_span[cost]。"},{"iden":"examples","content":"输入\n3 0\n输出\n3\n\n输入\n3 1\n1 1 3 2\n输出\n5\n\n输入\n3 2\n1 1 3 2\n2 1 3 2\n输出\n9\n\n输入\n3 2\n1 1 3 2\n2 1 3 1\n输出\n-1"}],"sample_group":[],"show_order":[],"formal_statement":"**Definitions**  \nLet $ n, q \\in \\mathbb{Z} $ with $ 1 \\leq n \\leq 50 $, $ 0 \\leq q \\leq 100 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be an array where each $ a_i \\in \\{1, 2, \\dots, n\\} $.  \nLet $ \\text{cnt}(i) = |\\{ j \\in \\{1, \\dots, n\\} \\mid a_j = i \\}| $ be the frequency of value $ i $ in $ A $.  \nDefine the **cost** of $ A $ as:  \n$$\n\\text{cost}(A) = \\sum_{i=1}^n \\text{cnt}(i)^2\n$$\n\nLet $ F = \\{ (t_k, l_k, r_k, v_k) \\mid k \\in \\{1, \\dots, q\\} \\} $ be a set of constraints, where for each fact:  \n- If $ t_k = 1 $: $ \\forall j \\in [l_k, r_k],\\ a_j = v_k $  \n- If $ t_k = 2 $: $ \\forall j \\in [l_k, r_k],\\ a_j \\neq v_k $\n\n**Constraints**  \n1. For each $ k \\in \\{1, \\dots, q\\} $:  \n   - $ 1 \\leq l_k \\leq r_k \\leq n $  \n   - $ 1 \\leq v_k \\leq n $  \n2. All constraints in $ F $ must be simultaneously satisfiable.  \n\n**Objective**  \nFind the minimum possible $ \\text{cost}(A) $ over all arrays $ A \\in \\{1, \\dots, n\\}^n $ satisfying $ F $.  \nIf no such $ A $ exists, output $ -1 $.","simple_statement":null,"has_page_source":false}