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 <bits/stdc++.h>
#include<string.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define int  long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
 
#define vi vector<int> 
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >   
#define vpl vector< pair<ll,ll> >   
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define mod %1000000007
//#define pi 3.14159265358979323846//20 decimals
#define ld long double
#define inf 1000000000
bool less_vectors(const vector<int>& a,const vector<int>& b) 
{
  return a.size() < b.size();
}
#define N 100001
#define eps 0.0000000001
int g[701][701];
int32_t main()
{                
    IOS;
    /*#ifndef ONLINE_JUDGE
    freopen("int.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif*/
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int a,b;
        cin>>a>>b;
        g[a][b]=1;
        g[b][a]=1;
    }
    int x=0;
    int y=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(g[i][j]==0)
            {
                x=i;
                y=j;
                break;

            }
        }
        if(x!=0)
            break;
    }
   // trace2(x,y);
    if(x==0)
    {
        if(n%2==0)
        {
            cout<<m-(n/2)*(n/2);
        }
        else
        {
            cout<<m-(n/2)*(n/2+1);
        }
        return 0;
    }
    vpi v(n-2);
    int u=0;
    for(int i=1;i<=n;i++)
    {
        if(x==i || y==i)
            continue;
        int cnt=0;
        if(g[i][x])
        {
            cnt++;
        }
        if(g[i][y])
            cnt++;
        v[u]=mp(cnt,i);
        u++;
    }
    sort(v.begin(),v.end());
    vi xg;
    vi yg;
    bool ch=1;
    for(int i=0;i<u;i++)
    {
        if(v[i].ff==0)
        {
            ch=0;
            break;
        }
        if(v[i].ff==1)
        {
            if(g[x][v[i].ss])
                xg.pb(v[i].ss);
            else
                yg.pb(v[i].ss);
        }
    }
    int ans=0;
    for(int i=0;i<xg.size();i++)
    {
        if(g[xg[i]][y])
            ans++;
       // trace1(xg[i]);
        for(int j=i+1;j<xg.size();j++)
        {
            if(g[xg[i]][xg[j]])
                ;
            else
                ch=0;
        }
    }
    //trace1(ch);
    for(int i=0;i<yg.size();i++)
    {
        if(g[yg[i]][x])
            ans++;
//                trace1(yg[i]);

        for(int j=i+1;j<yg.size();j++)
        {
            if(g[yg[i]][yg[j]])
                ;
            else
                ch=0;
        }
    }
       // trace1(ch);

    for(int j=0;j<v.size();j++)
    {
        if(v[j].ff==2)
        {
            int node=v[j].ss;
            int cn1=0;
            int cn2=0;
            for(int i=0;i<xg.size();i++)
            {
                if(g[node][xg[i]])
                    cn1++;
            }
            for(int i=0;i<yg.size();i++)
            {
                if(g[node][yg[i]])
                    cn2++;
            }
           // trace5(cn1,cn2,node,xg.size(),yg.size());
            if(cn1!=xg.size() && cn2!=yg.size())
                ch=0;
            else
                if(cn1==xg.size() && cn2==yg.size())
                {
                    if(cn1>cn2)
                    {
                        ans=ans+1+cn1;
                        yg.pb(node);
                    }
                    else
                    {
                        ans=ans+1+cn2;
                        xg.pb(node);
                    }
                }
                else
                    if(cn1==xg.size())
                        {ans=1+cn2;xg.pb(node);}
                    else
                        {ans=1+cn1;yg.pb(node);}
           // trace1(ch);
            
        }
    }
    if(!ch)
        cout<<-1;
    else
        cout<<m-ans;
    return 0;
}