#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w))
#define dump(...)
const signed INF_=1001001001; const long long INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct before_main_function {
    before_main_function() {
        cin.tie(0); ios::sync_with_stdio(false);
        cout << setprecision(15) << fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

bool dp[2][800][800];
signed main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> g(N);
    set<pii> E;
    REP(i, M) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        E.insert({a, b});
        E.insert({b, a});
    }
    REP(i, N) {
        REP(j, N) {
            if (i == j) continue;
            if (!CONTAIN(E, pii(i, j))) {
                g[i].push_back(j);
            }
        }
    }

    dp[0][0][0] = true;
    int sign = 0;

    vector<int> col(N, -1);
    bool ng = false;
    REP(i, N) {
        if (col[i] != -1) continue;

        int one = 0;
        int comp = 0;
        function<void(int, int)> dfs = [&](int v, int c) {
            comp++;
            col[v] = c;
            if (c == 0) one++;
            FORE(w, g[v]) {
                if (col[w] != -1 && col[w] != (c ^ 1)) {
                    ng = true;
                    return;
                }
                if (col[w] != -1) continue;
                dfs(w, c ^ 1);
            }
        };

        dfs(i, 0);
        if (ng) {
            cout << -1 << endl;
            return 0;
        }

        int other = comp - one;
        REP(x, N + 1) {
            REP(y, N + 1) {
                if (dp[sign][x][y]) {
                    dp[sign ^ 1][x + one][y + other] = true;
                    dp[sign ^ 1][x + other][y + one] = true;
                    dp[sign][x][y] = false;
                }
            }
        }
        sign ^= 1;
    }

    int ans = INF;
    REP(x, N + 1) {
        REP(y, N + 1) {
            if (!dp[sign][x][y]) continue;
            chmin(ans, x * (x - 1) / 2 + y * (y - 1) / 2);
        }
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 31872