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)

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// }}}
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
// clang-format off
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":", ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream &o, tuple<T...> const &t){ o << "("; _ot<0>(o, t); o << ")"; return o; }
template<class T, class U> ostream & operator<<(ostream &o, pair<T, U> const &p) { o << "(" << p.first << ", " << p.second << ")"; return o; }
template < class T > ostream &operator<<(ostream &o, const stack<T> &a) { o << "{"; for(auto tmp = a; tmp.size(); tmp.pop()) o << (a.size() == tmp.size() ? "" : ", ") << tmp.top(); o << "}"; return o; }
#ifdef DEBUG
#if !defined(DEBUG_OUT)
#define DEBUG_OUT cerr
#endif
#if !defined(DEBUG_LEFT)
#define DEBUG_LEFT "\e[1;36m"
#endif
#if !defined(DEBUG_RIGHT)
#define DEBUG_RIGHT ":\e[m"
#endif
#define dump(...) [&](){auto __debug_tap=make_tuple(__VA_ARGS__);DEBUG_OUT<<DEBUG_LEFT<<__LINE__ << DEBUG_RIGHT << " " <<#__VA_ARGS__<<" = "<<__debug_tap<<endl;}()
template < class T > inline void dump2D(T &d, size_t sizey, size_t sizex) { for(size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for(size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << endl; } }
template < class T, class = typename iterator_traits< typename T::iterator >::value_type, class = typename enable_if<!is_same<T, string>::value>::type > ostream &operator<<(ostream &o, const T &a) { o << "{"; for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : ", ") << *ite; o << "}"; return o; }
#else
#define dump(...) (42)
#define dump2D(...) (42)
template < class T, class = typename iterator_traits< typename T::iterator >::value_type, class = typename enable_if<!is_same<T, string>::value>::type > ostream &operator<<(ostream &o, const T &a) { for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : " ") << *ite; return o; }
#endif
// clang-format on
// }}}


const int N = 1e5;
int n, m;
vector<vector<int>> g(N);

#include<functional>
int cycleSize() {
  vector<int> used(n);
  vector<int> dist(n);
  const function<int(int, int)> dfs = [&](int i, int p) {
    if(used[i]) {
      return dist[p] + 1;
    }
    used[i] = 1;
    for(int j : g[i]) if(j != p) {
      dist[j] = dist[i] + 1;
      int d = dfs(j, i);
      if(d != -1) return d;
    }
    return -1;
  };
  dist[0] = 0;
  return dfs(0, -1);
}

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n >> m;
  if(n == 2) {
    cout << 1 << endl;
    return 0;
  }
  if(n % 2 == 1) {
    cout << - 1 << endl;
    return 0;
  }
  vector<int> deg(n);
  for(int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b; a--; b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
    deg[a]++;
    deg[b]++;
  }
  ll ans = 0;
  vector<vector<int>> w(2, vector<int>(n));
  vector<int> used(n);
  queue<int> q;
  for(int i = 0; i < n; i++) {
    w[0][i]++;
    if(g[i].size() == 1) { // is leaf
      q.emplace(i);
    }
  }
  while(q.size()) {
    int i = q.front(); q.pop();
    if(deg[i] == 0) {
      // n == m - 1
      if(w[0][i] != w[1][i]) {
        cout << -1 << endl;
        return 0;
      }
      break;
    }
    used[i] = 1;
    int m = min(w[0][i], w[1][i]);
    w[0][i] -= m;
    w[1][i] -= m;
    ans += w[0][i] | w[1][i];
    for(int j : g[i]) if(!used[j]) {
      deg[j]--;
      for(int c = 0; c < 2; c++) w[1-c][j] += w[c][i];
      if(deg[j] == 1) {
        q.emplace(j);
      }
      break;
    }
  }
  dump(ans);
  if(n == m) {
    int circ = cycleSize();
    dump(circ);
    int start;
    for(int i = 0; i < n; i++) if(deg[i] == 2) start = i;
    int last;
    for(int j : g[start]) if(deg[j] == 2) last = j;
    int now = start;
    vector<queue<pair<int, int>>> tap(2); // x-cord, id
    vector<int> noe(circ * 4); // x-cord to size
    vector<int> ids(circ * 4);
    vector<int> type(circ * 4);
    int x = -1;
    while(1) {
      x++;
      // dump(now);
      int m = min(w[0][now], w[1][now]);
      w[0][now] -= m;
      w[1][now] -= m;
      
      int t = !w[0][now];
      for(int i = 0; i < 4; i++) {
        type[x + circ * i] = t ^ (x&1) ^ ((circ*i)&1);
        noe[x + circ * i] = w[t][now];
      }

      // next
      for(int j : g[now]) if(deg[j] == 2 && j != last) {
        last = now;
        now = j;
        break;
      }
      if(now == start) break;
    }
    // assert(x == circ - 1);

    for(int i = 0; i < circ * 4 - 1; i++) {
      ids[i + 1] = ids[i] + noe[i];
    }

    int num = ids.back() + noe.back();
    dump(num);
    // dump(type);
    vector<int> next(num, -1); // id to id
    vector<int> prev(num, -1); // id to id

    vector<int> idToX(num);
    vector<int> idToType(num);
    int cnt = 0;
    for(int i = 0; i < circ * 4; i++) {
      for(int j = 0; j < noe[i]; j++) {
        tap[type[i]].emplace(i, ids[i] + j);
        idToX[ids[i] + j] = i;
        idToType[ids[i] + j] = type[i];
        if(type[i]) cnt++;
      }
    }
    
    if(num != cnt * 2) {
      cout << -1 << endl;
      return 0;
    }

    for(int i = 0; i < circ * 4; i++) {
      auto &eca = tap[1-type[i]];
      while(eca.size() && eca.front().first < i) eca.pop();
      for(int j = 0; j < noe[i]; j++) {
        int id0 = ids[i] + j;
        if(eca.size() && prev[id0] == -1) {
          int id1 = eca.front().second;
          // dump(id0, "->", id1);
          next[id0] = id1;
          prev[id1] = id0;
          eca.pop();
        }
      }
    }

    for(int i = 0; i < 2; i++) while(tap[i].size()) tap[i].pop();
    for(int i = 0; i < circ * 4; i++) {
      for(int j = 0; j < noe[i]; j++) {
        if(prev[ids[i] + j] == -1) tap[type[i]].emplace(i, ids[i] + j);
      }
    }

    for(int i = 0; i < circ * 4; i++) {
      auto &eca = tap[1-type[i]];
      while(eca.size() && eca.front().first < i) eca.pop();
      for(int j = 0; j < noe[i]; j++) {
        int id0 = ids[i] + j;
        if(eca.size() && next[id0] == -1) {
          int id1 = eca.front().second;
          // dump(id0, "->", id1);
          next[id0] = id1;
          prev[id1] = id0;
          eca.pop();
        }
      }
    }
    
    vector<vector<int>> xv; // [leader id][]
    vector<vector<vector<int>>> sum(2); // [type][leader id][]
    vector<int> lid;
    vector<int> leader(num);
    { // {{{
      int k = -1;
      for(int i = 0; i < num; i++) {
        // dump(i, prev[i]);
        if(prev[i] == -1) { // is leader
          k++;
          lid.emplace_back(i);
          xv.push_back({});
          sum[0].push_back({});
          sum[1].push_back({});
          for(int now = i; now != -1; now = next[now]) {
            leader[now] = k;
            xv.back().emplace_back(idToX[now]);
            sum[0].back().emplace_back(0);
            sum[1].back().emplace_back(0);
          }
        }
      }
      // assert(k + 1 == lid.size());
    } // }}}
    dump(lid.size());
    
    for(int i = 0; i < 2; i++) {
      for(int j = 0; j < sum[i].size(); j++) {
        for(int l = 0; l < sum[i][j].size(); l++) {
          if(l + 1 < sum[i][j].size()) {
            sum[i][j][l] = type[xv[j][l]] == i ? xv[j][l + 1] - xv[j][l] : 0;
          }
          if(l) sum[i][j][l] += sum[i][j][l-1];
        }
      }
    }

    vector<int> qina(lid.size());

    vector<int> isInvalid(lid.size());
    int invalid = 0;

    auto get = [&](int i, int s) {
      // i : leader id
      // [s, s + circ * 2)
      isInvalid[i] = 0;
      int l = lower_bound((xv[i]).begin(), (xv[i]).end(), s) - begin(xv[i]);
      int r = lower_bound((xv[i]).begin(), (xv[i]).end(), s + circ * 2) - begin(xv[i]);
      // assert(l < r);
      // assert(l != xv[i].size());
      // assert(r >= 1);
      isInvalid[i] = type[xv[i][l]] ^ type[xv[i][r-1]] ^ 1;
      // l < r
      int t = type[xv[i][l]];
      int res = 0;
      if(r-2 >= 0) res = sum[t][i][r-2];
      if(l-1 >= 0) res -= sum[t][i][l-1];
      return res;
    };

    ll res = 0;
    for(int i = 0; i < lid.size(); i++) {
      int id = lid[i];
      qina[i] = get(i, 0);
      // dump(i, qina[i]);
      res += qina[i];
      invalid += isInvalid[i];
      // dump(idToX[id]);
    }
    // assert(invalid == 0);
    ll eca = 1e9;
    if(invalid == 0) eca = res;

    for(int i = 0; i < circ * 2; i++) {
      // i を消して i + circ * 2 を足す
      // remove
      for(int j = 0; j < noe[i]; j++) {
        int id = ids[i] + j;
        res -= qina[leader[id]];
        invalid -= isInvalid[leader[id]];
        qina[leader[id]] = get(leader[id], i + 1);
        res += qina[leader[id]];
        invalid += isInvalid[leader[id]];
      }
      // dump(invalid, res);
      // add
      for(int j = 0; j < noe[i + circ * 2]; j++) {
        int id = ids[i + circ * 2] + j;
        res -= qina[leader[id]];
        invalid -= isInvalid[leader[id]];
        qina[leader[id]] = get(leader[id], i + 1);
        res += qina[leader[id]];
        invalid += isInvalid[leader[id]];
      }
      if(invalid == 0) eca = min(eca, res);
    }

    ans += eca / 2;
  }
  cout << ans << endl;
  return 0;
}