#include <bits/stdc++.h>
#define int long long
#define rint register int
using namespace std;
 
template<typename tp> inline void read(tp &x) {
  x = 0; char c = getchar(); int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
  if (f) x = -x;
}
#define pb push_back
const int N = 3e5 + 233;
int top = 0, st[N];
int S = -1, T = -1;
int dep[N], fa[N], vis[N], dp[N], tag[N], has[N];
int n, m;
vector <int> g[N];

inline void fcr(int u, int fat) {
  dep[u] = dep[fa[u] = fat] + 1;
  vis[u] = true; dp[u] = has[u];
  for (auto v : g[u])
    if (v != fat) {
      if (vis[v]) {
        if (S == v && T == u)
          continue;
        assert(S == -1);
        S = u; T = v;
      } else {
        fcr(v, u);
        dp[u] += -dp[v];            
      }
    }
}

inline int getDist(int u, int v) {
  int d = 0;
  while (u != v) {
    if (dep[u] < dep[v]) swap(u, v);
    ++ d; u = fa[u];
  }
  return d;
}

inline void dfs(int u, int fat) {
  dp[u] = has[u]; vis[u] = true;
  for (auto v : g[u])
    if (!vis[v]) dfs(v, u), dp[u] += -dp[v], tag[u] += tag[v];
}

main(void) {
  read(n); read(m);
  for (int i = 1; i <= n; i ++)
    has[i] = 1;
  for (int i = 1; i <= m; i ++) {
    int x, y; read(x); read(y);
    g[x].pb(y); g[y].pb(x);
  }
  fcr(1, 0);
  int sum = dp[1];
  if (S == -1) {
    if (sum)
      return puts("-1"), 0;
    int ans = 0;
    for (int i = 1; i <= n; i ++)
      ans += abs(dp[i]);
    cout << ans << "\n";
    return 0;
  }
  int d = getDist(S, T) + 1;
  if (d & 1) {
    if (sum & 1)
      return puts("-1"), 0;
    int ans = abs(sum) / 2;
    has[S] -= sum / 2; has[T] -= sum / 2;
    memset(vis, 0, sizeof vis);
    dfs(1, 0);
    for (int i = 1; i <= n; i ++)
      ans += abs(dp[i]);
    cout << ans << "\n";
  } else {
    if (sum)
      return puts("-1"), 0;
    tag[S] = -1; tag[T] = 1;
    memset(vis, 0, sizeof vis);
    dfs(1, 0);
    int ans = 0;
    st[++ top] = 0;
    for (int i = 1; i <= n; i ++)
      if (tag[i]) st[++ top] = dp[i];
      else ans += abs(dp[i]);
    sort(st + 1, st + top + 1);
    int x = st[(top + 1) / 2];
    for (int i = 1; i <= top; i ++)
      ans += abs(st[i] - x);
    cout << ans << "\n";
  }
}
