Problem:
Score : 2200 points

Problem Statement

You are given an undirected graph with N vertices and M edges.
Here, N-1≤M≤N holds and the graph is connected.
There are no self-loops or multiple edges in this graph.

The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects vertices a_i and b_i.

The color of each vertex can be either white or black.
Initially, all the vertices are white.
Snuke is trying to turn all the vertices black by performing the following operation some number of times:

Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.

Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.

Constraints

2≤N≤10^5

N-1≤M≤N

1≤a_i,b_i≤N

There are no self-loops or multiple edges in the given graph.

The given graph is connected.

Partial Score

In the test set worth 1500 points, M=N-1.

Input

The 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

If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective.
Otherwise, print -1 instead.

Sample Input 1

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

Sample Output 1

5

For example, it is possible to perform the operations as shown in the following diagram:

Sample Input 2

3 2
1 2
2 3

Sample Output 2

-1

It is not possible to turn all the vertices black.

Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

2

This case is not included in the test set for the partial score.

Sample Input 4

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

Sample Output 4

7

This case is not included in the test set for the partial score.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
vector<int> v[100005];
LL ans;
int dep[100005];
int val[100005];
int a,b;
int dfs(int x,int f){
    dep[x]=dep[f]+1;
    int sum=0;
    for(auto it:v[x]){
        if(it!=f){
            if((it==a&&x==b)||(it==b&&x==a))continue;
            sum+=dfs(it,x);
        }
    }
    if(dep[x]&1)sum++;
    else sum--;
    sum+=val[x];
    ans+=abs(sum);
    return sum;
}
int vis[100005];
vector<int> circle;
vector<int> stk;
int Find;
void dfsc(int x,int f){
    stk.pb(x);
    vis[x]=1;
    for(auto it:v[x]){
        if(it!=f){
            if(vis[it]){
                while(stk.back()!=it){
                    circle.pb(stk.back());
                    stk.pop_back();
                }
                circle.pb(stk.back());
                Find=1;
            }
            else{
                dfsc(it,x);
            }
            if(Find)return;
        }
    }
    if(stk.back()==x)stk.pop_back();
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i = 0;i<m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        v[x].pb(y);
        v[y].pb(x);
    }
    if(m==n){
        dfsc(1,0);
        //for(auto it:circle)
       // printf("%d ",it);
       // printf("\n");
        if(circle.size()&1){
            a=circle[0],b=circle[1];
            int x=dfs(1,0);
            if(x&1){
                printf("-1\n");
                return 0;
            }
            else{
                val[circle[0]]=-x/2;
                val[circle[1]]=-x/2;
            }
         //   printf("%d %d\n",circle[0],circle[1]);
        //    printf("%d\n",x);
            ans=0;
            dfs(1,0);
            printf("%lld\n",ans+abs(x)/2);
        }
        else{
            a=circle[0],b=circle[1];
            if(dfs(1,0)){
                printf("-1\n");
                return 0;
            }
            int Max=n,Min=-n;
            while(Max>Min+10){
                int mid1=(Max*2+Min)/3,mid2=(Max+Min*2)/3;
                val[a]=mid1,val[b]=-mid1;
                ans=0;
                dfs(1,0);
                int a1=ans+abs(mid1);
                val[a]=mid2,val[b]=-mid2;
                ans=0;
                dfs(1,0);
                int a2=ans+abs(mid2);
                if(a1>a2){
                    Max=mid1;
                }
                else{
                    Min=mid2;
                }
            }
            LL res=1e18;
            for(int i = Min;i<=Max;i++){
                val[a]=i;val[b]=-i;
                ans=0;
                dfs(1,0);
                res=min(ans+abs(i),res);
            }
            printf("%lld\n",res);
            //assert(0);
        }
        return 0;
    }
    if(dfs(1,0)){
        printf("-1\n");
    }
    else
    printf("%lld\n",ans);
}