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>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 +c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
struct node {
    int to,next;
}E[MAXN * 2];
int N,head[MAXN],sumE,M,fa[MAXN],dep[MAXN],st,ed;
int64 dp[MAXN][2],ans,pre[MAXN],suf[MAXN],prez[MAXN][2],sufz[MAXN][2];
bool vis[MAXN];
int c[MAXN],tot;
int sta[MAXN],top,num[MAXN];
void add(int u,int v) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    head[u] = sumE;
}
void dfs(int u) {
    dp[u][0] = 1;
    for(int i = head[u] ; i ; i = E[i].next) {
	int v = E[i].to;
	if(v != fa[u]) {
	    fa[v] = u;
	    dfs(v);
	    ans += dp[v][0] + dp[v][1];
	    dp[u][0] += dp[v][1];
	    dp[u][1] += dp[v][0];
	}
    }
    int t = min(dp[u][0],dp[u][1]);
    dp[u][0] -= t;dp[u][1] -= t;
}
void find_circle(int u) {
    dep[u] = dep[fa[u]] + 1;
    vis[u] = 1;
    for(int i = head[u] ; i ; i = E[i].next) {
	int v = E[i].to;
	if(!vis[v]) {
	    fa[v] = u;
	    find_circle(v);
	}
	else if(dep[v] < dep[u]) {
	    st = u;ed = v;
	}
    }
}
void Calc(int u) {
    dp[u][0] = 1;
    vis[u] = 1;
    for(int i = head[u] ; i ; i = E[i].next) {
	int v = E[i].to;
	if(!vis[v]) {
	    Calc(v);
	    ans += dp[v][0] + dp[v][1];
	    dp[u][0] += dp[v][1];
	    dp[u][1] += dp[v][0];
	}
    }
    int t = min(dp[u][0],dp[u][1]);
    dp[u][0] -= t;
    dp[u][1] -= t;
}
void Solve() {
    read(N);read(M);
    int a,b;
    for(int i = 1 ; i <= M ; ++i) {
	read(a);read(b);
	add(a,b);add(b,a);
    }
    if(N & 1) {puts("-1");return;}
    if(M == N - 1) {
	dfs(1);
	if(dp[1][0] || dp[1][1]) {puts("-1");return;}
	out(ans);enter;
    }
    else {
	find_circle(1);
	int p = st;
	while(1) {
	    c[++tot] = p;
	    if(p == ed) break;
	    p = fa[p];
	}
	memset(vis,0,sizeof(vis));
	for(int i = 1 ; i <= tot ; ++i) vis[c[i]] = 1;
	for(int i = 1 ; i <= tot ; ++i) {
	    Calc(c[i]);
	}
	top = 0;
	for(int i = 1 ; i <= tot ; ++i) {
	    int u = c[i];
	    pre[i] = pre[i - 1];
	    if(dp[u][0] || dp[u][1]) {
		pre[i] += (i - 1) * (dp[u][0]+dp[u][1]);
	    }
	    for(int k = 0 ; k <= 1 ; ++k) {
		int t = k ^ ((i - 1) & 1);
		int cnt = dp[u][k];
		while(cnt && top && num[top] == t ^ 1) {
		    pre[i] -= sta[top] * 2;
		    --top;--cnt;
		}
		if(cnt) {
		    for(int j = 1 ; j <= cnt ; ++j) {
			sta[++top] = i - 1;num[top] = t;
		    }
		}
	    }
	    if(top) prez[i][num[top]] = top; 
	}
	top = 0;
	for(int i = tot ; i >= 1 ; --i) {
	    int u = c[i];
	    suf[i] = suf[i + 1];
	    if(dp[u][0] || dp[u][1]) {
		suf[i] += (tot - i) * (dp[u][0] + dp[u][1]);
	    }
	    for(int k = 0 ; k <= 1 ; ++k) {
		int t = k ^ ((tot - i) & 1);
		int cnt = dp[u][k];
		while(cnt && top && num[top] == t ^ 1) {
		    suf[i] -= sta[top] * 2;
		    --top;--cnt;
		}
		if(cnt) {
		    for(int j = 1 ; j <= cnt ; ++j) {sta[++top] = tot - i;num[top] = t;}
		}
	    }
	    if(top) sufz[i][num[top]] = top;
	}
	int64 res = 1e18;
	if(prez[tot][0] + prez[tot][1] == 0) res = pre[tot];
	for(int i = 1 ; i < tot ; ++i) {
	    if(prez[i][0] == sufz[i + 1][0] && prez[i][1] == sufz[i + 1][1]) {
		res = min(res,pre[i] + suf[i + 1] + prez[i][0] + prez[i][1]);
	    }
	}
	if(res >= 1e18) {puts("-1");return;}
	out(ans + res);enter;
    }
}

int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}