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: Wrong Answer)

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 1e-8;

long long int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> M;
	vector<vector<int>>edge(N, vector<int>(N));
	vector<int>z_connected;
	vector<int>z_noconnected;
	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		L--;
		R--;
		edge[L][R] = 1;
		edge[R][L] = 1;
	}
	z_connected.push_back(0);
	for (int i = 1; i < N; i++) {
		if (edge[0][i]) {
			z_connected.push_back(i);
		}
		else {
			z_noconnected.push_back(i);
		}
	}
	vector<int>dis_node(N);
	vector<vector<int>>dis_edge(N);
	for (int i = 0; i < z_connected.size(); i++) {
		for (int j = i + 1; j < z_connected.size(); j++) {
			if (!edge[z_connected[i]][z_connected[j]]) {
				dis_node[z_connected[i]] = 1;
				dis_node[z_connected[j]] = 1;
				dis_edge[z_connected[i]].push_back(z_connected[j]);
				dis_edge[z_connected[j]].push_back(z_connected[i]);
			}
		}
	}
	queue<int>Q;
	int st = 0;
	for (int i = 0; i < N; i++) {
		if (dis_node[i]) {
			st = i;
			break;
		}
	}
	vector<int>dis(N, MOD);
	dis[st] = 0;
	Q.push(st);
	while (!Q.empty()) {
		int cn = Q.front();
		Q.pop();
		for (auto i : dis_edge[cn]) {
			if (dis[i] > dis[cn] + 1) {
				dis[i] = dis[cn] + 1;
				Q.push(i);
			}
		}
	}
	bool imp = false;
	for (int i = 0; i < N; i++) {
		for (auto j : dis_edge[i]) {
			if (dis[i] % 2 == dis[j] % 2) {
				imp = true;
			}
		}
	}
	if (imp) {
		cout << -1 << endl;
		return 0;
	}
	long long int ans = MOD;
	for (int i = 0; i < 2; i++) {
		auto box = z_noconnected;
		for (int j = 0; j < N; j++) {
			if (dis_node[j] && dis[j] % 2 == i) {
				box.push_back(j);
			}
		}
		bool can = true;
		for (int j = 0; j < box.size(); j++) {
		//	cout << box[j] << endl;
			for (int k = j+1; k < box.size(); k++) {
				if (!edge[box[j]][box[k]]) {
					can = false;
				}
			}
		}
		if (can) {
			ans = min(ans, (long long int)box.size()*(long long int)(box.size() - 1) / 2 + (long long int)(N - box.size())*(long long int)(N - box.size() - 1) / 2);
			if (N - (long long int)box.size() > (long long int)box.size()) {
				K = N - N / 2;
				ans = min(ans, N / 2 * (N / 2 - 1) / 2 + K * (K - 1) / 2);
			}
		}
	}
	
	if (ans == MOD) {
		cout << -1 << endl;
	}
	else {
		cout << ans << endl;
	}
	return 0;
}