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: Accepted)

#include "bits/stdc++.h"
using namespace std;

#define int long long

vector<int> g[101010];
int color[101010];
int black_all, white_all, type, start, goal;
long long ans;

bool in_start[101010], in_goal[101010];
vector<int> flow;

int cnt_k;

void check(int v, int prev, int d) {
        color[v] = d;
        if (d & 1) black_all ++;
        else white_all ++;
        for (auto u : g[v]) if (u != prev) { 
                if (color[u] == -1) {
                        check(u, v, 1 - d);
                } else {
                        start = v;
                        goal = u;
                        if (color[u] == 1 - d) { 
                                type = 1;
                        } else { 
                                type = 2;
                        }
                }
        }
}

pair<int, int> dfs(int v, int prev, int d) {
        int black = 0, white = 0;
        for (auto u : g[v]) if (u != prev) {
                pair<int, int> get = dfs(u, v, d + 1);
                black += get.first;
                white += get.second;
        }
        if (d & 1) black ++;
        else white ++;
        ans += abs(black - white);
        return make_pair(black, white);
}

pair<int, int> dfs2(int v, int prev, int d) {
        int black = 0, white = 0;
        in_start[v] = false, in_goal[v] = false;
        if (v == start) in_start[v] = true;
        if (v == goal) in_goal[v] = true;
        for (auto u : g[v]) if (u != prev) {
                pair<int, int> get = dfs2(u, v, d + 1);
                in_start[v] |= in_start[u];
                in_goal[v] |= in_goal[u];
                black += get.first;
                white += get.second;
        }
        if (d & 1) black ++;
        else white ++;
        if ((in_start[v] && in_goal[v]) || (!in_start[v] && !in_goal[v])) {
                ans += abs(black - white);
        } else {
                flow.push_back(black - white);
        }
        return make_pair(black, white);
}

pair<int, int> dfs3(int v, int prev, int d) {
        int black = 0, white = 0;
        for (auto u : g[v]) if (u != prev) {
                pair<int, int> get = dfs3(u, v, d + 1);
                black += get.first;
                white += get.second;
        }
        if (d & 1) black ++;
        else white ++;
        if (v == start || v == goal) black -= cnt_k;
        ans += abs(black - white);
        return make_pair(black, white);
}

signed main() {
        int n, m;
        scanf("%lld%lld", &n, &m);
        for (int i = 0; i < m; i ++) {
                int a, b;
                scanf("%lld%lld", &a, &b);
                a --, b --;
                g[a].push_back(b);
                g[b].push_back(a);
        }
        type = 0;
        memset(color, -1, sizeof color);
        check(0, -1, 0);
        if (type == 0 || type == 1) {
                if (black_all != white_all) {
                        puts("-1");
                        return 0;
                }
        }
        if (type == 2) {
                if (black_all % 2 != white_all % 2) {
                        puts("-1");
                        return 0;
                }
        }
        if (type == 0) {
                dfs(0, -1, 0);
                printf("%lld\n", ans);
                return 0;
        }
        for (int i = 0; i < g[start].size(); i ++) {
                if (g[start][i] == goal) {
                        g[start].erase(g[start].begin() + i);
                }
        }
        for (int i = 0; i < g[goal].size(); i ++) {
                if (g[goal][i] == start) {
                        g[goal].erase(g[goal].begin() + i);
                }
        }
        if (type == 1) {
                for (int i = 0; i < g[start].size(); i ++) {
                        if (g[start][i] == goal) {
                                g[start].erase(g[start].begin() + i);
                        }
                }
                for (int i = 0; i < g[goal].size(); i ++) {
                        if (g[goal][i] == start) {
                                g[goal].erase(g[goal].begin() + i);
                        }
                }
                dfs2(0, -1, 0);
                int lb = -101010, ub = 101010;
                int cnt = 0;
                while (ub - lb > 0) {
                        cnt ++;
                        if (cnt > 100) break;
                        int left = (lb * 2 + ub) / 3;
                        int right = (lb + ub * 2) / 3;
                        int left_ans = 0, right_ans = 0;
                        for (auto f : flow) {
                                left_ans += abs(left + f);
                                right_ans += abs(right + f);
                        }
                        if (left_ans < right_ans) ub = right;
                        else lb = left;
                }
                int res = 0x3f3f3f3f;
                for (int i = lb - 10; i < lb + 10; i ++) {
                        int sum = 0;
                        for (auto f : flow) {
                                sum += abs(i + f);
                        }             
                        sum += abs(i); //self
                        res = min(res, sum);
                }
                ans += res;
                printf("%lld\n", ans);
                return 0;
        }
        if (type == 2) {
                cnt_k = (black_all - white_all) / 2;
                dfs3(0, -1, 0);
                ans += abs(cnt_k);
                printf("%lld\n", ans);
        }
        return 0;
}