Description
Do you know a story about the three musketeers? Anyway, you will learn about its origins now.
Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.
There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.
Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.
Input
The first line contains two space-separated integers, n and m (3 ≤ n ≤ 4000, 0 ≤ m ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other.
i-th of the following m lines contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.
Output
If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).
Sample Input
5 6
1 2
1 3
2 3
2 4
3 4
4 5
2
7 4
2 1
3 6
5 1
1 7
-1
#include<iostream> #include<stdio.h> using namespace std; const int maxn = 4005; struct Node{ int a,b; }edg[maxn]; int mapp[maxn][maxn]; int deg[maxn]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) mapp[i][j]=0; for(int i=0;i<=n;i++) deg[i]=0; for(int i=0;i<m;i++){ int tmp1,tmp2; scanf("%d%d",&tmp1,&tmp2); edg[i].a=tmp1; edg[i].b=tmp2; mapp[tmp1][tmp2]=mapp[tmp2][tmp1]=1; deg[tmp1]++; deg[tmp2]++; } int inf=0x7fffffff; int ans=inf; for(int i=0;i<m;i++){ for(int j=1;j<=n;j++){ if(mapp[j][edg[i].a]&&mapp[j][edg[i].b]){ ans=min(ans,(deg[j]+deg[edg[i].a]+deg[edg[i].b])); } } } if(ans<inf){ printf("%d ",ans-6); }else{ printf("-1 "); } return 0; }