Problem:
Score : 700 points

Problem Statement

In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1, 2, ..., N.
M bidirectional roads connect these cities.
The i-th road connects City A_i and City B_i.
Every road connects two distinct cities.
Also, for any two cities, there is at most one road that directly connects them.

One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi.
After the division, each city in Takahashi would belong to either Taka or Hashi.
It is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi.
Here, the following condition should be satisfied:

Any two cities in the same state, Taka or Hashi, are directly connected by a road.

Find the minimum possible number of roads whose endpoint cities belong to the same state.
If it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print -1.

Constraints

2 \leq N \leq 700

0 \leq M \leq N(N-1)/2

1 \leq A_i \leq N

1 \leq B_i \leq N

A_i \neq B_i

If i \neq j, at least one of the following holds: A_i \neq A_j and B_i \neq B_j.

If i \neq j, at least one of the following holds: A_i \neq B_j and B_i \neq A_j.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the answer.

Sample Input 1

5 5
1 2
1 3
3 4
3 5
4 5

Sample Output 1

4

For example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong to Hashi, the condition is satisfied.
Here, the number of roads whose endpoint cities belong to the same state, is 4.

Sample Input 2

5 1
1 2

Sample Output 2

-1

In this sample, the condition cannot be satisfied regardless of which cities belong to each state.

Sample Input 3

4 3
1 2
1 3
2 3

Sample Output 3

3

Sample Input 4

10 39
7 2
7 1
5 6
5 8
9 10
2 8
8 7
3 10
10 1
8 10
2 3
7 4
3 9
4 10
3 4
6 1
6 7
9 5
9 7
6 9
9 4
4 6
7 5
8 3
2 5
9 2
10 7
8 6
8 9
7 3
5 3
4 5
6 3
2 10
5 10
4 2
6 2
8 4
10 6

Sample Output 4

21

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
 
using namespace std;
 
#define cint(d) scanf("%d", &d)
#define cint2(a, b) scanf("%d %d", &a, &b)
#define cint3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define cint4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
 
#define clong(d) scanf("%lld", &d)
#define clong2(a, b) scanf("%lld %lld", &a, &b)
#define clong3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define clong4(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
 
#define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin();  v != (c).rend(); ++v)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
 
typedef long long int slong;
typedef pair<int, int> pii;
typedef pair<slong, slong> pll;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
 
#ifdef VSP4 
    #include "debug.h"    
#else
    #define debug(args...)                  // Just strip off all debug tokens
#endif
 
const int MOD = 1000000007;
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD;
#define MODADDSET(d) if ((d) >= MOD) d -= MOD;
#define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD;

const int MAXN = 700;
const int SQRTN = 550;
const int LOGN = 18;
 
const int INT_INFINITY = 1001001001;
const long long int LONG_INFINITY = 1001001001001001001ll;
const long long int LARGEVALUE = 10010010010010ll;

int n, m;
vector<int> graph[MAXN+5];

bool hasedge[MAXN+5][MAXN+5];

int componentID;
vector<int> components[MAXN+5];

bool visited[MAXN+5];
void dfs(int u)
{
	visited[u] = true;
	components[componentID].pb(u);

	for (auto v: graph[u])
	{
		if (!visited[v])
		{
			dfs(v);
		}
	}
}

vector<pii> data;

void find(vector<int> ids)
{
	int n = ids.size();
	int m = 0;

	for (auto u: ids)
	{
		for (auto v: graph[u])
		{
			if (u > v)
			{
				m++;
			}
		}
	}

	if (m == ((n*(n-1))/2))
	{
		//fully connected
		data.eb(n, 0);
	}
	else
	{
		int x = -1;
		int y = -1;

		for (auto u: ids)
		{
			for (auto v: ids)
			{
				if (!hasedge[u][v])
				{
					x = u;
					y = v;
					break;
				}
			}
		}

		assert(x != -1);

		vector<int> a;
		vector<int> b;
		a.pb(x);
		b.pb(y);

		vector<int> both;
		for (auto u: ids)
		{
			if (u != x && u != y)
			{
				both.pb(u);
			}
		}

		while (true)
		{
			bool done = false;

			for (auto u: both)
			{
				//check if this can't be added to a
				bool canta = false;
				bool cantb = false;

				{
					for (auto p: a)
					{
						if (!hasedge[p][u])
						{
							canta = true;
							break;
						}
					}
				}


				{
					for (auto p: b)
					{
						if (!hasedge[p][u])
						{
							cantb = true;
							break;
						}
					}

				}

				if (canta && cantb)
				{
					cout << "-1";
					exit(0);
				}
				else if (canta)
				{
					//move u to b
					done = true;
					both.erase(find(ALL(both), u));
					b.pb(u);
					break;
				}
				else if (cantb)
				{
					//move u to a
					done = true;
					both.erase(find(ALL(both), u));
					a.pb(u);
					break;
				}
			}

			if (!done)
			{
				break;
			}
		}

		data.eb(a.size(), b.size());

		if (!both.empty())
		{
			find(both);
		}
	}
}

int main()
{
	clock_t begin = clock();

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int t, i, j;
 	
 	cin >> n >> m;
 	for (int i = 1; i <= m; i++)
 	{
 		int u, v;
 		cin >> u >> v;
 		graph[u].pb(v);
 		graph[v].pb(u);

 		hasedge[u][v] = true;
 		hasedge[v][u] = true;
 	}

 	for (int i = 1; i <= n; i++)
 	{
 		if (!visited[i])
 		{
 			++componentID;
 			dfs(i);
 		}
 	}

 	if (componentID >= 3)
 	{
 		cout << "-1\n";
 	}
 	else if (componentID == 2)
 	{
 		int ans = 0;

 		int ap = (components[1].size()*(components[1].size() - 1))/2;
 		int bp = (components[2].size()*(components[2].size() - 1))/2;

 		int tot = 0;

 		for (auto u: components[1])
 		{
 			for (auto v: graph[u])
 			{
 				if (u > v)
 				{
 					tot++;
 				}
 			}
 		}

 		if (ap != tot)
 		{
 			cout << "-1\n";
 			return 0;
 		}

 		tot = 0;

 		for (auto u: components[2])
 		{
 			for (auto v: graph[u])
 			{
 				if (u > v)
 				{
 					tot++;
 				}
 			}
 		}

 		if (tot == bp)
 		{
 			cout << (ap+bp) << "\n";
 		}
 		else
 		{
 			cout << -1 << "\n";
 		}
 	}
 	else
 	{
 		vector<int> curr;
 		for (int i = 1; i <= n; i++)
 		{
 			curr.pb(i);
 		}

 		find(curr);

 		vector<bool> dp(n+1, false);
 		dp[0] = true;

 		for (auto p: data)
 		{
 			vector<bool> newdp(n+1, false);

 			if (p.se == 0)
 			{
 				for (int asz = 0; asz <= p.fi; asz++)
 				{
		 			for (int x = 0; x+asz <= n; x++)
		 			{
		 				if (dp[x])
		 				{
		 					newdp[x+asz] = true;
		 				}
		 			}
 				}
 			}
 			else
 			{
	 			for (int x = 0; x <= n; x++)
	 			{
	 				if (dp[x])
	 				{
	 					//x+p.fi
	 					//x+p.se
	 					newdp[x+p.fi] = true;
	 					newdp[x+p.se] = true;
	 				}
	 			}
 			}

 			dp = newdp;
 		}

 		int ans = MOD;

 		for (int u = 0; u <= n; u++)
 		{
 			int a = u;
 			int b = n - u;

 			if (dp[u])
 			{
 				ans = min(ans, ((a*(a-1))/2) + ((b*(b-1))/2));
 			}
 		}

 		if (ans == MOD)
 		{
 			ans = -1;
 		}

 		assert(false);

 		cout << ans;
 	}


	clock_t end = clock();
	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
	
	cerr << elapsed_secs << endl;
	
	return 0;
}