#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  int data = 0, m = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') {
    m = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    data = (data << 1) + (data << 3) + (ch ^ 48);
    ch = getchar();
  }
  return (m) ? data : -data;
}
int f[100005], q[100005], q2[100005], K[100005], fa[100005], B[100005], tot,
    tot2, n, m, dsu[100005], dep[100005], U, V, sum;
int Ans;
bool cov[100005];
vector<int> edge[100005];
inline int Find(int x) { return x ^ dsu[x] ? dsu[x] = Find(dsu[x]) : x; }
namespace cpp1 {
void dfs(int u, int From) {
  for (auto v : edge[u]) {
    if (v != From) dfs(v, u), f[u] -= f[v];
  }
  f[u]++;
  Ans += abs(f[u]);
}
inline void main() {
  dfs(1, 0);
  if (f[1] != 0)
    puts("-1");
  else
    cout << Ans << endl;
}
}  // namespace cpp1
namespace cpp2 {
void dfs1(int u, int From) {
  for (auto v : edge[u])
    if (v != From) dep[v] = dep[u] + 1, fa[v] = u, dfs1(v, u);
}
void dfs2(int u, int From) {
  for (auto v : edge[u])
    if (!cov[v] && v != From) dfs2(v, u), f[u] -= f[v];
  f[u]++;
  Ans += abs(f[u]);
  return;
}
inline void main() {
  dfs1(1, 0);
  while (U ^ V) {
    if (dep[U] > dep[V])
      q[++tot] = U, cov[U] = true, U = fa[U];
    else
      q2[++tot2] = V, cov[V] = true, V = fa[V];
  }
  q[++tot] = U;
  cov[U] = true;
  while (tot2) q[++tot] = q2[tot2--];
  for (int i = 1; i <= tot; i++)
    dfs2(q[i], 0), Ans -= abs(f[q[i]]), sum += f[q[i]];
  K[1] = 1;
  B[1] = 0;
  for (int i = 2; i <= tot; i++) K[i] = -K[i - 1], B[i] = f[q[i]] - B[i - 1];
  if (sum & 1) puts("-1"), exit(0);
  if (tot & 1) {
    int x = f[q[1]] - B[tot];
    x >>= 1;
    for (int i = 1; i <= tot; i++) Ans = Ans + abs(K[i] * x + B[i]);
  } else {
    for (int i = 1; i <= tot; i++)
      if (K[i] < 0) B[i] = -B[i];
    sort(B + 1, B + tot + 1);
    for (int i = 1; i <= tot; i++) Ans = Ans + abs(B[i] - B[(tot + 1) >> 1]);
  }
  cout << Ans << endl;
}
}  // namespace cpp2
int main() {
  n = gi();
  m = gi();
  for (int i = 1; i <= n; i++) dsu[i] = i;
  for (int i = 1, u, v, fu, fv; i <= m; i++) {
    u = gi(), v = gi();
    fu = Find(u);
    fv = Find(v);
    if (fu != fv)
      edge[u].push_back(v), edge[v].push_back(u), dsu[fu] = fv;
    else
      U = u, V = v;
  }
  if (n != m)
    cpp1::main();
  else
    cpp2::main();
  return 0;
}
