#include "bits/stdc++.h"

using namespace std;

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl


typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);i++)
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(ll)(n);i>0;i--)
#define REP(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(v) v.begin(),v.end()
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
const ll inf = 1000000001;
const ll INF = 2e18;
const ll MOD = 1000000007;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);

#define N 720
vii G(N, vi()); //グラフの隣接リスト表現
vi match(N); //マッチングのペア
vector<bool> used(N); //DFSですでに調べたかのフラグ
int n, m;
vi color(N);

//二部グラフかどうかを判断
bool bipartite(int s, int col) {
	stack<int> st;
	color[s] = col;
	st.push(s);
	int i, j;
	while (!st.empty()) {
		i = st.top();
		st.pop();
		for (j = 0; j < G[i].size(); j++) {
			if (color[G[i][j]] == color[i]) {
				//cout << "NO" << endl;
				return false;
			}
			else {
				if (color[G[i][j]] == 0) {
					color[G[i][j]] = -color[i];
					st.push(G[i][j]);
				}
			}
		}
	}
	//cout << "YES" << endl;
	return true;
}

int main() {
	fio();
	int n, m;
	cin >> n >> m;
	vi a(m), b(m);
	vector<vector<bool> > hyou(n, vector<bool>(n, false));
	rep(i, n) {
		hyou[i][i] = true;
	}
	rep(i, m) {
		cin >> a[i] >> b[i];
		a[i]--; b[i]--;
		hyou[a[i]][b[i]] = hyou[b[i]][a[i]] = true;
	}
	rep(i, n) {
		rep(j, n) {
			if (!hyou[i][j]) {
				hyou[i][j] = hyou[j][i] = true;
				G[i].push_back(j);
				G[j].push_back(i);
			}
		}
	}
	int col = 0;
	rep(i, n) {
		if (color[i] != 0) {
			continue;
		}
		if (!bipartite(i, ++col)) {
			cout << -1 << endl;
			return 0;
		}
	}
	//DEBUG_VEC(color);
	vector<pll> p(col, pll(0, 0));
	//DEBUG(col);
	rep(i, n) {
		if (color[i] < 0) {
			p[-color[i] - 1].second++;
		}
		else {
			p[color[i] - 1].first++;
		}
	}
	//DEBUG_VEC(p);
	vector<vector<bool> > dp(col + 1, vector<bool>(n + 1, false));
	dp[0][0] = true;
	rep(i, col) {
		rep(j, n + 1) {
			if (dp[i][j]) {
				if (j + p[i].first <= n) {
					dp[i + 1][j + p[i].first] = true;
				}
				if (j + p[i].second <= n) {
					dp[i + 1][j + p[i].second] = true;
				}
			}
		}
	}
	ll ans = INF;
	rep(i, n + 1) {
		if (dp[col][i]) {
			ans = min(ans, (ll)i*(i - 1) / 2 + (ll)(n - i)*(n - i - 1) / 2);
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3200