{"trustable":false,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n section pre {\n display: block;\n padding: 9.5px;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n word-break: break-all;\n word-wrap: break-word;\n color: #333;\n background: rgba(255, 255, 255, 0.5);\n border: 1px solid #ccc;\n border-radius: 6px;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\(\u0027, right: \u0027\\\\)\u0027, display: false},\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"Problem Statement","value":{"format":"HTML","content":"\u003cscript type\u003d\"text/x-mathjax-config\"\u003eMathJax.Hub.Config({messageStyle:\"none\",tex2jax:{skipTags:[\"script\",\"noscript\",\"style\",\"textarea\",\"code\"],inlineMath:[[\u0027\\\\(\u0027,\u0027\\\\)\u0027]]},delayStartupUntil:\"onload\"});\u003c/script\u003e\n\u003cscript src\u003d\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config\u003dTeX-MML-AM_CHTML\"\u003e\u003c/script\u003e\n\u003ch3\u003e\u003c/h3\u003e\n\u003cp\u003eThere are \u003cvar\u003e\\(N\\)\u003c/var\u003e kinds of magical gems, numbered \u003cvar\u003e\\(1, 2, \\ldots, N\\)\u003c/var\u003e, distributed in the AtCoder Kingdom.\u003cbr\u003e Takahashi is trying to make an ornament by arranging gems in a row.\u003cbr\u003e For some pairs of gems, we can put the two gems next to each other; for other pairs, we cannot. We have \u003cvar\u003e\\(M\\)\u003c/var\u003e pairs for which the two gems can be adjacent: (Gem \u003cvar\u003e\\(A_1\\)\u003c/var\u003e, Gem \u003cvar\u003e\\(B_1\\)\u003c/var\u003e), (Gem \u003cvar\u003e\\(A_2\\)\u003c/var\u003e, Gem \u003cvar\u003e\\(B_2\\)\u003c/var\u003e), \u003cvar\u003e\\(\\ldots\\)\u003c/var\u003e, (Gem \u003cvar\u003e\\(A_M\\)\u003c/var\u003e, Gem \u003cvar\u003e\\(B_M\\)\u003c/var\u003e). For the other pairs, the two gems cannot be adjacent. (Order does not matter in these pairs.)\u003cbr\u003e Determine whether it is possible to form a sequence of gems that has one or more gems of each of the kinds \u003cvar\u003e\\(C_1, C_2, \\dots, C_K\\)\u003c/var\u003e. If the answer is yes, find the minimum number of stones needed to form such a sequence.\u003c/p\u003e\n\u003cbr\u003e\n给你n个宝石,有m对(a[i],b[i])关系,以及k种必须出现的宝石\u003cbr\u003e\n你要保证出现k种必须出现的宝石(每种宝石\u003e\u003d1即可),且出现的宝石要满足m对关系的两两相邻 求最少的宝石数,不满足则直接输出-1\u003cbr\u003e\n1\u003c\u003dn\u003c\u003d1e5 ,0\u003c\u003dm\u003c\u003d1e5,1\u003c\u003dk\u003c\u003d17\u003cbr\u003e"}},{"title":"Constraints","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cul\u003e \n \u003cli\u003eAll values in input are integers.\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 ≤\u0026nbsp;N ≤ 10^5\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(0 ≤\u0026nbsp;M ≤ 10^5\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 ≤ A_i \u0026lt; B_i ≤ N\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003eIf \u003cvar\u003e\\(i ≠ j\\)\u003c/var\u003e, \u003cvar\u003e\\((A_i, B_i) ≠ (A_j, B_j)\\)\u003c/var\u003e.\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 ≤ K ≤ 17\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 ≤ C_1 \u0026lt; C_2 \u0026lt; \\dots \u0026lt; C_K ≤ N\\)\u003c/var\u003e\u003c/li\u003e \n\u003c/ul\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cp\u003eInput is given from Standard Input in the following format:\u003c/p\u003e \n\u003cpre\u003e\u003cvar\u003e\\(N\\)\u003c/var\u003e \u003cvar\u003e\\(M\\)\u003c/var\u003e\n\u003cvar\u003e\\(A_1\\)\u003c/var\u003e \u003cvar\u003e\\(B_1\\)\u003c/var\u003e\n\u003cvar\u003e\\(A_2\\)\u003c/var\u003e \u003cvar\u003e\\(B_2\\)\u003c/var\u003e\n\u003cvar\u003e\\(\\hspace{7mm}\\vdots\\)\u003c/var\u003e\n\u003cvar\u003e\\(A_M\\)\u003c/var\u003e \u003cvar\u003e\\(B_M\\)\u003c/var\u003e\n\u003cvar\u003e\\(K\\)\u003c/var\u003e\n\u003cvar\u003e\\(C_1\\)\u003c/var\u003e \u003cvar\u003e\\(C_2\\)\u003c/var\u003e \u003cvar\u003e\\(\\cdots\\)\u003c/var\u003e \u003cvar\u003e\\(C_K\\)\u003c/var\u003e\n\u003c/pre\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cp\u003ePrint the minimum number of stones needed to form a sequence of gems that has one or more gems of each of the kinds \u003cvar\u003e\\(C_1, C_2, \\dots, C_K\\)\u003c/var\u003e.\u003cbr\u003e If such a sequence cannot be formed, print \u003ccode\u003e-1\u003c/code\u003e instead.\u003c/p\u003e"}},{"title":"Sample Input 1","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e4 3\n1 4\n2 4\n3 4\n3\n1 2 3\n\u003c/pre\u003e"}},{"title":"Sample Output 1","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e5\n\u003c/pre\u003e \n\u003cp\u003eFor example, by arranging the gems in the order \u003cvar\u003e\\([1, 4, 2, 4, 3]\\)\u003c/var\u003e, we can form a sequence of length \u003cvar\u003e\\(5\\)\u003c/var\u003e with Gems \u003cvar\u003e\\(1, 2, 3\\)\u003c/var\u003e.\u003c/p\u003e"}},{"title":"Sample Input 2","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e4 3\n1 4\n2 4\n1 2\n3\n1 2 3\n\u003c/pre\u003e"}},{"title":"Sample Output 2","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e-1\n\u003c/pre\u003e"}},{"title":"Sample Input 3","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e10 10\n3 9\n3 8\n8 10\n2 10\n5 8\n6 8\n5 7\n6 7\n1 6\n2 4\n4\n1 2 7 9\n\u003c/pre\u003e"}},{"title":"Sample Output 3","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e11\n\u003c/pre\u003e \n\u003cp\u003eFor example, by arranging the gems in the order \u003cvar\u003e\\([1, 6, 7, 5, 8, 3, 9, 3, 8, 10, 2]\\)\u003c/var\u003e, we can form a sequence of length \u003cvar\u003e\\(11\\)\u003c/var\u003e with Gems \u003cvar\u003e\\(1, 2, 7, 9\\)\u003c/var\u003e.\u003c/p\u003e"}}]}