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];
long long black, white, type, cnt;
int s, t;
long long ans;

bool in_s[101010], in_t[101010];
vector<long long> flow;

void check(int v, int prev, int d) {
        color[v] = d;
        if (d & 1) black ++;
        else white ++;
        for (auto u : g[v]) if (u != prev) { 
                if (color[u] == -1) {
                        check(u, v, 1 - d);
                } else {
                        s = v;
                        t = u;
                        if (color[u] == 1 - d) { 
                                type = 1;
                        } else { 
                                type = 2;
                        }
                }
        }
}

pair<long long, long long> dfs(int v, int prev, int d) {
        long long b = 0, w = 0;
        for (auto u : g[v]) if (u != prev) {
                pair<long long, long long> get = dfs(u, v, d + 1);
                b += get.first;
                w += get.second;
        }
        if (d & 1) b ++;
        else w ++;
        if (type == 2 && (v == s || v == t)) b -= cnt;
        ans += abs(b - w);
        return make_pair(b, w);
}

pair<long long, long long> dfs2(int v, int prev, int d) {
        long long b = 0, w = 0;
        in_s[v] = false, in_t[v] = false;
        if (v == s) in_s[v] = true;
        if (v == t) in_t[v] = true;
        for (auto u : g[v]) if (u != prev) {
                pair<long long, long long> get = dfs2(u, v, d + 1);
                in_s[v] |= in_s[u];
                in_t[v] |= in_t[u];
                b += get.first;
                w += get.second;
        }
        if (d & 1) b ++;
        else w ++;
        if ((in_s[v] && in_t[v]) || (!in_s[v] && !in_t[v])) {
                ans += abs(b - w);
        } else {
                flow.push_back(b - w);
        }
        return make_pair(b, w);
}

signed main() {
        long long n, m;
        scanf("%lld%lld", &n, &m);
        for (int i = 0; i < m; i ++) {
                long long 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 && black != white) ||
            (type == 1 && black != white) ||
            (type == 2 && black % 2 != white % 2)) {
                puts("-1");
                return 0;
        }
        if (type == 0) dfs(0, -1, 0);
        g[s].erase(remove(g[s].begin(), g[s].end(), t), g[s].end());
        g[t].erase(remove(g[t].begin(), g[t].end(), s), g[t].end());
        if (type == 1) {
                dfs2(0, -1, 0);
                long long lb = -101010, ub = 101010;
                while (ub - lb > 3) {
                        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;
                }
                long long res = 0x3f3f3f3f;
                for (long long i = lb - 10; i < lb + 10; i ++) {
                        long long sum = 0;
                        for (auto f : flow) sum += abs(i + f);
                        sum += abs(i); //self
                        res = min(res, sum);
                }
                ans += res;
        }
        if (type == 2) {
                cnt = (black - white) / 2;
                dfs(0, -1, 0);
                ans += abs(cnt);
        }
        printf("%lld\n", ans);
        return 0;
}